feat: SSF step detector
This commit is contained in:
@@ -3,6 +3,8 @@ project(Pasada_Lib)
|
||||
SET(PASADA_SRC
|
||||
library.cpp
|
||||
iir_filter.cpp
|
||||
ssf_filter.cpp
|
||||
include/ssf_filter.h
|
||||
)
|
||||
|
||||
if(PASADA_BUILD_TESTS)
|
||||
|
||||
54
pasada-lib/include/ssf_filter.h
Normal file
54
pasada-lib/include/ssf_filter.h
Normal file
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// Created by david on 03.03.2026.
|
||||
//
|
||||
|
||||
#ifndef PASADASUPERPROJECT_SSF_FILTER_H
|
||||
#define PASADASUPERPROJECT_SSF_FILTER_H
|
||||
|
||||
#include "iir_filter.h"
|
||||
|
||||
#define FPS 60
|
||||
#define MAX_BPM 300
|
||||
|
||||
/**
|
||||
* Sum-Slope Function filter.
|
||||
*
|
||||
* See Zong et al, 2003: An open-source algorithm to detect onset of arterial blood pressure pulses.
|
||||
*/
|
||||
class SsfFilter {
|
||||
protected:
|
||||
size_t sw;
|
||||
Filt f_delta_u;
|
||||
Filt f_window;
|
||||
public:
|
||||
SsfFilter(size_t upslope_width);
|
||||
double filter(double val);
|
||||
};
|
||||
|
||||
/**
|
||||
* Provides dirac pulses upon steps, based on SSF input.
|
||||
*
|
||||
* Settling time is LEN_INIT (currently 3.0 sec),
|
||||
* no steps are detected before.
|
||||
*/
|
||||
class SsfStepDetector {
|
||||
protected:
|
||||
const size_t LEN_INIT;
|
||||
const size_t LEN_TH_WIN;
|
||||
size_t num_samples;
|
||||
double ssf_threshold;
|
||||
size_t len_refr;
|
||||
size_t n_refr;
|
||||
bool is_refr;
|
||||
double nm1_ssf;
|
||||
Filt f_ssf_mean;
|
||||
public:
|
||||
/**
|
||||
* @param len_refr duration of refractory period, in samples
|
||||
*/
|
||||
SsfStepDetector(size_t len_refr);
|
||||
double filter(double val);
|
||||
double peek_threshold();
|
||||
};
|
||||
|
||||
#endif //PASADASUPERPROJECT_SSF_FILTER_H
|
||||
65
pasada-lib/ssf_filter.cpp
Normal file
65
pasada-lib/ssf_filter.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
//
|
||||
// Created by david on 03.03.2026.
|
||||
//
|
||||
|
||||
#include "include/ssf_filter.h"
|
||||
#include <limits>
|
||||
#include <cmath>
|
||||
#include <cassert>
|
||||
|
||||
static std::vector<double> make_ones(size_t sw) {
|
||||
std::vector<double> ones;
|
||||
ones.resize(sw);
|
||||
ones.assign(sw, 1.0);
|
||||
return ones;
|
||||
}
|
||||
|
||||
SsfFilter::SsfFilter(size_t upslope_width) :
|
||||
sw(upslope_width),
|
||||
// Filt(N, shift, offset, taps)
|
||||
f_delta_u(2, 0, 0, std::vector {1.0, -1.0}),
|
||||
f_window(upslope_width, 0, 0, make_ones(upslope_width))
|
||||
{}
|
||||
double SsfFilter::filter(double val) {
|
||||
double du = f_delta_u.filter(val);
|
||||
double duc = std::max(0.0, du);
|
||||
double ssf = f_window.filter(duc);
|
||||
return ssf;
|
||||
}
|
||||
|
||||
|
||||
SsfStepDetector::SsfStepDetector(size_t len_refr) :
|
||||
LEN_INIT((size_t) (3.0 * FPS)), // initial window length for ssf_threshold
|
||||
LEN_TH_WIN((size_t) (3.0 * FPS)), // subsequent window length for ssf_threshold
|
||||
num_samples(0),
|
||||
ssf_threshold(std::numeric_limits<double>::infinity()),
|
||||
len_refr(len_refr), n_refr(0), is_refr(false),
|
||||
nm1_ssf(0.0),
|
||||
f_ssf_mean(LEN_TH_WIN, 0, 0, make_ones(LEN_TH_WIN))
|
||||
{
|
||||
assert (LEN_INIT >= LEN_TH_WIN && "LEN_INIT < LEN_TH_WIN, check normalization of initial ssf_threshold");
|
||||
}
|
||||
double SsfStepDetector::filter(double val) {
|
||||
double ssf_mean = f_ssf_mean.filter(val) / ((double) LEN_TH_WIN);
|
||||
double rv = 0.0;
|
||||
if (num_samples >= LEN_INIT) {
|
||||
// initial and subsequent threshold setting.
|
||||
ssf_threshold = 3.0 * ssf_mean * 0.99; // see Zong 2003 for the magic numbers
|
||||
}
|
||||
// threshold crossing detection
|
||||
bool is_txing = nm1_ssf < ssf_threshold && val >= ssf_threshold;
|
||||
// refractory period reset
|
||||
if (num_samples - n_refr >= len_refr) is_refr = false;
|
||||
// transition and not in refractory period? detected a step.
|
||||
if (is_txing && !is_refr) {
|
||||
rv = 1.0;
|
||||
is_refr = true;
|
||||
n_refr = num_samples;
|
||||
}
|
||||
nm1_ssf = val;
|
||||
num_samples++;
|
||||
return rv;
|
||||
}
|
||||
double SsfStepDetector::peek_threshold() {
|
||||
return ssf_threshold;
|
||||
}
|
||||
Reference in New Issue
Block a user