-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFilter.cpp
More file actions
executable file
·240 lines (208 loc) · 6.2 KB
/
Filter.cpp
File metadata and controls
executable file
·240 lines (208 loc) · 6.2 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
* Filter - Arduino data filtering library
*
* The Filter library provides Arduino programmers with data filtering
* operations on a configurable number of recent values.
*
* Copyright 2012-2013 Karl Ward, Surya Mattu, Tom Igoe, and contributors
* See the file CREDITS for contributors and external code referenced/incorporated
* See the file COPYING for details on software licensing
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* Version 0.6.1 */
#include "Arduino.h"
#include "DataStream.h"
#include "Filter.h"
// CONSTRUCTORS
// Constructor: no-arg version
Filter::Filter() {
_sampleSize = 0;
_values.resize(0); // if you use no-arg constructor, you must call resize() yourself
}
// Constructor
Filter::Filter(long sampleSize) {
_sampleSize = sampleSize;
// ensure that Filter object has correct capacity to hold sampleSize elements
_values.resize(sampleSize);
}
// Copy constructor
Filter::Filter(const Filter& other) {
_sampleSize = other.capacity();
_values.resize(_sampleSize);
unsigned long otherSize = other.available();
for (unsigned long i = 0; i < otherSize; i++) {
write(other.peek(i));
}
}
// Operator assignment overload
Filter& Filter::operator= (const Filter& other) {
_sampleSize = other.capacity();
_values.resize(_sampleSize);
unsigned long otherSize = other.available();
for (unsigned long i = 0; i < otherSize; i++) {
write(other.peek(i));
}
return(*this);
}
// DATA STRUCTURE METHODS
unsigned long Filter::available() const {
return(_values.available());
}
unsigned long Filter::capacity() const {
return(_values.capacity());
}
bool Filter::contains(long value) const {
unsigned long count = available();
for (unsigned long i=0; i<count; i++) {
if (value == peek(i)) {
return(true);
}
}
return(false); // if we get this far, the value is not present
}
long Filter::peek() const {
return(_values.peek());
}
long Filter::peek(const long index) const {
return(_values.peek(index));
}
void Filter::resize(long newMaxSize) {
_values.resize(newMaxSize);
}
void Filter::write(long value) {
_values.write(value);
}
// GENERAL PURPOSE METHODS
String Filter::describe() const {
String description = String("stored values count: ");
description.concat(_values.available());
description.concat(" of ");
description.concat(_values.capacity());
description.concat("\n");
// show the first ten values
description.concat("values: ");
long i = 0;
while ((i < _values.available()) && (i < 10)) {
description.concat(_values.peek(i));
description.concat(' ');
i++;
}
if (i < _values.available()) {
description.concat('...');
}
description.concat("\n");
return(description);
}
// BASIC STATISTICS METHODS
long Filter::maximum() const {
long maximum = _values.peek(0); // slightly redundant, done to avoid comparison against undefined value
long i = 0;
while(i < _values.available()) {
if (_values.peek(i) > maximum) {
maximum = _values.peek(i);
}
i++;
}
return(maximum);
}
long Filter::mean() const {
long sum = 0;
long i = 0;
while (i < _values.available()) {
sum = sum + (_values.peek(i) * 10);
i++;
}
long mean = sum / _values.available();
mean = _longRound(mean, 10);
return(mean);
}
long Filter::median() const {
DataStream<long>* medianValues = _orderedValues();
long median;
// median is the element in the middle of the ordered list of values
long midpoint = 0;
if (_values.available() > 1) {
midpoint = (medianValues->available() - 1) / 2;
}
if (_values.available() % 2 == 1) { // we have an odd number of values
median = medianValues->peek(midpoint);
}
else { // we have an even number of values, so get mean of midpoint pair
// NOTE: we're doing floating point math in long rather than using floats
median = ((medianValues->peek(midpoint) + medianValues->peek(midpoint+1)) * 10) / 2;
median = _longRound(median, 10);
}
return(median);
}
long Filter::minimum() const {
long minimum = _values.peek(0); // slightly redundant, done to avoid comparison against undefined value
long i = 0;
while(i < _values.available()) {
if (_values.peek(i) < minimum) {
minimum = _values.peek(i);
}
i++;
}
return(minimum);
}
long Filter::stDevPopulation() const {
return(_stDev(0));
}
long Filter::stDevSample() const {
if (_values.available() > 1) {
return(_stDev(1));
}
else {
return(-1); // FIXME: this silently avoids divide by zero error, revisit
}
}
// private methods
long Filter::_longRound(long input, long multiplier) const {
if (input % multiplier < (multiplier/2)) {
input = input / multiplier; // round down
}
else {
input = (input / multiplier) + 1; // round up
}
return(input);
}
DataStream<long>* Filter::_orderedValues() const {
DataStream<long>* medianValues = (DataStream<long>*) malloc(sizeof(DataStream<long>));
medianValues->begin();
medianValues->resize(_values.available());
long i = 0;
while (i < _values.available()) {
medianValues->writeOrdered(_values.peek(i));
i++;
}
return(medianValues);
}
long Filter::_stDev(bool type) const {
// standard deviation calculation
long sum = 0;
for (long i = 0; i < _values.available(); i++) {
sum += sq(_values.peek(i) - mean()) * 100; // i.e. a multiplier of 10 (100 is 10 squared)
}
long denominator;
if (type == 0) {
denominator = _values.available(); // population
}
else {
denominator = _values.available() - 1; // sample
}
long stDev = sqrt(sum / denominator);
stDev = _longRound(stDev, 10); // round and undo that multiplier of 10
return(stDev);
}