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

47
math/Delay.h Normal file
View File

@@ -0,0 +1,47 @@
#ifndef DELAY_H
#define DELAY_H
#include <cstdint>
#include <vector>
template <typename T> class Delay {
private:
size_t size;
/** up to "size" elements */
std::vector<T> values;
public:
/** ctor */
Delay(const int size) : size(size) {;}
/** add a new value */
void add(const T val) {
// add new value
values.push_back(val);
// remove old ones
while(values.size() > size) {
values.erase(values.begin());
}
}
/** get the delayed value */
T get() const {
return values.front();
}
/** delay output valid? */
bool isValid() const {
return values.size() == size;
}
};
#endif // DELAY_H

View File

@@ -1,6 +1,63 @@
#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;
@@ -21,12 +78,12 @@ public:
Res(bool isMax, float val) : isMax(isMax), val(val) {;}
};
/** ctor. use only every n-th sample */
// ctor. use only every n-th sample
LocalMaxima(const size_t everyNth) : everyNth(everyNth) {
reset();
}
/** is the given value a local maxima? */
// is the given value a local maxima?
Res add(const float s) {
if (cnt == 0*everyNth) {s0 = s;} // set, wait some time
@@ -60,5 +117,5 @@ private:
}
};
*/
#endif // LOCALMAXIMA_H