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/MovingAverageTS.h
frank 857d7a1553 fixed some issues
added new pose/turn detections
new helper classes
define-flags for libEigen
2018-09-04 10:49:00 +02:00

96 lines
1.7 KiB
C++

#ifndef MOVINGAVERAGETS_H
#define MOVINGAVERAGETS_H
#include <vector>
#include "../data/Timestamp.h"
#include <algorithm>
#include "KahanSum.h"
template <typename T> class MovingAverageTS {
private:
/** timestamp -> value combination */
struct Entry {
Timestamp ts;
T value;
Entry(const Timestamp ts, const T& value) : ts(ts), value(value) {;}
};
/** the regional window to use */
Timestamp window;
/** the value history for the window-size */
std::vector<Entry> history;
const T zero;
/** current sum */
T sum;
/** more exact running summation of many values */
KahanSum<T> kSum;
public:
/** ctor with the window-size to use */
MovingAverageTS(const Timestamp window, const T zeroElement) : window(window), zero(zeroElement), sum(zeroElement), kSum(zeroElement) {
}
/** add a new entry */
void add(const Timestamp ts, const T& data) {
// append to history
history.push_back(Entry(ts, data));
// adjust sum
sum += data;
kSum += data;
// remove too-old history entries
const Timestamp oldest = ts - window;
while(history.front().ts < oldest) {
// adjust sum
sum -= history.front().value;
kSum -= history.front().value;
// remove from history
history.erase(history.begin());
}
}
/** get the current average (with increasing error due to float sum!) */
T getOldAPX() const {
return sum / history.size();
}
/** get the current average */
T get() const {
return kSum.get() / history.size();
}
// T get() const {
// T sum = zero;
// volatile T comp = zero;
// for (const auto e : history) {
// T inp = e.value;
// T y = inp - comp;
// T t = sum + y;
// comp = (t-sum) - y;
// sum = t;
// }
// return sum / history.size();
// }
};
#endif // MOVINGAVERAGETS_H