port oxullo library Arduino

Dependents:   MAX30100_oxullo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MAX30100_BeatDetector.h Source File

MAX30100_BeatDetector.h

00001 /*
00002 Arduino-MAX30100 oximetry / heart rate integrated sensor library
00003 Copyright (C) 2016  OXullo Intersecans <x@brainrapers.org>
00004 This program is free software: you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation, either version 3 of the License, or
00007 (at your option) any later version.
00008 This program is distributed in the hope that it will be useful,
00009 but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 GNU General Public License for more details.
00012 You should have received a copy of the GNU General Public License
00013 along with this program.  If not, see <http://www.gnu.org/licenses/>.
00014 */
00015 
00016 #ifndef MAX30100_BEATDETECTOR_H
00017 #define MAX30100_BEATDETECTOR_H
00018 
00019 #include "mbed.h"
00020 #include <stdint.h>
00021 #include <algorithm>
00022 
00023 #define BEATDETECTOR_INIT_HOLDOFF                2000    // in ms, how long to wait before counting
00024 #define BEATDETECTOR_MASKING_HOLDOFF             200     // in ms, non-retriggerable window after beat detection
00025 #define BEATDETECTOR_BPFILTER_ALPHA              0.6     // EMA factor for the beat period value
00026 #define BEATDETECTOR_MIN_THRESHOLD               20      // minimum threshold (filtered) value
00027 #define BEATDETECTOR_MAX_THRESHOLD               800     // maximum threshold (filtered) value
00028 #define BEATDETECTOR_STEP_RESILIENCY             30      // maximum negative jump that triggers the beat edge
00029 #define BEATDETECTOR_THRESHOLD_FALLOFF_TARGET    0.3     // thr chasing factor of the max value when beat
00030 #define BEATDETECTOR_THRESHOLD_DECAY_FACTOR      0.99    // thr chasing factor when no beat
00031 #define BEATDETECTOR_INVALID_READOUT_DELAY       2000    // in ms, no-beat time to cause a reset
00032 #define BEATDETECTOR_SAMPLES_PERIOD              10      // in ms, 1/Fs
00033 
00034 
00035 typedef enum BeatDetectorState {
00036     BEATDETECTOR_STATE_INIT,
00037     BEATDETECTOR_STATE_WAITING,
00038     BEATDETECTOR_STATE_FOLLOWING_SLOPE,
00039     BEATDETECTOR_STATE_MAYBE_DETECTED,
00040     BEATDETECTOR_STATE_MASKING
00041 } BeatDetectorState;
00042 
00043 
00044 class BeatDetector
00045 {
00046 public:
00047     BeatDetector();
00048     bool addSample(float sample);
00049     float getRate();
00050     float getCurrentThreshold();
00051 
00052 private:
00053     bool checkForBeat(float value);
00054     void decreaseThreshold();
00055 
00056     BeatDetectorState state;
00057     float threshold;
00058     float beatPeriod;
00059     float lastMaxValue;
00060     uint32_t tsLastBeat;
00061     Timer t;
00062 };
00063 
00064 #endif