Compare commits
8 Commits
90f8943930
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| b919e845c7 | |||
| d88ac81345 | |||
| ba923c53bd | |||
| 13eecdb706 | |||
| 716a54e76e | |||
| bfb3c99184 | |||
| ee77180994 | |||
| 9aaec182a8 |
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
[submodule "google-tests/libnpy"]
|
||||
path = google-tests/libnpy
|
||||
url = https://github.com/llohse/libnpy.git
|
||||
@@ -9,9 +9,11 @@ include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR} libnpy/inclu
|
||||
# 'Google_Tests_run' is the target name
|
||||
# 'test1.cpp test2.cpp' are source files with tests
|
||||
add_executable(Google_Tests_run
|
||||
test_helpers.cpp
|
||||
test1.cpp
|
||||
test2.cpp
|
||||
test3.cpp
|
||||
test4.cpp
|
||||
)
|
||||
|
||||
file(COPY test1/data1.npy DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/test1)
|
||||
@@ -23,6 +25,10 @@ file(COPY test1/iir_t1_y.npy DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/test1)
|
||||
file(COPY test2/ssf_t2_acc.npy DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/test2)
|
||||
file(COPY test2/ssf_t2_y_ref.npy DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/test2)
|
||||
|
||||
file(COPY test3/ssf_t3_acc.npy DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/test3)
|
||||
|
||||
file(COPY test4/step_150a.npy DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/test4)
|
||||
|
||||
target_link_libraries(Google_Tests_run pasada)
|
||||
#target_include_directories(Google_Tests_run PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/pasada-lib/include")
|
||||
|
||||
|
||||
1
google-tests/libnpy
Submodule
1
google-tests/libnpy
Submodule
Submodule google-tests/libnpy added at 471fe480d5
@@ -6,56 +6,13 @@
|
||||
#include <vector>
|
||||
#include "iir_filter.h"
|
||||
#include "ssf_filter.h"
|
||||
#include "test_helpers.h"
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
|
||||
#define FPS 60
|
||||
#define MAX_BPM 300
|
||||
|
||||
template <typename T> static std::vector<double> apply_filter(T& filter, std::vector<double>& x) {
|
||||
std::vector<double> y;
|
||||
y.resize(x.size());
|
||||
for (int i = 0; i < x.size(); i++) {
|
||||
y[i] = filter.filter(x[i]);
|
||||
}
|
||||
return y;
|
||||
}
|
||||
|
||||
static void npy_save(std::string path, std::vector<double>& x) {
|
||||
npy::npy_data_ptr<double> d;
|
||||
d.data_ptr = x.data();
|
||||
d.shape = {(unsigned long) x.size()};
|
||||
npy::write_npy(path, d);
|
||||
}
|
||||
|
||||
static std::vector<double> fetch_y_axis(npy::npy_data<double>& acc) {
|
||||
// TODO: later on, we should use a vector projection towards gravity
|
||||
std::vector<double> signal;
|
||||
const size_t rows_real = acc.shape[0];
|
||||
#if DEBUG_IIR == 1
|
||||
const size_t rows = 5;
|
||||
#else
|
||||
const size_t rows = acc.shape[0];
|
||||
#endif
|
||||
int stride = 3;
|
||||
int offset = 1; // [x,y,z] per row - fetch y
|
||||
signal.resize(rows);
|
||||
if (acc.fortran_order) {
|
||||
stride = 1;
|
||||
offset = (int) rows_real;
|
||||
}
|
||||
/*
|
||||
std::cout << "is_fortran=" << acc.fortran_order << std::endl;
|
||||
for (size_t i = 0; i < 10; i++) {
|
||||
std::cout << "acc.data[" << i << "]=" << acc.data[i] << std::endl;
|
||||
}
|
||||
*/
|
||||
for (int i = 0; i < rows; i++) {
|
||||
signal[i] = acc.data[i * stride + offset];
|
||||
}
|
||||
return signal;
|
||||
}
|
||||
|
||||
TEST(HelloTest, Zong_SSF_Stage1) {
|
||||
npy::npy_data acc = npy::read_npy<double>("test2/ssf_t2_acc.npy");
|
||||
npy::npy_data y_ref = npy::read_npy<double>("test2/ssf_t2_y_ref.npy");
|
||||
@@ -166,16 +123,6 @@ TEST(HelloTest, Zong_SSF_Stage2) {
|
||||
npy_save("test2/ssf_t2_ssf.npy", ssf);
|
||||
}
|
||||
|
||||
/** Returns the ssf_threshold as the filter output for debugging. */
|
||||
class DebugSsfStepDetectorThreshold : public SsfStepDetector {
|
||||
public:
|
||||
DebugSsfStepDetectorThreshold(size_t len_refr) : SsfStepDetector(len_refr) {}
|
||||
double filter(double val) {
|
||||
this->SsfStepDetector::filter(val);
|
||||
return peek_threshold();
|
||||
}
|
||||
};
|
||||
|
||||
TEST(HelloTest, Zong_SSF_Stage3) {
|
||||
npy::npy_data acc = npy::read_npy<double>("test2/ssf_t2_acc.npy");
|
||||
|
||||
|
||||
@@ -2,9 +2,14 @@
|
||||
// Created by david on 04.03.2026.
|
||||
//
|
||||
#include <gtest/gtest.h>
|
||||
#include "npy.hpp"
|
||||
//#include <utility>
|
||||
#include <deque>
|
||||
#include <iomanip>
|
||||
|
||||
#include "pd_signal.h"
|
||||
#include "ssf_filter.h"
|
||||
#include "test_helpers.h"
|
||||
|
||||
using namespace pd_signal;
|
||||
|
||||
@@ -28,6 +33,41 @@ TEST(SignalTest, interp_t1) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(SignalTest, cross_corr_t1) {
|
||||
std::vector<double> sig {0.9, 1.5, 2.0, 3.0, 5.0, 4.0, 1.0, 0.5, 0.3, 0.2};
|
||||
double corr = pd_signal::crossCorr(sig, sig);
|
||||
ASSERT_NEAR(1.0, corr, 1e-7);
|
||||
}
|
||||
|
||||
TEST(SignalTest, cross_corr_t2) {
|
||||
std::vector<double> x {0.9, 1.5, 2.0, 3.0, 5.0, 4.0, 1.0, 0.5, 0.3, 0.2};
|
||||
std::vector<double> y {0.4, 0.7, 0.9, 1.5, 2.5, 2.0, 0.5, 0.25, 0.15, 0.1};
|
||||
double corr = pd_signal::crossCorr(x, y);
|
||||
ASSERT_NEAR(0.999, corr, 1e-3);
|
||||
}
|
||||
|
||||
TEST(SignalTest, resample_t1) {
|
||||
std::vector<double> x {0.9, 1.5, 2.0, 3.0, 5.0, 4.0, 1.0, 0.5, 0.3, 0.2};
|
||||
std::vector<double> y_e {0.9, 1.2, 1.5, 1.75, 2.0, 2.5, 3.0, 4.0, 5.0, 4.5, 4.0, 2.5, 1.0, 0.75, 0.5, 0.4, 0.3, 0.25, 0.2};
|
||||
|
||||
std::vector<double> t;
|
||||
linspace(t, 0, (double) x.size()-1, y_e.size(), false);
|
||||
// interp t=0.000 0.500 1.000 1.500 2.000 2.500 3.000 3.500 4.000 4.500 5.000 5.500 6.000 6.500 7.000 7.500 8.000 8.500 9.000
|
||||
/*
|
||||
std::cout << "interp t=";
|
||||
for (size_t n = 0; n < t.size(); n++) {
|
||||
std::cout << std::fixed << std::setw(5) << std::setprecision(3) << t[n] << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
*/
|
||||
|
||||
std::vector<double> y;
|
||||
pd_signal::resample(y, x, y_e.size());
|
||||
|
||||
double corr = pd_signal::crossCorr(y, y_e);
|
||||
ASSERT_NEAR(1.0, corr, 1e-3);
|
||||
}
|
||||
|
||||
TEST(SignalTest, ranges) {
|
||||
const double abs_error = 1e-5;
|
||||
std::vector<double> i;
|
||||
@@ -38,137 +78,29 @@ TEST(SignalTest, ranges) {
|
||||
ASSERT_NEAR(2.0, i[2], abs_error);
|
||||
}
|
||||
|
||||
/**
|
||||
* Running signal quality indicator.
|
||||
*/
|
||||
class RunningQuality {
|
||||
protected:
|
||||
// TODO: make it a filter (output proper samples)
|
||||
|
||||
/** template beat is resampled to this #samples */
|
||||
const int BEAT_LEN = 120 /* 2*FPS for 30 bpm lower end */;
|
||||
|
||||
/** threshold for accepting initial beats */
|
||||
const double BEAT_CORR_THR_1 = 0.9;
|
||||
/** threshold for accepting subsequent beats */
|
||||
const double BEAT_CORR_THR_2 = 0.8;
|
||||
/** absolute SSF threshold for accepting any beat */
|
||||
const double SSF_THRESHOLD = 5.0;
|
||||
/** number of recent beats to use for beat template. must be even (alternating feet have different patterns; make it symmetric) */
|
||||
const int NUM_BEATS = 4;
|
||||
|
||||
std::deque<std::vector<double> > beatTemplates;
|
||||
std::vector<double> beatTemplate;
|
||||
//std::vector<std::pair<int, int> > badBeatRanges;
|
||||
double beatCorrThr2;
|
||||
bool justLocked;
|
||||
int idx;
|
||||
|
||||
/** for debugging only - disable SSF_THRESHOLD */
|
||||
bool disableSsf;
|
||||
|
||||
void addTemplate(std::vector<double>& x) {
|
||||
beatTemplates.emplace_back(x);
|
||||
while (beatTemplates.size() > NUM_BEATS) {
|
||||
// sliding window on 'beat_templates', do not use all history
|
||||
beatTemplates.pop_front();
|
||||
}
|
||||
pd_signal::mean(beatTemplate, beatTemplates);
|
||||
}
|
||||
|
||||
void replaceTemplate(std::vector<double>& x) {
|
||||
beatTemplates.clear();
|
||||
beatTemplates.emplace_back(x);
|
||||
// essentially just a copy
|
||||
pd_signal::mean(beatTemplate, beatTemplates);
|
||||
}
|
||||
|
||||
virtual void dispatchLocked() { /* implement me, add Listener etc. */ }
|
||||
virtual void dispatchBeat(int idx, bool good, double posCorr) { /* implement me, add Listener etc. */ }
|
||||
|
||||
public:
|
||||
RunningQuality(): beatCorrThr2(BEAT_CORR_THR_2), justLocked(false), idx(0), disableSsf(false) {}
|
||||
explicit RunningQuality(bool disableSsf): beatCorrThr2(BEAT_CORR_THR_2), justLocked(false), idx(0), disableSsf(disableSsf) {}
|
||||
virtual ~RunningQuality() {}
|
||||
|
||||
// note: arg should be an iterator really, but can do later
|
||||
/**
|
||||
* @param beat individual beat accelero signal
|
||||
*/
|
||||
void append(std::vector<double> &rawBeat, std::vector<double> &rawSsf) {
|
||||
// TODO: should ignore crazy-long and very short beats here. (filter up on beat detector)
|
||||
|
||||
std::vector<double> beat;
|
||||
std::vector<double> ssf;
|
||||
resample(beat, rawBeat, BEAT_LEN);
|
||||
resample(ssf, rawSsf, BEAT_LEN);
|
||||
//std::ranges::copy(rawBeat, std::back_inserter(beat));
|
||||
|
||||
// check ssf at sample 2 (mid-slope of 4 window of ssf)
|
||||
// TODO: param upon SsfFilter.upslope_width/2 instead of hardcoding
|
||||
double checkedSsf = ssf[(int) (2*((double)beat.size())/((double)rawBeat.size()))];
|
||||
|
||||
double corr = std::numeric_limits<double>::quiet_NaN();
|
||||
double posCorr = std::numeric_limits<double>::quiet_NaN();
|
||||
bool goodBeat = false;
|
||||
if (beatTemplates.size() > 0) {
|
||||
corr = pd_signal::crossCorr(ssf, beatTemplate);
|
||||
posCorr = pd_signal::clip(corr, 0.0, 1.0);
|
||||
double corrThreshold = (beatTemplates.size() > 2) ? beatCorrThr2 : BEAT_CORR_THR_1;
|
||||
goodBeat = (corr > corrThreshold) && (checkedSsf > SSF_THRESHOLD || disableSsf);
|
||||
}
|
||||
|
||||
if (beatTemplates.size() == 0) {
|
||||
// cannot correlate the first beat, no template yet
|
||||
std::cerr << "(0) first beat -> addTemplate()" << std::endl;
|
||||
addTemplate(beat);
|
||||
justLocked = false;
|
||||
} else if (beatTemplates.size() <= 2) {
|
||||
// restart if there is no clear correlation between beats
|
||||
if (goodBeat) {
|
||||
std::cerr << "(2) good initial beat -> addTemplate()" << std::endl;
|
||||
addTemplate(beat);
|
||||
if (beatTemplates.size() > 2)
|
||||
justLocked = true; // TODO why not set? wrong compiler optimization? (is it unaware of member change?)
|
||||
//std::cerr << " (2) beatTemplates.size()=" << beatTemplates.size() << " justLocked=" << ((int) justLocked) << std::endl;
|
||||
} else {
|
||||
std::cerr << "(2) bad initial beat -> replaceTemplate()" << std::endl;
|
||||
replaceTemplate(beat);
|
||||
//badBeatRanges.clear();
|
||||
justLocked = false;
|
||||
}
|
||||
} else {
|
||||
// running mode: collect bad beats, but may be OK not to restart immediately
|
||||
|
||||
std::cerr << "(3) running mode, good=" << ((int) goodBeat) << " justLocked=" << ((int) justLocked) << std::endl;
|
||||
if (goodBeat) {
|
||||
addTemplate(beat);
|
||||
} else {
|
||||
// badBeatRanges.add(s, e)
|
||||
// numNoisy++
|
||||
}
|
||||
// runningCorrs.add(posCorr)
|
||||
if (justLocked) { dispatchLocked(); justLocked = false; }
|
||||
dispatchBeat(idx, goodBeat, posCorr);
|
||||
}
|
||||
idx++;
|
||||
}
|
||||
};
|
||||
|
||||
class DebugRunningQuality : public RunningQuality {
|
||||
protected:
|
||||
virtual void dispatchLocked() { locked = true; }
|
||||
virtual void dispatchBeat(int idx, bool good, double posCorr) { corrs.push_back(posCorr); }
|
||||
virtual void dispatchBeat(int idx, bool good, double posCorr) {
|
||||
if (locked && lockedAt == -1)
|
||||
lockedAt = idx;
|
||||
goods.push_back(good);
|
||||
corrs.push_back(posCorr);
|
||||
}
|
||||
|
||||
int lockedAt;
|
||||
bool locked;
|
||||
std::vector<double> corrs;
|
||||
std::vector<bool> goods;
|
||||
|
||||
public:
|
||||
DebugRunningQuality(): locked(false) {}
|
||||
DebugRunningQuality(): lockedAt(-1), locked(false) {}
|
||||
explicit DebugRunningQuality(bool disableSsf): RunningQuality(disableSsf), locked(false) {}
|
||||
virtual ~DebugRunningQuality() {}
|
||||
bool isLocked() { return locked; }
|
||||
std::vector<double> getCorrs() { return corrs; }
|
||||
std::vector<bool> getGoods() { return goods; }
|
||||
int getLockedAt() { return lockedAt; }
|
||||
std::vector<double> getBeatTemplate() { return this->beatTemplate; }
|
||||
};
|
||||
|
||||
@@ -213,3 +145,84 @@ TEST(SignalTest, RunningQuality_t1) {
|
||||
//ASSERT_NEAR(0.7, sqi.getBeatTemplate()[4], 1e-6); // nb. resampled!
|
||||
ASSERT_NEAR(num/norm, sqi.getCorrs()[0], 1e-3);
|
||||
}
|
||||
|
||||
TEST(SignalTest, RunningQuality_t2) {
|
||||
npy::npy_data acc = npy::read_npy<double>("test3/ssf_t3_acc.npy");
|
||||
|
||||
std::vector<double> signal = fetch_y_axis(acc);
|
||||
|
||||
#if (FPS != 60)
|
||||
#error "FPS must currently be 60, as highpass taps are pre-computed for that value"
|
||||
#endif
|
||||
|
||||
// TODO: SQI: cehck input file
|
||||
// TODO: SQI: print debug values corr,idx, checkedSsf
|
||||
|
||||
// Butterworth filter: order=5, fc=0.5, fs=60, btype='highpass'
|
||||
std::vector b {0.91875845, -4.59379227, 9.18758454, -9.18758454, 4.59379227, -0.91875845};
|
||||
std::vector a {1. , -4.83056552, 9.33652742, -9.02545247, 4.36360803, -0.8441171};
|
||||
IirFilter filter(b, a);
|
||||
|
||||
//std::cerr << "before stage 1" << std::endl;
|
||||
|
||||
// Stage 1: high-pass
|
||||
auto y = apply_filter(filter, signal);
|
||||
|
||||
Filt f_neg(1, 0, 0, std::vector {-1.0});
|
||||
auto y_neg = apply_filter(f_neg, y);
|
||||
|
||||
//std::cerr << "before stage 2" << std::endl;
|
||||
|
||||
// Stage 2: sum slope function
|
||||
const size_t upslope_width = 4;
|
||||
SsfFilter f_ssf(upslope_width);
|
||||
auto ssf = apply_filter(f_ssf, y_neg);
|
||||
|
||||
//std::cerr << "before stage 3" << std::endl;
|
||||
|
||||
// Stage 3: threshold detection
|
||||
const size_t len_refr = (size_t) (FPS / (MAX_BPM / 60));
|
||||
DebugSsfStepDetectorThreshold f_ssd_thr(len_refr);
|
||||
auto ssf_threshold = apply_filter(f_ssd_thr, ssf);
|
||||
|
||||
//std::cerr << "before writing results 1 and doing step detection" << std::endl;
|
||||
|
||||
npy_save("test2/ssf_t3_ssf_threshold.npy", ssf_threshold);
|
||||
|
||||
SsfStepDetector f_ssd(len_refr);
|
||||
auto steps = apply_filter(f_ssd, ssf);
|
||||
|
||||
//std::cerr << "before writing results 2" << std::endl;
|
||||
|
||||
npy_save("test2/ssf_t3_steps.npy", steps);
|
||||
|
||||
// Debug SQI
|
||||
|
||||
DebugRunningQuality sqi;
|
||||
|
||||
std::vector<double> beat_buf;
|
||||
std::vector<double> ssf_buf;
|
||||
// y, ssf
|
||||
size_t N = y.size();
|
||||
for (size_t i = 0; i < N; i++) {
|
||||
if (steps[i] == 1.0) {
|
||||
sqi.append(beat_buf, ssf_buf);
|
||||
beat_buf.clear();
|
||||
ssf_buf.clear();
|
||||
}
|
||||
beat_buf.push_back(y[i]);
|
||||
ssf_buf.push_back(ssf[i]);
|
||||
}
|
||||
|
||||
EXPECT_TRUE(sqi.isLocked());
|
||||
EXPECT_TRUE(sqi.getCorrs().size() > 50);
|
||||
|
||||
EXPECT_TRUE(sqi.getLockedAt() < 10);
|
||||
std::cout << "lockedAt=" << sqi.getLockedAt() << std::endl;
|
||||
|
||||
std::vector<double> corrs(sqi.getCorrs());
|
||||
npy_save("test3/ssf_t3_sqi_corrs.npy", corrs);
|
||||
|
||||
std::vector<bool> goods(sqi.getGoods());
|
||||
npy_save("test3/ssf_t3_sqi_goods.npy", goods);
|
||||
}
|
||||
|
||||
BIN
google-tests/test3/ssf_t3_acc.npy
Normal file
BIN
google-tests/test3/ssf_t3_acc.npy
Normal file
Binary file not shown.
38
google-tests/test4.cpp
Normal file
38
google-tests/test4.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
//
|
||||
// Created by david on 15.03.2026.
|
||||
//
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "step_detector.h"
|
||||
#include "npy.hpp"
|
||||
#include "test_helpers.h"
|
||||
|
||||
TEST(StepDetector, t1_sub_sample_resolution) {
|
||||
npy::npy_data s = npy::read_npy<double>("test4/step_150a.npy");
|
||||
|
||||
std::vector<double> signal = fetch_y_axis(s);
|
||||
const size_t N = signal.size();
|
||||
const size_t N_INIT = SsfStepDetector::initial_samples();
|
||||
|
||||
StepDetector det(nullptr, true);
|
||||
|
||||
// initialize: feed for priming the filters
|
||||
det.primeFilters(signal);
|
||||
|
||||
// feed for actual test
|
||||
for (size_t i = 0; i < N; i++) {
|
||||
const auto a_i = static_cast<float>(signal[i]);
|
||||
det.filter(std::vector<float> {0.0f, a_i, 0.0f});
|
||||
}
|
||||
|
||||
std::vector<double> ssd = det.getBufSsd(); // raw SsfStepDetector
|
||||
std::vector<double> sqi = det.getBufSqi(); // SQI - RunningQuality beat correlations
|
||||
std::vector<double> out = det.getBufOut(); // steps where SQI is OK
|
||||
|
||||
npy_save("test4/t1_ssd.npy", ssd);
|
||||
npy_save("test4/t1_sqi.npy", sqi);
|
||||
npy_save("test4/t1_out.npy", out);
|
||||
|
||||
// http://localhost:8888/notebooks/2026-03-10%20step%20interpolate%2F2026-03-15%20synth.ipynb
|
||||
}
|
||||
BIN
google-tests/test4/step_150.npy
Normal file
BIN
google-tests/test4/step_150.npy
Normal file
Binary file not shown.
BIN
google-tests/test4/step_150a.npy
Normal file
BIN
google-tests/test4/step_150a.npy
Normal file
Binary file not shown.
53
google-tests/test_helpers.cpp
Normal file
53
google-tests/test_helpers.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// Created by david on 11.03.2026.
|
||||
//
|
||||
|
||||
#include "test_helpers.h"
|
||||
|
||||
void npy_save(std::string path, std::vector<double>& x) {
|
||||
npy::npy_data_ptr<double> d;
|
||||
d.data_ptr = x.data();
|
||||
d.shape = {(unsigned long) x.size()};
|
||||
npy::write_npy(path, d);
|
||||
}
|
||||
void npy_save(std::string path, std::vector<bool>& x) {
|
||||
npy::npy_data_ptr<int> d;
|
||||
std::vector<int> xx(x.begin(), x.end());
|
||||
d.data_ptr = xx.data();
|
||||
d.shape = {(unsigned long) x.size()};
|
||||
npy::write_npy(path, d);
|
||||
}
|
||||
|
||||
std::vector<double> fetch_y_axis(npy::npy_data<double>& acc) {
|
||||
// TODO: later on, we should use a vector projection towards gravity
|
||||
std::vector<double> signal;
|
||||
const size_t rows_real = acc.shape[0];
|
||||
#if DEBUG_IIR == 1
|
||||
const size_t rows = 5;
|
||||
#else
|
||||
const size_t rows = acc.shape[0];
|
||||
#endif
|
||||
int stride = 3;
|
||||
int offset = 1; // [x,y,z] per row - fetch y
|
||||
signal.resize(rows);
|
||||
if (acc.fortran_order) {
|
||||
stride = 1;
|
||||
offset = (int) rows_real;
|
||||
}
|
||||
/*
|
||||
std::cout << "is_fortran=" << acc.fortran_order << std::endl;
|
||||
for (size_t i = 0; i < 10; i++) {
|
||||
std::cout << "acc.data[" << i << "]=" << acc.data[i] << std::endl;
|
||||
}
|
||||
*/
|
||||
for (int i = 0; i < rows; i++) {
|
||||
signal[i] = acc.data[i * stride + offset];
|
||||
}
|
||||
return signal;
|
||||
}
|
||||
|
||||
DebugSsfStepDetectorThreshold::DebugSsfStepDetectorThreshold(size_t len_refr) : SsfStepDetector(len_refr) {}
|
||||
double DebugSsfStepDetectorThreshold::filter(double val) {
|
||||
this->SsfStepDetector::filter(val);
|
||||
return peek_threshold();
|
||||
}
|
||||
34
google-tests/test_helpers.h
Normal file
34
google-tests/test_helpers.h
Normal file
@@ -0,0 +1,34 @@
|
||||
//
|
||||
// Created by david on 11.03.2026.
|
||||
//
|
||||
|
||||
#ifndef PASADASUPERPROJECT_TEST_HELPERS_H
|
||||
#define PASADASUPERPROJECT_TEST_HELPERS_H
|
||||
|
||||
#include "npy.hpp"
|
||||
#include "ssf_filter.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
template <typename T> static std::vector<double> apply_filter(T& filter, std::vector<double>& x) {
|
||||
std::vector<double> y;
|
||||
y.resize(x.size());
|
||||
for (int i = 0; i < x.size(); i++) {
|
||||
y[i] = filter.filter(x[i]);
|
||||
}
|
||||
return y;
|
||||
}
|
||||
|
||||
void npy_save(std::string path, std::vector<double>& x);
|
||||
void npy_save(std::string path, std::vector<bool>& x);
|
||||
|
||||
std::vector<double> fetch_y_axis(npy::npy_data<double>& acc);
|
||||
|
||||
/** Returns the ssf_threshold as the filter output for debugging. */
|
||||
class DebugSsfStepDetectorThreshold : public SsfStepDetector {
|
||||
public:
|
||||
DebugSsfStepDetectorThreshold(size_t len_refr);
|
||||
double filter(double val);
|
||||
};
|
||||
|
||||
#endif //PASADASUPERPROJECT_TEST_HELPERS_H
|
||||
@@ -5,6 +5,7 @@ SET(PASADA_SRC
|
||||
iir_filter.cpp
|
||||
ssf_filter.cpp
|
||||
pd_signal.cpp
|
||||
step_detector.cpp
|
||||
)
|
||||
|
||||
if(PASADA_BUILD_TESTS)
|
||||
|
||||
@@ -5,7 +5,9 @@
|
||||
#include "iir_filter.h"
|
||||
#include <iostream>
|
||||
|
||||
#ifndef DEBUG_IIR
|
||||
#define DEBUG_IIR 0
|
||||
#endif
|
||||
|
||||
#if (DEBUG_IIR == 1)
|
||||
#define DEBUG_PRINT(expr) do { expr; } while (0)
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#define PASADASUPERPROJECT_SSF_FILTER_H
|
||||
|
||||
#include "iir_filter.h"
|
||||
#include <deque>
|
||||
|
||||
#define FPS 60
|
||||
#define MAX_BPM 300
|
||||
@@ -51,6 +52,71 @@ public:
|
||||
SsfStepDetector(size_t len_refr);
|
||||
double filter(double val);
|
||||
double peek_threshold();
|
||||
|
||||
static size_t initial_samples();
|
||||
};
|
||||
|
||||
/**
|
||||
* Running signal quality indicator.
|
||||
*/
|
||||
class RunningQuality {
|
||||
protected:
|
||||
// TODO: make it a filter (output proper samples)
|
||||
|
||||
/** template beat is resampled to this #samples */
|
||||
const int BEAT_LEN = 120 /* 2*FPS for 30 bpm lower end */;
|
||||
|
||||
/** threshold for accepting initial beats */
|
||||
const double BEAT_CORR_THR_1 = 0.9;
|
||||
/** threshold for accepting subsequent beats */
|
||||
const double BEAT_CORR_THR_2 = 0.8;
|
||||
/** absolute SSF threshold for accepting any beat */
|
||||
const double SSF_THRESHOLD = 5.0;
|
||||
/** number of recent beats to use for beat template. must be even (alternating feet have different patterns; make it symmetric) */
|
||||
const int NUM_BEATS = 4;
|
||||
|
||||
std::deque<std::vector<double> > beatTemplates;
|
||||
std::vector<double> beatTemplate;
|
||||
//std::vector<std::pair<int, int> > badBeatRanges;
|
||||
double beatCorrThr2;
|
||||
bool justLocked;
|
||||
int idx;
|
||||
|
||||
/** for debugging only - disable SSF_THRESHOLD */
|
||||
bool disableSsf;
|
||||
|
||||
void addTemplate(std::vector<double>& x);
|
||||
|
||||
void replaceTemplate(std::vector<double>& x);
|
||||
|
||||
virtual void dispatchLocked();
|
||||
virtual void dispatchBeat(int idx, bool good, double posCorr);
|
||||
|
||||
public:
|
||||
RunningQuality();
|
||||
explicit RunningQuality(bool disableSsf);
|
||||
virtual ~RunningQuality();
|
||||
|
||||
// note: arg should be an iterator really, but can do later
|
||||
/**
|
||||
* @param beat individual beat accelero signal
|
||||
* @return true if it is good beat
|
||||
*/
|
||||
bool append(std::vector<double> &rawBeat, std::vector<double> &rawSsf);
|
||||
};
|
||||
|
||||
/**
|
||||
* Signal quality indicator.
|
||||
*/
|
||||
class RunningQualityFilter {
|
||||
protected:
|
||||
RunningQuality f_sqi;
|
||||
std::vector<double> beat_buf;
|
||||
std::vector<double> ssf_buf;
|
||||
double sqi;
|
||||
public:
|
||||
RunningQualityFilter(size_t upslope_width);
|
||||
double filter(double y, double ssf, double step);
|
||||
};
|
||||
|
||||
#endif //PASADASUPERPROJECT_SSF_FILTER_H
|
||||
52
pasada-lib/include/step_detector.h
Normal file
52
pasada-lib/include/step_detector.h
Normal file
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// Created by david on 15.03.2026.
|
||||
//
|
||||
|
||||
#ifndef PASADASUPERPROJECT_STEP_DETECTOR_H
|
||||
#define PASADASUPERPROJECT_STEP_DETECTOR_H
|
||||
|
||||
#include "iir_filter.h"
|
||||
#include "ssf_filter.h"
|
||||
#include <vector>
|
||||
|
||||
class StepListener {
|
||||
public:
|
||||
virtual ~StepListener() {}
|
||||
virtual void playBeat() = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Step detector from accelerometer signal.
|
||||
*
|
||||
* Settling time is 3.0 sec (defined in SsfStepDetector.LEN_INIT),
|
||||
* no steps are detected before.
|
||||
*/
|
||||
class StepDetector {
|
||||
protected:
|
||||
StepListener *listener;
|
||||
IirFilter f_highpass;
|
||||
Filt f_neg;
|
||||
SsfFilter f_ssf;
|
||||
SsfStepDetector f_ssd;
|
||||
RunningQualityFilter f_sqi;
|
||||
|
||||
bool debug;
|
||||
std::vector<double> buf_ssd;
|
||||
std::vector<double> buf_sqi;
|
||||
std::vector<double> buf_out;
|
||||
|
||||
public:
|
||||
StepDetector(StepListener *listener, bool debug = false);
|
||||
void filter(std::vector<float> values);
|
||||
std::vector<double> getBufSsd();
|
||||
std::vector<double> getBufSqi();
|
||||
std::vector<double> getBufOut();
|
||||
|
||||
/**
|
||||
* Prime the filters using the given input signal.
|
||||
* Used for debugging (non-realtime processing) to align the signal.
|
||||
*/
|
||||
void primeFilters(std::vector<double> sig);
|
||||
};
|
||||
|
||||
#endif //PASADASUPERPROJECT_STEP_DETECTOR_H
|
||||
@@ -90,7 +90,7 @@ void interp(std::vector<double>& y, std::vector<double>& x, std::vector<double>&
|
||||
void resample(std::vector<double> &out, std::vector<double> &x, int beat_len) {
|
||||
std::vector<double> t;
|
||||
std::vector<double> i;
|
||||
linspace(t, 0, (double) x.size(), beat_len, false);
|
||||
linspace(t, 0, (double) (x.size()-1), beat_len, false);
|
||||
linspace(i, 0, (int) (x.size()-1), (int) x.size(), false);
|
||||
interp(out, t, i, x);
|
||||
}
|
||||
|
||||
@@ -2,12 +2,24 @@
|
||||
// Created by david on 03.03.2026.
|
||||
//
|
||||
|
||||
#include "include/ssf_filter.h"
|
||||
#include "ssf_filter.h"
|
||||
#include "pd_signal.h"
|
||||
#include <limits>
|
||||
#include <cmath>
|
||||
#include <cassert>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
|
||||
#ifndef DEBUG_SSF
|
||||
#define DEBUG_SSF 0
|
||||
#endif
|
||||
|
||||
#if (DEBUG_SSF == 1)
|
||||
#define DEBUG_PRINT(expr) do { expr; } while (0)
|
||||
#else
|
||||
#define DEBUG_PRINT(expr) while(0) { expr; }
|
||||
#endif
|
||||
|
||||
|
||||
static std::vector<double> make_ones(size_t sw) {
|
||||
std::vector<double> ones;
|
||||
ones.resize(sw);
|
||||
@@ -28,8 +40,10 @@ double SsfFilter::filter(double val) {
|
||||
return ssf;
|
||||
}
|
||||
|
||||
size_t SsfStepDetector::initial_samples() { return (size_t) (3.0 * FPS); }
|
||||
|
||||
SsfStepDetector::SsfStepDetector(size_t len_refr) :
|
||||
// note: also change above, in initial_samples()
|
||||
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),
|
||||
@@ -65,15 +79,15 @@ double SsfStepDetector::filter(double ssf) {
|
||||
if (num_samples == LEN_INIT) {
|
||||
// initial threshold setting
|
||||
ssf_threshold = 3.0 * ssf_mean * 0.99; // see Zong 2003 for the magic numbers
|
||||
//std::cerr << "before prime()" << std::endl;
|
||||
//DEBUG_PRINT(std::cerr << "before prime()" << std::endl);
|
||||
f_ssf_threshold_smoothing.prime(ssf_threshold);
|
||||
} else if (num_samples > LEN_TH_WIN) {
|
||||
//std::cerr << "adaptive threshold setting" << std::endl;
|
||||
//DEBUG_PRINT(std::cerr << "adaptive threshold setting" << std::endl);
|
||||
// adaptive threshold setting
|
||||
// +2 is half the window size
|
||||
// TODO: param upon SsfFilter.upslope_width/2 instead of hardcoding -- also f_ssf_threshold_smoothing(), nb. should be even number
|
||||
if (num_samples == n_refr + 2) {
|
||||
//std::cerr << "setting adaptive threshold setting" << std::endl;
|
||||
//DEBUG_PRINT(std::cerr << "setting adaptive threshold setting" << std::endl);
|
||||
ssf_threshold_nm1 = ssf_threshold;
|
||||
// the ssf peak comes 3 samples (half-window + 1 sample) after the crossing
|
||||
ssf_threshold = f_ssf_threshold_smoothing.filter(ssf) / ((double) f_ssf_threshold_smoothing.size()) * 0.6;
|
||||
@@ -86,3 +100,103 @@ double SsfStepDetector::filter(double ssf) {
|
||||
double SsfStepDetector::peek_threshold() {
|
||||
return ssf_threshold;
|
||||
}
|
||||
|
||||
|
||||
void RunningQuality::addTemplate(std::vector<double>& x) {
|
||||
beatTemplates.emplace_back(x);
|
||||
while (beatTemplates.size() > NUM_BEATS) {
|
||||
// sliding window on 'beat_templates', do not use all history
|
||||
beatTemplates.pop_front();
|
||||
}
|
||||
pd_signal::mean(beatTemplate, beatTemplates);
|
||||
}
|
||||
|
||||
void RunningQuality::replaceTemplate(std::vector<double>& x) {
|
||||
beatTemplates.clear();
|
||||
beatTemplates.emplace_back(x);
|
||||
// essentially just a copy
|
||||
pd_signal::mean(beatTemplate, beatTemplates);
|
||||
}
|
||||
|
||||
void RunningQuality::dispatchLocked() { /* implement me, add Listener etc. */ }
|
||||
void RunningQuality::dispatchBeat(int idx, bool good, double posCorr) { /* implement me, add Listener etc. */ }
|
||||
|
||||
RunningQuality::RunningQuality(): beatCorrThr2(BEAT_CORR_THR_2), justLocked(false), idx(0), disableSsf(false) {}
|
||||
RunningQuality::RunningQuality(bool disableSsf): beatCorrThr2(BEAT_CORR_THR_2), justLocked(false), idx(0), disableSsf(disableSsf) {}
|
||||
RunningQuality::~RunningQuality() {}
|
||||
|
||||
// note: arg should be an iterator really, but can do later
|
||||
bool RunningQuality::append(std::vector<double> &rawBeat, std::vector<double> &rawSsf) {
|
||||
// TODO: should ignore crazy-long and very short beats here. (filter up on beat detector)
|
||||
|
||||
std::vector<double> beat;
|
||||
std::vector<double> ssf;
|
||||
pd_signal::resample(beat, rawBeat, BEAT_LEN);
|
||||
pd_signal::resample(ssf, rawSsf, BEAT_LEN);
|
||||
//std::ranges::copy(rawBeat, std::back_inserter(beat));
|
||||
|
||||
// check ssf at sample 2 (mid-slope of 4 window of ssf)
|
||||
// TODO: param upon SsfFilter.upslope_width/2 instead of hardcoding
|
||||
double checkedSsf = ssf[(int) (2*((double)beat.size())/((double)rawBeat.size()))];
|
||||
|
||||
double corr = std::numeric_limits<double>::quiet_NaN();
|
||||
double posCorr = std::numeric_limits<double>::quiet_NaN();
|
||||
bool goodBeat = false;
|
||||
if (beatTemplates.size() > 0) {
|
||||
corr = pd_signal::crossCorr(ssf, beatTemplate);
|
||||
posCorr = pd_signal::clip(corr, 0.0, 1.0);
|
||||
double corrThreshold = (beatTemplates.size() > 2) ? beatCorrThr2 : BEAT_CORR_THR_1;
|
||||
goodBeat = (corr > corrThreshold) && (checkedSsf > SSF_THRESHOLD || disableSsf);
|
||||
}
|
||||
|
||||
if (beatTemplates.size() == 0) {
|
||||
// cannot correlate the first beat, no template yet
|
||||
DEBUG_PRINT(std::cerr << "(0) first beat -> addTemplate()" << std::endl);
|
||||
addTemplate(ssf);
|
||||
justLocked = false;
|
||||
} else if (beatTemplates.size() <= 2) {
|
||||
// restart if there is no clear correlation between beats
|
||||
if (goodBeat) {
|
||||
DEBUG_PRINT(std::cerr << "(2) good initial beat -> addTemplate()" << std::endl);
|
||||
addTemplate(ssf);
|
||||
if (beatTemplates.size() > 2)
|
||||
justLocked = true;
|
||||
//DEBUG_PRINT(std::cerr << " (2) beatTemplates.size()=" << beatTemplates.size() << " justLocked=" << ((int) justLocked) << std::endl);
|
||||
} else {
|
||||
DEBUG_PRINT(std::cerr << "(2) bad initial beat idx=" << idx << " -> replaceTemplate() corr=" << std::fixed << std::setw(7) << std::setprecision(4) << corr << " checkedSsf=" << checkedSsf << std::endl);
|
||||
replaceTemplate(ssf);
|
||||
//badBeatRanges.clear();
|
||||
justLocked = false;
|
||||
}
|
||||
} else {
|
||||
// running mode: collect bad beats, but may be OK not to restart immediately
|
||||
|
||||
DEBUG_PRINT(std::cerr << "(3) running mode, good=" << ((int) goodBeat) << " justLocked=" << ((int) justLocked) << std::endl);
|
||||
if (goodBeat) {
|
||||
addTemplate(ssf);
|
||||
} else {
|
||||
// badBeatRanges.add(s, e)
|
||||
// numNoisy++
|
||||
}
|
||||
// runningCorrs.add(posCorr)
|
||||
if (justLocked) { dispatchLocked(); justLocked = false; }
|
||||
dispatchBeat(idx, goodBeat, posCorr);
|
||||
}
|
||||
idx++;
|
||||
if (!goodBeat) return 0.0;
|
||||
return posCorr;
|
||||
}
|
||||
|
||||
|
||||
RunningQualityFilter::RunningQualityFilter(size_t upslope_width) : sqi(0.0) {}
|
||||
|
||||
double RunningQualityFilter::filter(double y, double ssf, double step) {
|
||||
if (step == 1.0) {
|
||||
sqi = f_sqi.append(beat_buf, ssf_buf);
|
||||
beat_buf.clear();
|
||||
ssf_buf.clear();
|
||||
}
|
||||
beat_buf.push_back(y);
|
||||
ssf_buf.push_back(ssf);
|
||||
return sqi;
|
||||
}
|
||||
|
||||
70
pasada-lib/step_detector.cpp
Normal file
70
pasada-lib/step_detector.cpp
Normal file
@@ -0,0 +1,70 @@
|
||||
//
|
||||
// Created by david on 15.03.2026.
|
||||
//
|
||||
|
||||
#include "step_detector.h"
|
||||
|
||||
// TODO: we are hardcoding filter coefficients for 60 Hz
|
||||
// TODO: this is tolerable for 50 Hz
|
||||
|
||||
// TODO: check if we can do with floats instead of doubles
|
||||
// (check how much the [already bad] accuracy of filtering suffers)
|
||||
|
||||
// TODO: in Java, check if delta timestamps effectively match FPS
|
||||
// TODO: FPS constant should be passed as argument to C++ (but we keep an FPS define to validate the coefficients)
|
||||
|
||||
// Butterworth filter: order=5, fc=0.5, fs=60, btype='highpass'
|
||||
static std::vector<double> hpf_taps_b {0.91875845, -4.59379227, 9.18758454, -9.18758454, 4.59379227, -0.91875845};
|
||||
static std::vector<double> hpf_taps_a {1. , -4.83056552, 9.33652742, -9.02545247, 4.36360803, -0.8441171};
|
||||
static size_t upslope_width = 4;
|
||||
const size_t len_refr = (size_t) (FPS / (MAX_BPM / 60));
|
||||
|
||||
StepDetector::StepDetector(StepListener *listener, bool debug) :
|
||||
listener(listener),
|
||||
f_highpass(hpf_taps_b, hpf_taps_a),
|
||||
f_neg(1, 0, 0, std::vector<double> {-1.0}),
|
||||
f_ssf(upslope_width),
|
||||
f_ssd(len_refr),
|
||||
f_sqi(upslope_width),
|
||||
debug(debug)
|
||||
{}
|
||||
|
||||
#if (FPS != 60)
|
||||
#error "FPS must currently be 60, as highpass taps are pre-computed for that value"
|
||||
#endif
|
||||
|
||||
void StepDetector::filter(std::vector<float> values) {
|
||||
// TODO: later on, we should use a vector projection towards gravity
|
||||
auto s0 = (double) values[1]; // take y-axis value for now
|
||||
auto s1 = f_highpass.filter(s0);
|
||||
auto s2 = f_neg.filter(s1);
|
||||
auto s3 = f_ssf.filter(s2);
|
||||
auto s4 = f_ssd.filter(s3);
|
||||
auto q5 = f_sqi.filter(s2, s3, s4);
|
||||
if (debug) {
|
||||
buf_ssd.push_back(s4);
|
||||
buf_sqi.push_back(q5);
|
||||
buf_out.push_back(s4 * (q5 > 0.0 ? 1.0 : 0.0));
|
||||
}
|
||||
// is step, step quality is OK, and we have a listener?
|
||||
if(s4 > 0.0 && q5 > 0.0 && listener != nullptr) {
|
||||
listener->playBeat();
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<double> StepDetector::getBufSsd() { return buf_ssd; }
|
||||
std::vector<double> StepDetector::getBufSqi() { return buf_sqi; }
|
||||
std::vector<double> StepDetector::getBufOut() { return buf_out; }
|
||||
|
||||
void StepDetector::primeFilters(std::vector<double> sig) {
|
||||
const size_t N_INIT = SsfStepDetector::initial_samples();
|
||||
// initialize: feed for priming the filters
|
||||
for (size_t i = 0; i < N_INIT; i++) {
|
||||
const auto a_i = static_cast<float>(sig[i]);
|
||||
filter(std::vector<float> {0.0f, a_i, 0.0f});
|
||||
}
|
||||
// clear debug buffers
|
||||
buf_ssd.clear();
|
||||
buf_sqi.clear();
|
||||
buf_out.clear();
|
||||
}
|
||||
Reference in New Issue
Block a user