File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
src/AudioTools/CoreAudio/AudioBasic Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments