-
Notifications
You must be signed in to change notification settings - Fork 0
/
filters.h
44 lines (35 loc) · 830 Bytes
/
filters.h
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
class low_pass {
public:
low_pass(int factor) {
_factor = factor;
}
float filter(float reading) {
_value = _value * (_factor) + reading * (1.0 - _factor);
return _value;
}
float _value = 0;
float _factor = 0.99;
};
class mean_filter {
public:
mean_filter(int values) {
_values = values;
}
void init(float value){
for(int i=0;i<_values;i++){
_data[i] = value;
}
}
float filter(float reading) {
float temp = 0;
_data[_values - 1] = reading;
for (int i = 0; i < _values - 1; i++) {
temp += _data[i];
_data[i] = _data[i + 1];
}
temp += reading;
return temp / float(_values);
}
int _values = 5;
float _data[100] = {0};
};