forked from MajenkoLibraries/Average
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Average.h
279 lines (238 loc) · 7.85 KB
/
Average.h
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* Copyright (c) , Majenko Technologies
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of Majenko Technologies nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _AVERAGE_H
#define _AVERAGE_H
#if (ARDUINO >= 100)
# include <Arduino.h>
#else
# include <WProgram.h>
#endif
#include <math.h>
#define INT32_MAX 0x7fffffffL
#define INT32_MIN (-INT32_MAX - 1L)
inline static float sqr(float x) {
return x*x;
}
template <class T> class Average {
private:
// Private functions and variables here. They can only be accessed
// by functions within the class.
T *_store;
T _sum; // _sum variable for faster mean calculation
uint32_t _position; // _position variable for circular buffer
uint32_t _count;
uint32_t _size;
T _min;
T _max;
public:
// Public functions and variables. These can be accessed from
// outside the class.
Average(uint32_t size);
~Average();
float rolling(T entry);
void push(T entry);
float mean();
T mode();
T minimum();
T maximum();
T absoluteMinimum();
T absoluteMaximum();
float stddev();
T get(uint32_t);
void leastSquares(float &m, float &b, float &r);
int getCount();
T predict(int x);
};
template <class T> int Average<T>::getCount() {
return _count;
}
template <class T> Average<T>::Average(uint32_t size) {
_size = size;
_count = 0;
_min = INT32_MAX;
_max = INT32_MIN;
_store = (T *)malloc(sizeof(T) * size);
_position = 0; // track position for circular storage
_sum = 0; // track sum for fast mean calculation
for (uint32_t i = 0; i < size; i++) {
_store[i] = 0;
}
}
template <class T> Average<T>::~Average() {
free(_store);
}
template <class T> void Average<T>::push(T entry) {
if (_count < _size) { // adding new values to array
_count++; // count number of values in array
}
else { // overwriting old values
_sum = _sum -_store[_position]; // remove old value from _sum
}
_store[_position] = entry; // store new value in array
_sum += entry; // add the new value to _sum
_position += 1; // increment the position counter
if(entry < _min) _min = entry; // Keep absolute minimum
if(entry > _max) _max = entry; // Keep absolute maximum
if (_position >= _size) _position = 0; // loop the position counter
}
template <class T> float Average<T>::rolling(T entry) {
push(entry);
return mean();
}
template <class T> float Average<T>::mean() {
if (_count == 0) {
return 0;
}
return (float(_sum) / (float)_count); // mean calculation based on _sum
}
template <class T> T Average<T>::mode() {
uint32_t pos;
uint32_t inner;
T most;
uint32_t mostcount;
T current;
uint32_t currentcount;
if (_count == 0) {
return 0;
}
most = _store[0];
mostcount = 1;
for(pos = 0; pos < _count; pos++) {
current = _store[pos];
currentcount = 0;
for(inner = pos + 1; inner < _count; inner++) {
if(_store[inner] == current) {
currentcount++;
}
}
if(currentcount > mostcount) {
most = current;
mostcount = currentcount;
}
// If we have less array slices left than the current
// maximum count, then there is no room left to find
// a bigger count. We have finished early and we can
// go home.
if(_count - pos < mostcount) {
break;
}
}
return most;
}
template <class T> T Average<T>::absoluteMinimum() {
return _min;
}
template <class T> T Average<T>::absoluteMaximum() {
return _max;
}
template <class T> T Average<T>::minimum() {
T minval;
if (_count == 0) {
return 0;
}
minval = _store[0];
for(uint32_t i = 0; i < _count; i++) {
if(_store[i] < minval) {
minval = _store[i];
}
}
return minval;
}
template <class T> T Average<T>::maximum() {
T maxval;
if (_count == 0) {
return 0;
}
maxval = _store[0];
for(uint32_t i = 0; i < _count; i++) {
if(_store[i] > maxval) {
maxval = _store[i];
}
}
return maxval;
}
template <class T> float Average<T>::stddev() {
float square;
float sum;
float mu;
float theta;
int i;
if (_count == 0) {
return 0;
}
mu = mean();
sum = 0;
for(uint32_t i = 0; i < _count; i++) {
theta = mu - (float)_store[i];
square = theta * theta;
sum += square;
}
return sqrt(sum/(float)_count);
}
template <class T> T Average<T>::get(uint32_t index) {
if (index >= _count) {
return 0;
}
int32_t cindex = _position-index; // position in circular buffer
if (cindex < 0) cindex = _size + cindex; // need to loop around for negative cindex
return _store[cindex];
}
template <class T> void Average<T>::leastSquares(float &m, float &c, float &r) {
float sumx = 0.0; /* sum of x */
float sumx2 = 0.0; /* sum of x**2 */
float sumxy = 0.0; /* sum of x * y */
float sumy = 0.0; /* sum of y */
float sumy2 = 0.0; /* sum of y**2 */
for (int i=0;i<_count;i++) {
sumx += i;
sumx2 += sqr(i);
sumxy += i * _store[i];
sumy += _store[i];
sumy2 += sqr(_store[i]);
}
float denom = (_count * sumx2 - sqr(sumx));
if (denom == 0) {
// singular matrix. can't solve the problem.
m = 0;
c = 0;
r = 0;
return;
}
m = 0 - (_count * sumxy - sumx * sumy) / denom;
c = (sumy * sumx2 - sumx * sumxy) / denom;
r = (sumxy - sumx * sumy / _count) / sqrt((sumx2 - sqr(sumx)/_count) * (sumy2 - sqr(sumy)/_count));
}
template <class T> T Average<T>::predict(int x) {
float m, c, r;
leastSquares(m, c, r); // y = mx + c;
T y = m * x + c;
return y;
}
#endif