-
Notifications
You must be signed in to change notification settings - Fork 10
/
runningAvg.cpp
156 lines (108 loc) · 3.45 KB
/
runningAvg.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
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
#include "runningAvg.h"
#include <stdlib.h>
#include <resizeBuff.h>
// Constructor, runningAvg wants to know how many data values to average over.
runningAvg::runningAvg(int inNumData) {
theValues = NULL;
resizeBuff(sizeof(float)*inNumData,(byte**)&theValues);
maxData = inNumData;
numValues = 0;
index = 0;
mResult = 0;
usingUpper = false;
upperLimit = 0;
usingLower = false;
lowerLimit = 0;
}
// Destructor, this recycles the data buffer.
runningAvg::~runningAvg(void) { resizeBuff(0,(byte**)&theValues); }
// The standard call. Drop in a value, pop out the average of the last n values.
float runningAvg::addData(float inData) {
float sum;
if (usingLower && inData<lowerLimit) return mResult;
if (usingUpper && inData>upperLimit) return mResult;
if (numValues<maxData) { // Early stages while filling.
theValues[index++] = inData; // Never been full so index must be ok.
numValues++;
} else {
if (index==maxData) { // Meaning its pointing past the array.
index = 0; // Cycle around.
}
theValues[index++] = inData; // And stuff the value in.
}
sum = 0;
for (int i=0;i<numValues;i++) { // We loop up to numValues but not including numValues.
sum = sum + theValues[i];
}
mResult = sum/numValues;
return mResult;
}
// This returns the current average that is stored since the last data point was added.
float runningAvg::getAve(void) { return mResult; }
// Run through the data values and return the largest one.
float runningAvg::getMax(void) {
float max = theValues[0];
for (int i=1;i<numValues;i++) {
if (theValues[i]>max) {
max = theValues[i];
}
}
return max;
}
// Run through the data values and return the smallest one.
float runningAvg::getMin(void) {
float min = theValues[0];
for (int i=1;i<numValues;i++) {
if (theValues[i]<min) {
min = theValues[i];
}
}
return min;
}
// Runs through the data, actually two times, then gives the difference between the
// largest and the smallest.
float runningAvg::getDelta(void) { return getMax()-getMin(); }
// Runs through the data, calculates and returns the standard deviation.
float runningAvg::getStdDev(void) {
float sum;
sum = 0;
for (int i=0;i<numValues;i++) {
sum = sum + pow(theValues[i]-mResult,2);
}
sum = sum/numValues;
return sqrt(sum);
}
// Returns the actual number of data items stored in the data.
int runningAvg::getNumValues(void) { return numValues; }
// Finds the nth data value from 0..numValues-1 and returns that value. Returns zero if it
// can't find the data item.
float runningAvg::getDataItem(int index) {
if (index>=0 && index < numValues) {
return theValues[index];
}
return 0;
}
// Anything above this will be cut from the data.
void runningAvg::setUpperLimit(float limit) {
upperLimit = limit;
usingUpper = true;
}
// Turn off the upper limiting.
void runningAvg::clearUpperLimit(void) { usingUpper = false; }
// Anything below this will be cut from the data.
void runningAvg::setLowerLimit(float limit) {
lowerLimit = limit;
usingLower = true;
}
// Turn off the lower limiting.
void runningAvg::clearLowerLimit(void) { usingLower = false; }
// Set both upper and lower limits.
void runningAvg::setLimits(float lowerLimit,float upperLimit) {
setLowerLimit(lowerLimit);
setUpperLimit(upperLimit);
}
// Shut down all limiting.
void runningAvg::clearLimits(void) {
clearUpperLimit();
clearLowerLimit();
}