-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathMovingAverage.hpp
55 lines (37 loc) · 864 Bytes
/
MovingAverage.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#ifndef _RADIUM_COMMON_MOVING_AVERAGE_HPP
#define _RADIUM_COMMON_MOVING_AVERAGE_HPP
namespace radium{
struct MovingAverage{
int _pos = 0;
const int _i_array_size;
const double _d_array_size;
double *_array;
double _sum;
MovingAverage(int array_size, double value = 0)
: _i_array_size(array_size)
, _d_array_size(array_size)
{
_array = new double[array_size];
reset(value);
}
~MovingAverage(){
delete[] _array;
}
void reset(double value){
_sum = value * _d_array_size;
for(int i=0;i<_i_array_size;i++)
_array[i] = value;
}
double get(double new_value){
int last_pos = _pos+1;
if(last_pos==_i_array_size)
last_pos = 0;
_sum -= _array[last_pos];
_sum += new_value;
_array[_pos] = new_value;
_pos = last_pos;
return _sum / _d_array_size;
}
};
}
#endif