10#ifndef M5_UNIT_HEART_UTILITY_PULSE_MONITOR_HPP
11#define M5_UNIT_HEART_UTILITY_PULSE_MONITOR_HPP
32 explicit EMA(
float factor) : _alpha(factor)
38 _ema_value = std::numeric_limits<float>::quiet_NaN();
41 inline float update(
float new_value)
43 if (!std::isnan(_ema_value)) {
44 _ema_value = _alpha * new_value + (1.0f - _alpha) * _ema_value;
46 _ema_value = new_value;
52 float _alpha{}, _ema_value{std::numeric_limits<float>::quiet_NaN()};
61 Filter(
const float cutoff,
const float sampling_rate)
63 setSamplingRate(cutoff, sampling_rate);
66 void setSamplingRate(
const float cutoff,
const float sampling_rate)
69 _samplingRate = sampling_rate;
70 _prevIn = _prevOut = 0.0f;
71 auto dt = 1.0f / _samplingRate;
72 auto RC = 1.0f / (2.0f * M_PI * _cutoff);
73 _alpha = RC / (RC + dt);
77 float process(
const float value)
79 float out = _ema.update(_alpha * (_prevOut + value - _prevIn));
87 float _cutoff{}, _samplingRate{};
88 float _prevIn{}, _prevOut{};
105 _sampling_rate{samplingRate},
106 _max_samples{(size_t)samplingRate * sec},
107 _filterIR(5.0f, samplingRate)
109 assert(sec >= 1 &&
"sec must be greater or eaual than 1");
110 assert(samplingRate >= 1.0f &&
"SamplingRate must be greater or equal than 1.0f");
150 void push_back(
const float ir,
const float red);
162 float latestIR()
const
164 return !_dataIR.empty() ? _dataIR.back() : std::numeric_limits<float>::quiet_NaN();
168 float calculate_bpm();
172 float _sampling_rate{};
173 size_t _max_samples{};
175 Filter _filterIR{0.5f, 100.f};
176 std::deque<float> _dataIR;
183 float _avered{}, _aveir{};
184 float _sumredrms{}, _sumirrms{};
Exponential Moving Average.
Definition pulse_monitor.hpp:30
Apply a high-pass filter and reverse polarity.
Definition pulse_monitor.hpp:59
Calculate BPM and SpO2, and detect the pulse beat.
Definition pulse_monitor.hpp:96
PulseMonitor(const float samplingRate, const uint32_t sec=5)
Costructor.
Definition pulse_monitor.hpp:103
bool isBeat() const
Detect beat?
Definition pulse_monitor.hpp:114
void push_back(const float ir)
Push back IR.
Definition pulse_monitor.cpp:39
void clear()
Clear inner data.
Definition pulse_monitor.cpp:29
float SpO2() const
Gets the SpO2.
Definition pulse_monitor.hpp:127
void update()
Update status.
Definition pulse_monitor.cpp:65
float bpm() const
Gets the BPM.
Definition pulse_monitor.hpp:119
void setSamplingRate(const float samplingRate)
Set the sampling rate.
Definition pulse_monitor.cpp:16
Top level namespace of M5stack.
MAX30100 Unit for M5UnitUnified.