RIT VEXU Core API
Toggle main menu visibility
Loading...
Searching...
No Matches
moving_average.h
1
#pragma once
2
#include <vector>
3
8
class
Filter
{
9
public
:
10
virtual
void
add_entry(
double
n) = 0;
11
virtual
double
get_value()
const
= 0;
12
};
13
28
class
MovingAverage
:
public
Filter
{
29
public
:
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
67
private
:
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
87
class
ExponentialMovingAverage
:
public
Filter
{
88
public
:
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
126
private
:
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
};
ExponentialMovingAverage::get_size
int get_size()
Definition
moving_average.cpp:127
ExponentialMovingAverage::ExponentialMovingAverage
ExponentialMovingAverage(int buffer_size)
Definition
moving_average.cpp:75
ExponentialMovingAverage::get_value
double get_value() const override
Definition
moving_average.cpp:124
ExponentialMovingAverage::add_entry
void add_entry(double n) override
Definition
moving_average.cpp:102
Filter
Definition
moving_average.h:8
MovingAverage::get_size
int get_size() const
Definition
moving_average.cpp:68
MovingAverage::MovingAverage
MovingAverage(int buffer_size)
Definition
moving_average.cpp:25
MovingAverage::get_value
double get_value() const override
Definition
moving_average.cpp:65
MovingAverage::add_entry
void add_entry(double n) override
Definition
moving_average.cpp:52
include
core
utils
moving_average.h
Generated by
1.17.0