/* * © Copyright 2014 – Urheberrechtshinweis * Alle Rechte vorbehalten / All Rights Reserved * * Programmcode ist urheberrechtlich geschuetzt. * Das Urheberrecht liegt, soweit nicht ausdruecklich anders gekennzeichnet, bei Frank Ebner. * Keine Verwendung ohne explizite Genehmigung. * (vgl. § 106 ff UrhG / § 97 UrhG) */ #ifndef STEPDETECTION3_H #define STEPDETECTION3_H #include "AccelerometerData.h" #include "../../data/Timestamp.h" #include #include #ifdef WITH_DEBUG_PLOT #include #include #include #include #include #include #endif #ifdef WITH_DEBUG_OUTPUT #include #endif #include "../../Assertions.h" #include "../../math/dsp/iir/BiQuadStack.h" #include "../../math/FixedFrequencyInterpolator.h" #include "../../math/DelayBuffer.h" /** * simple step detection based on accelerometer magnitude. * interpolated to a fixed frequency * passed through IIR filter * searching for zero crossings that follow a peak value * that is above a certain threshold */ class StepDetection3 { static constexpr float gravity = 9.81; static constexpr float stepRate_hz = 2.0; static constexpr float cutOff_hz = 3.0; static constexpr float iirQ = 0.70; static constexpr int sRate_hz = 100; static constexpr int every_ms = 1000 / sRate_hz; float threshold = 0.8; float max = 0; Timestamp maxTS; private: FixedFrequencyInterpolator interpol; IIR::BiQuadStack biquad; DelayBuffer lookBehind; #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 */ StepDetection3(bool useBandPass) : interpol(Timestamp::fromMS(every_ms)), lookBehind(5) { //biquad.setBandPass(stepRate_hz, 1.5, sRate_hz); //biquad.preFill(gravity); 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 } /** 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) // NOTE!!!! MIGHT TRIGGER MORE THAN ONCE PER add() !!! auto onResample = [&] (const Timestamp ts, const AccelerometerData data) { bool step = false; const float mag = data.magnitude(); // apply filter const float fMag = biquad.filter(mag - 9.81); // remove gravity // history buffer float fMagOld = lookBehind.add(fMag); // zero crossing? float tmp = max; if (fMagOld > 0 && fMag < 0) { if (max > threshold) { step = true; gotStep = true; } lookBehind.setAll(0); max = 0; } // track maximum value if (fMag > max) {max = fMag; maxTS = ts;} #ifdef WITH_DEBUG_OUTPUT if (step) { // // track delay due to zero crossing // const float tsDelay = ts.ms() - maxTS.ms(); // std::cout << "step at " << ts.ms() << ". delay due to zero crossing: " << tsDelay << std::endl; outSteps << maxTS.ms() << " " << tmp << "\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((maxTS-plotRef).ms(), tmp) ); } 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 }; // ensure fixed sampling rate for FIR freq filters to work! interpol.add(ts, acc, onResample); return gotStep; } }; #endif // STEPDETECTION3_H