-
Notifications
You must be signed in to change notification settings - Fork 0
/
Buffer.cpp
101 lines (93 loc) · 1.8 KB
/
Buffer.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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "Buffer.h"
Buffer::Buffer(int length)
:m_length(length)
{
m_buffer = new double[m_length] {0};
}
void Buffer::push(double value)
{
for (int count = m_length - 1; count>0; --count)
{
m_buffer[count] = m_buffer[count - 1];
}
if (m_numberOfElement<m_length)
++m_numberOfElement;
m_buffer[0] = value;
}
double Buffer::filter()
{
return 0.5 * m_buffer[0] + 0.25*m_buffer[1] + 0.25*m_buffer[2];
}
void Buffer::pushFilter(double value)
{
for (int count = m_length - 1; count>0; --count)
{
m_buffer[count] = m_buffer[count - 1];
}
if (m_numberOfElement<m_length)
++m_numberOfElement;
if (m_numberOfElement >= 3)
{
m_buffer[0] = value * 0.5 + 0.35 * m_buffer[1] +
0.15 * m_buffer[2];
}
else
m_buffer[0] = value;
}
void Buffer::pushFilter2(double value)
{
if (!m_first)
{
double valueOut = m_buffer[m_length - 1];
for (int count = m_length - 1; count > 0; --count)
{
m_buffer[count] = m_buffer[count - 1];
}
if (m_numberOfElement < m_length)
{
++m_numberOfElement;
m_sum += value;
}
else
{
m_sum += value;
m_sum -= valueOut;
}
m_buffer[0] = m_sum / m_numberOfElement;
}
else m_first = false;
}
double Buffer::average()
{
int limit = 10;
double dummy = 0;
if (m_length < 5)
limit = m_length;
for (int count = 0; count < limit; ++count)
{
if (m_buffer[count] != 0)
dummy += m_buffer[count];
}
return dummy / m_numberOfElement;
}
bool Buffer::isFull()
{
return m_numberOfElement >= m_length;
}
void Buffer::reset()
{
for (int count = 0; count < m_numberOfElement; ++count)
{
m_buffer[count] = 0;
}
m_numberOfElement = 0;
m_first = true;
}
double &Buffer::operator[](int index)
{
return m_buffer[index];
}
Buffer::~Buffer()
{
delete[] m_buffer;
}