65 lines
1022 B
C++
65 lines
1022 B
C++
#ifndef LOCALMAXIMA_H
|
|
#define LOCALMAXIMA_H
|
|
|
|
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
|