This repository has been archived on 2020-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
Files
Indoor/sensors/imu/StepDetection2.h
frank 857d7a1553 fixed some issues
added new pose/turn detections
new helper classes
define-flags for libEigen
2018-09-04 10:49:00 +02:00

198 lines
4.9 KiB
C++

#ifndef STEPDETECTION2_H
#define STEPDETECTION2_H
#include "AccelerometerData.h"
#include "../../data/Timestamp.h"
#include <cmath>
#include <vector>
#ifdef WITH_DEBUG_PLOT
#include <KLib/misc/gnuplot/Gnuplot.h>
#include <KLib/misc/gnuplot/GnuplotSplot.h>
#include <KLib/misc/gnuplot/GnuplotSplotElementLines.h>
#include <KLib/misc/gnuplot/GnuplotPlot.h>
#include <KLib/misc/gnuplot/GnuplotPlotElementLines.h>
#include <KLib/misc/gnuplot/GnuplotPlotElementPoints.h>
#endif
#ifdef WITH_DEBUG_OUTPUT
#include <fstream>
#endif
#include "../../Assertions.h"
#include "../../math/dsp/iir/BiQuadStack.h"
//#include "../../math/dsp/fir/Real.h"
//#include "../../math/dsp/fir/RealFactory.h"
#include "../../math/FixedFrequencyInterpolator.h"
#include "../../math/LocalMaxima.h"
#include "../../math/MovingAverageTS.h"
/**
* simple step detection based on accelerometer magnitude.
* magnitude > threshold? -> step!
* block for several msec until detecting the next one
*/
class StepDetection2 {
static constexpr int sRate_hz = 100;
static constexpr int every_ms = 1000 / sRate_hz;
private:
FixedFrequencyInterpolator<AccelerometerData> interpol;
//FIR::Real::Filter fir;
IIR::BiQuadStack<float> biquad;
LocalMaxima locMax;
// longterm average to center around zero
MovingAverageTS<float> avg = MovingAverageTS<float>(Timestamp::fromMS(2000), 0);
float threshold = 0.50;
float curFiltered = 0;
#ifdef WITH_DEBUG_PLOT
K::Gnuplot gp;
K::GnuplotPlot plot;
K::GnuplotPlotElementLines lineRaw;
K::GnuplotPlotElementLines lineFiltered;
K::GnuplotPlotElementPoints pointDet;
Timestamp plotRef;
Timestamp lastPlot;
#endif
#ifdef WITH_DEBUG_OUTPUT
std::ofstream outFiltered;
std::ofstream outSteps;
#endif
public:
/** ctor */
StepDetection2(bool useBandPass) : interpol(Timestamp::fromMS(every_ms)), locMax(8) {
//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
//FIR::Real::Factory fac(sRate_hz);
//fir.setKernel(fac.getBandpass(0.66, 2.0, 40));
if (useBandPass) {
biquad.resize(3);
biquad[0].setHighPass(1, 0.7, sRate_hz);
biquad[1].setLowPass(3.0, 0.7, sRate_hz);
biquad[2].setLowPass(3.0, 1.0, sRate_hz);
//biquad.setBandPass(2, 3.0, sRate_hz);
threshold = 0.6; // needs a little reduction
} else {
threshold = 0.8;
biquad.resize(1);
biquad[0].setLowPass(3, 0.7, sRate_hz);
}
#ifdef WITH_DEBUG_PLOT
gp << "set autoscale xfix\n";
plot.setTitle("Step Detection");
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
#ifdef WITH_DEBUG_OUTPUT
outFiltered = std::ofstream("/tmp/sd2_filtered.dat");
outSteps = std::ofstream("/tmp/sd2_steps.dat");
#endif
}
float getCurFiltered() const {
return curFiltered;
}
/** does the given data indicate a step? */
bool add(const Timestamp ts, const AccelerometerData& acc) {
bool gotStep = 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();
Assert::isNotNaN(mag, "detected NaN magnitude");
// use long-term average to center around zero
avg.add(ts, mag);
const float mag0 = mag - avg.get();
// const float f = fir.append(mag0);
// if (f != f) {return;}
const float f = biquad.filter(mag0);
const float fMag = f;
curFiltered = fMag;
Assert::isNotNaN(fMag, "detected NaN filtered magnitude");
const bool isMax = locMax.add(fMag);
const bool step = (isMax) && (locMax.get() > threshold);
if (step) {gotStep = true;}
#ifdef WITH_DEBUG_OUTPUT
if (step) {
outSteps << ts.ms() << " " << fMag << "\n";
outSteps.flush();
}
outFiltered << ts.ms() << " " << fMag << "\n";
#endif
#ifdef WITH_DEBUG_PLOT
if (plotRef.isZero()) {plotRef = ts;}
const Timestamp tsPlot = (ts-plotRef);
const Timestamp tsOldest = tsPlot - Timestamp::fromMS(5000);
lineRaw.add( K::GnuplotPoint2(tsPlot.ms(), mag) );
lineFiltered.add( K::GnuplotPoint2(tsPlot.ms(), fMag) );
if (step) {
pointDet.add( K::GnuplotPoint2(tsPlot.ms(), fMag) );
}
if (lastPlot + Timestamp::fromMS(50) < tsPlot) {
lastPlot = tsPlot;
auto remove = [tsOldest] (const K::GnuplotPoint2 pt) {return pt.x < tsOldest.ms();};
lineRaw.removeIf(remove);
lineFiltered.removeIf(remove);
pointDet.removeIf(remove);
gp.draw(plot);
gp.flush();
usleep(100);
}
#endif
};
//qDebug() << ts.ms() << " ---" << acc.x << " " << acc.y << " " << acc.z;
// ensure fixed sampling rate for FIR freq filters to work!
interpol.add(ts, acc, onResample);
return gotStep;
}
};
#endif // STEPDETECTION2_H