forked from zacsketches/Filter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilter.cpp
More file actions
104 lines (86 loc) · 1.96 KB
/
Copy pathFilter.cpp
File metadata and controls
104 lines (86 loc) · 1.96 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
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
102
103
104
#include "Filter.h"
//*******************************************************************
//* LIST DEFINITIONS
//*******************************************************************
//Destructor
FIFO_list::~FIFO_list() {
Node* p = head;
Node* q;
while (p != NULL){
q = p;
p = p->next;
delete q;
#ifdef COMPILE_FOR_CMD_LINE_TEST
std::cout<<"destroying node"<<std::endl;
#endif
}
}
// remove an element
void inline FIFO_list::remove_node(Node* p){
--cnt;
delete p;
}
// add elem to the end of L
void FIFO_list::append(int elem) {
Node* newNode = new Node;
newNode->data = elem;
newNode->next = 0;
if (cnt == 0) {
head = tail = newNode;
}
else {
tail->next = newNode;
tail = newNode;
}
cnt++;
}
// return sum of Nodes in the list
int FIFO_list::sum(){
Node* p = head;
int sum = 0;
while(p != 0){
sum += p->data;
p = p->next;
}
return sum;
}
// add new data to the FIFO list
void FIFO_list::add(int new_data) {
//append the new_data
if(head != NULL){
Node* p = head;
append(new_data);
head = head->next;
remove_node(p);
}
}
#ifdef COMPILE_FOR_CMD_LINE_TEST
//print element values to cout
void FIFO_list::print() {
Node* p = head;
while (p != NULL){
std::cout<<"\t"<<p->data<<std::endl;
p = p->next;
}
}
#endif
//*******************************************************************
//* MOVING AVERAGE DEFINITIONS
//*******************************************************************
//CONSTRUCTOR
Moving_average::Moving_average(int length, int default_data)
:len(length), his(len-1) {
for(size_t i = 0; i < his; ++i) {
data.append(default_data);
}
}
//filter an incoming data point and return the filtered value
int Moving_average::filter(int new_data) {
int result = data.sum() + new_data;
result = result / len;
//add the new data point to the history
data.add(new_data);
//update the current average
set_current(result);
return result;
}