fixed some plotting issues

modified step detection
This commit is contained in:
k-a-z-u
2018-06-06 11:21:00 +02:00
parent 9e6d9f4ce7
commit 38b633b9be
6 changed files with 143 additions and 19 deletions

View File

@@ -25,6 +25,7 @@
#include "../../math/dsp/FIRComplex.h"
#include "../../math/FixedFrequencyInterpolator.h"
#include "../../math/LocalMaxima.h"
#include "../../math/MovingAverageTS.h"
/**
@@ -43,12 +44,16 @@ private:
FIRComplex fir;
LocalMaxima locMax;
const float threshold = 0.5;
// longterm average to center around zero
MovingAverageTS<float> avg = MovingAverageTS<float>(Timestamp::fromMS(2000), 0);
const float threshold = 0.50;
#ifdef WITH_DEBUG_PLOT
K::Gnuplot gp;
K::GnuplotPlot plot;
K::GnuplotPlotElementLines lineMag;
K::GnuplotPlotElementLines lineRaw;
K::GnuplotPlotElementLines lineFiltered;
K::GnuplotPlotElementPoints pointDet;
Timestamp plotRef;
Timestamp lastPlot;
@@ -63,15 +68,18 @@ private:
public:
/** ctor */
StepDetection2() : interpol(Timestamp::fromMS(every_ms)), fir(sRate_hz), locMax(5) {
StepDetection2() : interpol(Timestamp::fromMS(every_ms)), fir(sRate_hz), locMax(8) {
fir.lowPass(0.66, 40); // allow deviation of +/- 0.66Hz
fir.shiftBy(2); // typical step freq ~2Hz
//fir.lowPass(0.66, 40); // allow deviation of +/- 0.66Hz
//fir.shiftBy(2.00); // typical step freq ~2Hz
fir.lowPass(3.5, 25); // everything up to 3 HZ
#ifdef WITH_DEBUG_PLOT
gp << "set autoscale xfix\n";
plot.setTitle("Step Detection");
plot.add(&lineMag); lineMag.getStroke().getColor().setHexStr("#000000");
plot.add(&lineRaw); lineRaw.getStroke().getColor().setHexStr("#0000FF");
plot.add(&lineFiltered); lineFiltered.getStroke().getColor().setHexStr("#000000");
plot.add(&pointDet); pointDet.setPointSize(2); pointDet.setPointType(7);
#endif
@@ -87,17 +95,23 @@ public:
bool step = false;
// accel-data incoming on a fixed sampling rate (needed for FIR to work)
auto onResample = [&] (const Timestamp ts, const AccelerometerData data) {
const float mag = data.magnitude();
const std::complex<float> c = fir.append(mag);
// use long-term average to center around zero
avg.add(ts, mag);
const float mag0 = mag - avg.get();
const std::complex<float> c = fir.append(mag0);
const float real = c.real();
if (real != real) {return;}
const float fMag = real;
LocalMaxima::Res res = locMax.add(fMag);
step = (res.isMax) && (res.val > threshold);
const bool isMax = locMax.add(fMag);
step = (isMax) && (locMax.get() > threshold);
#ifdef WITH_DEBUG_OUTPUT
if (step) {
@@ -113,7 +127,8 @@ public:
const Timestamp tsPlot = (ts-plotRef);
const Timestamp tsOldest = tsPlot - Timestamp::fromMS(5000);
lineMag.add( K::GnuplotPoint2(tsPlot.ms(), fMag) );
lineRaw.add( K::GnuplotPoint2(tsPlot.ms(), mag) );
lineFiltered.add( K::GnuplotPoint2(tsPlot.ms(), fMag) );
if (step) {
pointDet.add( K::GnuplotPoint2(tsPlot.ms(), fMag) );
@@ -123,7 +138,8 @@ public:
lastPlot = tsPlot;
auto remove = [tsOldest] (const K::GnuplotPoint2 pt) {return pt.x < tsOldest.ms();};
lineMag.removeIf(remove);
lineRaw.removeIf(remove);
lineFiltered.removeIf(remove);
pointDet.removeIf(remove);
gp.draw(plot);
@@ -136,6 +152,7 @@ public:
};
// ensure fixed sampling rate for FIR freq filters to work!
interpol.add(ts, acc, onResample);
return step;