-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsma.h
75 lines (53 loc) · 1.22 KB
/
sma.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* "THE BEER-WARE LICENSE" (Revision 42):
* <robin.lilja@gmail.com> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return - Robin Lilja
*
* @file sma.h
* @author Robin Lilja
* @date 26 Sep 2015
*/
#ifndef _SMA_H_
#define _SMA_H_
//#include "Arduino.h"
/**
* Fast simple moving average class.
*/
class SMA {
public:
/**
* Constructor.
* @param length of buffer.
*/
SMA(uint8_t length) {
this->buffer = new float[length];
this->average = 0.0f;
this->pt = 0;
this->length = length;
for (uint8_t i = 0; i < length; i++) { buffer[i] = 0.0f; }
};
/**
* Update buffer with latest value.
* @param value new value to be added to buffer.
* @return the average value.
*/
float update(float value);
/**
* Get average value.
* @return the average value.
*/
float output() { return average; }
private:
float *buffer;
double average;
uint8_t pt;
uint8_t length;
};
inline float SMA::update(float value) {
average += (double)((value - buffer[pt]) / (float)length);
buffer[pt] = value;
pt = (pt+1) % length;
return average;
}
#endif /* _SMA_H_ */