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/math/LocalMaxima.h
k-a-z-u 38b633b9be fixed some plotting issues
modified step detection
2018-06-06 11:21:00 +02:00

122 lines
1.7 KiB
C++

#ifndef LOCALMAXIMA_H
#define LOCALMAXIMA_H
#include "Delay.h"
class LocalMaxima {
int delay;
Delay<float> d0;
Delay<float> d1;
float cur;
int block = 0;
public:
LocalMaxima(int delay) : delay(delay), d0(delay*2), d1(delay) {
;
}
bool add(const float s) {
if (block > 0) {--block;}
d0.add(s);
d1.add(s);
this->cur = s;
if (isMax()) {
block = delay;
return true;
}
return false;
}
float get() {
return d1.get();
}
private:
bool isMax() {
if (block > 0) {return false;}
if (!d0.isValid()) {return false;}
if (!d1.isValid()) {return false;}
const float s0 = d0.get();
const float s1 = d1.get();
const float s2 = cur;
return (s1 > s0) && (s1 > s2);
}
};
/*
class LocalMaxima {
static constexpr float MAX = 1e40;
size_t everyNth;
size_t cnt = 0;
float s0;
float s1; // center value
float s2;
public:
struct Res {
bool isMax;
float val;
Res(bool isMax, float val) : isMax(isMax), val(val) {;}
};
// ctor. use only every n-th sample
LocalMaxima(const size_t everyNth) : everyNth(everyNth) {
reset();
}
// is the given value a local maxima?
Res add(const float s) {
if (cnt == 0*everyNth) {s0 = s;} // set, wait some time
else if (cnt == 1*everyNth) {s1 = s;} // set, wait some time
else if (cnt == 2*everyNth) {s2 = s;} // set
else if (cnt > 2*everyNth) { // now shift values for every time step, until max is found
s0 = s1;
s1 = s2;
s2 = s;
}
++cnt;
if ((s1 > s0) && (s1 > s2)) {
Res res(true, s1);
reset();
return res;
}
return Res(false, 0);
}
private:
void reset() {
s0 = MAX;
s1 = MAX;
s2 = MAX;
cnt = 0;
}
};
*/
#endif // LOCALMAXIMA_H