-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunningAverageFilter.cpp
More file actions
59 lines (46 loc) · 1.25 KB
/
RunningAverageFilter.cpp
File metadata and controls
59 lines (46 loc) · 1.25 KB
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
#include "RunningAverageFilter.h"
#include <string.h> //for memeset
RunningAverageFilter::RunningAverageFilter()
{
flush();
}//end constructor
void RunningAverageFilter::push(uint16_t newValue)
{
//Subtract the old value from the total.
m_total -= m_pBuffer[m_writeIndex];
//Save the new value to our buffer and add it to the total.
m_pBuffer[m_writeIndex] = newValue;
m_total += newValue;
//Advance the writeIndex. Wrap around if needed.
m_writeIndex++;
if(m_writeIndex == BUFFER_LENGTH)
{
m_writeIndex = 0;
}
// Keep track of how many valid numbers we have in the buffer
m_count++;
if (m_count > BUFFER_LENGTH)
{
m_count = BUFFER_LENGTH;
}
}//end push
uint16_t RunningAverageFilter::currentAverage()
{
return (uint16_t)(m_total / BUFFER_LENGTH);
}//end currentAverage
bool RunningAverageFilter::primed()
{
// we are primed if we have a full history.
if (m_count >= BUFFER_LENGTH)
{
return true;
}
return false;
}//end primed
void RunningAverageFilter::flush()
{
m_writeIndex = 0;
m_total = 0;
m_count = 0;
memset(m_pBuffer, 0, sizeof(uint16_t) * BUFFER_LENGTH);
}//end flush