Line | |
---|
1 | #ifndef RunningAverage_h
|
---|
2 | #define RunningAverage_h
|
---|
3 |
|
---|
4 | // RunningAverage class
|
---|
5 | // based on RunningAverage library for Arduino
|
---|
6 | // source: http://playground.arduino.cc/Main/RunningAverage
|
---|
7 | template <typename T>
|
---|
8 | class RunningAverage
|
---|
9 | {
|
---|
10 | public:
|
---|
11 | RunningAverage(void);
|
---|
12 | RunningAverage(int);
|
---|
13 | ~RunningAverage();
|
---|
14 | void clear();
|
---|
15 | void addValue(T);
|
---|
16 | T getAverage() const;
|
---|
17 | void fillValue(T, int);
|
---|
18 | protected:
|
---|
19 | int _size;
|
---|
20 | int _cnt;
|
---|
21 | int _idx;
|
---|
22 | T _sum;
|
---|
23 | T * _ar;
|
---|
24 | static T zero;
|
---|
25 | };
|
---|
26 |
|
---|
27 | // RunningAverage class
|
---|
28 | // based on RunningAverage library for Arduino
|
---|
29 | // source: http://playground.arduino.cc/Main/RunningAverage
|
---|
30 | // author: Rob.Tillart@gmail.com
|
---|
31 | // Released to the public domain
|
---|
32 |
|
---|
33 | template <typename T>
|
---|
34 | T RunningAverage<T>::zero = static_cast<T>(0);
|
---|
35 |
|
---|
36 | template <typename T>
|
---|
37 | RunningAverage<T>::RunningAverage(int n)
|
---|
38 | {
|
---|
39 | _size = n;
|
---|
40 | _ar = (T*) malloc(_size * sizeof(T));
|
---|
41 | clear();
|
---|
42 | }
|
---|
43 |
|
---|
44 | template <typename T>
|
---|
45 | RunningAverage<T>::~RunningAverage()
|
---|
46 | {
|
---|
47 | free(_ar);
|
---|
48 | }
|
---|
49 |
|
---|
50 | // resets all counters
|
---|
51 | template <typename T>
|
---|
52 | void RunningAverage<T>::clear()
|
---|
53 | {
|
---|
54 | _cnt = 0;
|
---|
55 | _idx = 0;
|
---|
56 | _sum = zero;
|
---|
57 | for (int i = 0; i< _size; i++) _ar[i] = zero; // needed to keep addValue simple
|
---|
58 | }
|
---|
59 |
|
---|
60 | // adds a new value to the data-set
|
---|
61 | template <typename T>
|
---|
62 | void RunningAverage<T>::addValue(T f)
|
---|
63 | {
|
---|
64 | _sum -= _ar[_idx];
|
---|
65 | _ar[_idx] = f;
|
---|
66 | _sum += _ar[_idx];
|
---|
67 | _idx++;
|
---|
68 | if (_idx == _size) _idx = 0; // faster than %
|
---|
69 | if (_cnt < _size) _cnt++;
|
---|
70 | }
|
---|
71 |
|
---|
72 | // returns the average of the data-set added so far
|
---|
73 | template <typename T>
|
---|
74 | T RunningAverage<T>::getAverage() const
|
---|
75 | {
|
---|
76 | if (_cnt == 0) return zero; // NaN ? math.h
|
---|
77 | return _sum / _cnt;
|
---|
78 | }
|
---|
79 |
|
---|
80 | // fill the average with a value
|
---|
81 | // the param number determines how often value is added (weight)
|
---|
82 | // number should preferably be between 1 and size
|
---|
83 | template <typename T>
|
---|
84 | void RunningAverage<T>::fillValue(T value, int number)
|
---|
85 | {
|
---|
86 | clear();
|
---|
87 | for (int i = 0; i < number; i++)
|
---|
88 | {
|
---|
89 | addValue(value);
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | #endif
|
---|
Note:
See
TracBrowser
for help on using the repository browser.