initial commit

This commit is contained in:
kazu
2017-07-15 15:52:05 +02:00
parent 087def3773
commit fb0ff1c076
16 changed files with 1857 additions and 1 deletions

63
MovingAVG.h Executable file
View File

@@ -0,0 +1,63 @@
#ifndef MOVINGAVG_H
#define MOVINGAVG_H
#include <vector>
#include "RingBuffer.h"
template <typename T> class MovingAVG {
private:
/** track the current sum of the vector's values */
T curSum;
/** the number of elements to average */
int size;
/** up to "size" elements */
RingBuffer<T> values;
public:
/** ctor */
MovingAVG(const int size) : curSum(), size(size), values(size) {;}
/** add a new value */
void add(const T val) {
// add new value
//values.push_back(val);
values.add(val);
curSum += val;
// too many values?
//if ((int) values.size() > size) {
// curSum -= values.front();
// values.erase(values.begin());
//}
if (values.size() >= size) {
curSum -= values.get();
}
}
/** get the current average */
T get() const {
return curSum / values.size();
}
/** get the number of used entries */
int getNumUsed() const {
return (int) values.size();
}
/** get number of entries to average */
int getSize() const {
return size;
}
};
#endif // MOVINGAVG_H