Compare commits
2 Commits
b333712d9c
...
f591f4950f
| Author | SHA1 | Date | |
|---|---|---|---|
| f591f4950f | |||
| 2371d16af8 |
@@ -20,12 +20,13 @@ TEST(StepDetector, t1_sub_sample_resolution) {
|
|||||||
StepDetector det(fps, nullptr, true);
|
StepDetector det(fps, nullptr, true);
|
||||||
|
|
||||||
// initialize: feed for priming the filters
|
// initialize: feed for priming the filters
|
||||||
det.primeFilters(fps, signal);
|
double ts = det.primeFilters(fps, signal);
|
||||||
|
|
||||||
// feed for actual test
|
// feed for actual test
|
||||||
for (size_t i = 0; i < N; i++) {
|
for (size_t i = 0; i < N; i++) {
|
||||||
const auto a_i = static_cast<float>(signal[i]);
|
const auto a_i = static_cast<float>(signal[i]);
|
||||||
det.filter(std::vector<float> {0.0f, a_i, 0.0f});
|
det.filter(ts, std::vector<float> {0.0f, a_i, 0.0f});
|
||||||
|
ts += 1.0 / fps;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<double> ssd = det.getBufSsd(); // raw SsfStepDetector
|
std::vector<double> ssd = det.getBufSsd(); // raw SsfStepDetector
|
||||||
|
|||||||
@@ -44,6 +44,8 @@ namespace pd_signal {
|
|||||||
double mean(const std::vector<double>& in);
|
double mean(const std::vector<double>& in);
|
||||||
void diff(std::vector<double>& out, const std::vector<double>& in);
|
void diff(std::vector<double>& out, const std::vector<double>& in);
|
||||||
|
|
||||||
|
std::vector<double> gauss(size_t N, double mu, double sigma);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convolution of two polynomials given in ASCENDING power order.
|
* Convolution of two polynomials given in ASCENDING power order.
|
||||||
* If <c>p = p_0 + p_1 x + ... + p_{P-1} x^{P-1}</c> and likewise for q,
|
* If <c>p = p_0 + p_1 x + ... + p_{P-1} x^{P-1}</c> and likewise for q,
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#include "iir_filter.h"
|
#include "iir_filter.h"
|
||||||
#include "ssf_filter.h"
|
#include "ssf_filter.h"
|
||||||
|
#include "pd_resamp.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
class StepListener {
|
class StepListener {
|
||||||
@@ -15,6 +16,19 @@ public:
|
|||||||
virtual void playBeat() = 0;
|
virtual void playBeat() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** mean-filter the gravity vector, then take acceleration downwards */
|
||||||
|
class GravityFilter {
|
||||||
|
size_t N;
|
||||||
|
std::vector<double> gauss_taps;
|
||||||
|
Filt gx;
|
||||||
|
Filt gy;
|
||||||
|
Filt gz;
|
||||||
|
public:
|
||||||
|
// 5 secs buffer, prime y with direction of gravity (for tests & faster init)
|
||||||
|
GravityFilter(double fps);
|
||||||
|
double filter(std::vector<double> values);
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Step detector from accelerometer signal.
|
* Step detector from accelerometer signal.
|
||||||
*
|
*
|
||||||
@@ -24,7 +38,7 @@ public:
|
|||||||
class StepDetector {
|
class StepDetector {
|
||||||
protected:
|
protected:
|
||||||
StepListener *listener;
|
StepListener *listener;
|
||||||
Filt f_neg;
|
GravityFilter f_grav;
|
||||||
SsfFilter f_ssf;
|
SsfFilter f_ssf;
|
||||||
SsfStepDetector f_ssd;
|
SsfStepDetector f_ssd;
|
||||||
RunningQualityFilter f_sqi;
|
RunningQualityFilter f_sqi;
|
||||||
@@ -34,9 +48,14 @@ protected:
|
|||||||
std::vector<double> buf_sqi;
|
std::vector<double> buf_sqi;
|
||||||
std::vector<double> buf_out;
|
std::vector<double> buf_out;
|
||||||
|
|
||||||
|
Resampler res_x;
|
||||||
|
Resampler res_y;
|
||||||
|
Resampler res_z;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
StepDetector(double fps, StepListener *listener, bool debug = false);
|
StepDetector(double fps, StepListener *listener, bool debug = false);
|
||||||
void filter(std::vector<float> values);
|
void filter(double ts, std::vector<float> values);
|
||||||
|
void filter_a(double s1);
|
||||||
std::vector<double> getBufSsd();
|
std::vector<double> getBufSsd();
|
||||||
std::vector<double> getBufSqi();
|
std::vector<double> getBufSqi();
|
||||||
std::vector<double> getBufOut();
|
std::vector<double> getBufOut();
|
||||||
@@ -45,7 +64,7 @@ public:
|
|||||||
* Prime the filters using the given input signal.
|
* Prime the filters using the given input signal.
|
||||||
* Used for debugging (non-realtime processing) to align the signal.
|
* Used for debugging (non-realtime processing) to align the signal.
|
||||||
*/
|
*/
|
||||||
void primeFilters(double fps, std::vector<double> sig);
|
double primeFilters(double fps, std::vector<double> sig);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //PASADASUPERPROJECT_STEP_DETECTOR_H
|
#endif //PASADASUPERPROJECT_STEP_DETECTOR_H
|
||||||
@@ -161,6 +161,16 @@ void diff(std::vector<double>& out, const std::vector<double>& in) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<double> gauss(size_t N, double mu, double sigma) {
|
||||||
|
const double norm = sigma * sqrt(2.0 * kPi);
|
||||||
|
std::vector<double> data(N);
|
||||||
|
for (int i = 0; i < N; i++) {
|
||||||
|
const double x = i;
|
||||||
|
data[i] = std::exp(-0.5 * (x - mu) * (x - mu) / (sigma * sigma)) / norm;
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
// Convolution of two polynomials in ascending power order.
|
// Convolution of two polynomials in ascending power order.
|
||||||
void polymul(std::vector<cplx>& out,
|
void polymul(std::vector<cplx>& out,
|
||||||
const std::vector<cplx>& p, const std::vector<cplx>& q) {
|
const std::vector<cplx>& p, const std::vector<cplx>& q) {
|
||||||
|
|||||||
@@ -4,19 +4,63 @@
|
|||||||
|
|
||||||
#include "step_detector.h"
|
#include "step_detector.h"
|
||||||
|
|
||||||
|
#include "pd_signal.h"
|
||||||
|
|
||||||
StepDetector::StepDetector(double fps, StepListener *listener, bool debug) :
|
StepDetector::StepDetector(double fps, StepListener *listener, bool debug) :
|
||||||
listener(listener),
|
listener(listener),
|
||||||
f_neg(1, 0, 0, std::vector<double> {-1.0}),
|
f_grav(fps),
|
||||||
f_ssf(fps),
|
f_ssf(fps),
|
||||||
f_ssd(fps),
|
f_ssd(fps),
|
||||||
f_sqi(fps),
|
f_sqi(fps),
|
||||||
debug(debug)
|
debug(debug)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
void StepDetector::filter(std::vector<float> values) {
|
static int gravity_num_taps(double fps) {
|
||||||
// TODO: later on, we should use a vector projection towards gravity
|
return 5.0 * fps;
|
||||||
auto s1 = (double) values[1]; // take y-axis value for now
|
}
|
||||||
auto s2 = f_neg.filter(s1);
|
|
||||||
|
// 5 secs buffer, prime y with direction of gravity (for tests & faster init)
|
||||||
|
GravityFilter::GravityFilter(double fps) :
|
||||||
|
N(gravity_num_taps(fps)),
|
||||||
|
gauss_taps(pd_signal::gauss(N, N/2, N/4)),
|
||||||
|
gx(N, 0, 0, gauss_taps),
|
||||||
|
gy(N, 0, 0, gauss_taps),
|
||||||
|
gz(N, 0, 0, gauss_taps)
|
||||||
|
{
|
||||||
|
gy.prime(-9.81);
|
||||||
|
}
|
||||||
|
|
||||||
|
double GravityFilter::filter(std::vector<double> values) {
|
||||||
|
gx.push(values[0]);
|
||||||
|
gy.push(values[1]);
|
||||||
|
gz.push(values[2]);
|
||||||
|
double x = gx.peek(), y = gy.peek(), z = gz.peek();
|
||||||
|
double g = sqrt(x * x + y * y + z * z);
|
||||||
|
// e = mean(a)
|
||||||
|
double ex = x / g, ey = y / g, ez = z / g;
|
||||||
|
// e \in a
|
||||||
|
double vx = values[0] * ex;
|
||||||
|
double vy = values[1] * ey;
|
||||||
|
double vz = values[2] * ez;
|
||||||
|
return vx + vy + vz;
|
||||||
|
}
|
||||||
|
|
||||||
|
void StepDetector::filter(double ts, std::vector<float> values) {
|
||||||
|
// resample to smooth over Android sensor FPS variations
|
||||||
|
res_x.push(ts, values[0]);
|
||||||
|
res_y.push(ts, values[1]);
|
||||||
|
res_z.push(ts, values[2]);
|
||||||
|
while (res_x.peek()) {
|
||||||
|
double x = res_x.get(), y = res_y.get(), z = res_z.get();
|
||||||
|
std::vector<double> samp { x, y, z };
|
||||||
|
// gravity filtering
|
||||||
|
double a = f_grav.filter(samp);
|
||||||
|
// pass on accel sample
|
||||||
|
filter_a(a);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void StepDetector::filter_a(double s2) {
|
||||||
auto s3 = f_ssf.filter(s2);
|
auto s3 = f_ssf.filter(s2);
|
||||||
auto s4 = f_ssd.filter(s3);
|
auto s4 = f_ssd.filter(s3);
|
||||||
auto q5 = f_sqi.filter(s2, s3, s4);
|
auto q5 = f_sqi.filter(s2, s3, s4);
|
||||||
@@ -35,15 +79,18 @@ std::vector<double> StepDetector::getBufSsd() { return buf_ssd; }
|
|||||||
std::vector<double> StepDetector::getBufSqi() { return buf_sqi; }
|
std::vector<double> StepDetector::getBufSqi() { return buf_sqi; }
|
||||||
std::vector<double> StepDetector::getBufOut() { return buf_out; }
|
std::vector<double> StepDetector::getBufOut() { return buf_out; }
|
||||||
|
|
||||||
void StepDetector::primeFilters(double fps, std::vector<double> sig) {
|
double StepDetector::primeFilters(double fps, std::vector<double> sig) {
|
||||||
const size_t N_INIT = SsfStepDetector::initial_samples(fps);
|
const size_t N_INIT = SsfStepDetector::initial_samples(fps);
|
||||||
// initialize: feed for priming the filters
|
// initialize: feed for priming the filters
|
||||||
|
double ts = 0;
|
||||||
for (size_t i = 0; i < N_INIT; i++) {
|
for (size_t i = 0; i < N_INIT; i++) {
|
||||||
const auto a_i = static_cast<float>(sig[i]);
|
const auto a_i = static_cast<float>(sig[i]);
|
||||||
filter(std::vector<float> {0.0f, a_i, 0.0f});
|
filter(ts, std::vector<float> {0.0f, a_i, 0.0f});
|
||||||
|
ts += 1.0 / fps;
|
||||||
}
|
}
|
||||||
// clear debug buffers
|
// clear debug buffers
|
||||||
buf_ssd.clear();
|
buf_ssd.clear();
|
||||||
buf_sqi.clear();
|
buf_sqi.clear();
|
||||||
buf_out.clear();
|
buf_out.clear();
|
||||||
|
return ts;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user