-
Notifications
You must be signed in to change notification settings - Fork 9
/
AvgFilter.cpp
47 lines (38 loc) · 891 Bytes
/
AvgFilter.cpp
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
#include "AvgFilter.h"
AvgFilter::AvgFilter(uint8_t N)
{
init(N);
}
AvgFilter::~AvgFilter() {}
void AvgFilter::init(uint8_t N)
{
m_N = N;
m_ring_buffer = (float*)malloc(m_N * sizeof(float));
reset();
}
void AvgFilter::reset(float val)
{
m_val = val;
const float scaled_val = val / (float)m_N;
m_idx = 0;
for (uint8_t i = 0; i < m_N; i++)
m_ring_buffer[i] = scaled_val;
}
void AvgFilter::reset()
{
reset(0.0f);
}
float AvgFilter::apply(float inp)
{
// remove last value from ringbuffer
m_val -= m_ring_buffer[m_idx];
// add new value and update ringbuffer, rinbuffer stores the scaled value
const float scaled_inp = inp / (float)m_N;
m_val += scaled_inp;
m_ring_buffer[m_idx] = scaled_inp;
// check if we are at the end of the ringbuffer
m_idx++;
if (m_idx == m_N)
m_idx = 0;
return m_val;
}