RIT VEXU Core API
Loading...
Searching...
No Matches
moving_average.h
1#pragma once
2#include <vector>
3
8class Filter {
9public:
10 virtual void add_entry(double n) = 0;
11 virtual double get_value() const = 0;
12};
13
28class MovingAverage : public Filter {
29public:
30 /*
31 * Create a moving average calculator with 0 as the default value
32 *
33 * @param buffer_size The size of the buffer. The number of samples that constitute a valid reading
34 */
35 MovingAverage(int buffer_size);
36 /*
37 * Create a moving average calculator with a specified default value
38 * @param buffer_size The size of the buffer. The number of samples that constitute a valid reading
39 * @param starting_value The value that the average will be before any data is added
40 */
41 MovingAverage(int buffer_size, double starting_value);
42
43 /*
44 * Add a reading to the buffer
45 * Before:
46 * [ 1 1 2 2 3 3] => 2
47 * ^
48 * After:
49 * [ 2 1 2 2 3 3] => 2.16
50 * ^
51 * @param n the sample that will be added to the moving average.
52 */
53 void add_entry(double n) override;
54
59 double get_value() const override;
60
65 int get_size() const;
66
67private:
68 int buffer_index; // index of the next value to be overridden
69 std::vector<double> buffer; // all current data readings we've taken
70 double current_avg; // the current value of the data
71};
72
88public:
89 /*
90 * Create a moving average calculator with 0 as the default value
91 *
92 * @param buffer_size The size of the buffer. The number of samples that constitute a valid reading
93 */
94 ExponentialMovingAverage(int buffer_size);
95 /*
96 * Create a moving average calculator with a specified default value
97 * @param buffer_size The size of the buffer. The number of samples that constitute a valid reading
98 * @param starting_value The value that the average will be before any data is added
99 */
100 ExponentialMovingAverage(int buffer_size, double starting_value);
101
102 /*
103 * Add a reading to the buffer
104 * Before:
105 * [ 1 1 2 2 3 3] => 2
106 * ^
107 * After:
108 * [ 2 1 2 2 3 3] => 2.16
109 * ^
110 * @param n the sample that will be added to the moving average.
111 */
112 void add_entry(double n) override;
113
118 double get_value() const override;
119
124 int get_size();
125
126private:
127 int buffer_index; // index of the next value to be overridden
128 std::vector<double> buffer; // all current data readings we've taken
129 double current_avg; // the current value of the data
130};
Definition moving_average.h:87
int get_size()
Definition moving_average.cpp:127
ExponentialMovingAverage(int buffer_size)
Definition moving_average.cpp:75
double get_value() const override
Definition moving_average.cpp:124
void add_entry(double n) override
Definition moving_average.cpp:102
Definition moving_average.h:8
Definition moving_average.h:28
int get_size() const
Definition moving_average.cpp:68
MovingAverage(int buffer_size)
Definition moving_average.cpp:25
double get_value() const override
Definition moving_average.cpp:65
void add_entry(double n) override
Definition moving_average.cpp:52