Skip to content

Commit 710fe3f

Browse files
committed
new MovingAverage class
1 parent 766871e commit 710fe3f

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
#include "Collections/List.h"
3+
4+
namespace audio_tools {
5+
6+
/**
7+
* @brief Caclulates the moving average of a number of values
8+
* @ingroup basic
9+
* @author Phil Schatzmann
10+
* @copyright GPLv3
11+
*/
12+
13+
template <class N>
14+
class MovingAverage {
15+
public:
16+
MovingAverage(int size) {
17+
this->size = size;
18+
this->values = List<float>();
19+
this->values.reserve(size);
20+
}
21+
22+
void add(float value) {
23+
if (this->values.size() == this->size) {
24+
this->values.pop_front();
25+
}
26+
this->values.push_back(value);
27+
}
28+
29+
float average() {
30+
float sum = 0;
31+
for (int i = 0; i < this->values.size(); i++) {
32+
sum += this->values[i];
33+
}
34+
return sum / this->values.size();
35+
}
36+
37+
protected:
38+
List<N> values;
39+
int size;
40+
};
41+
42+
} // namespace audio_tools

0 commit comments

Comments
 (0)