-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathQuickStats.cpp
289 lines (267 loc) · 9.22 KB
/
QuickStats.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
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
280
281
282
283
284
285
286
287
288
289
/* QuickStats.cpp version 1.2.3 - Library for quick descriptive statistics of an array samples[] of size m
* Created by David Dubins, January 10th, 2016.
* Last updated: 01-Apr-25.
* Released into the public domain.
* Requires Arduino 1.6.6 or greater.
* https://github.com/dndubins/QuickStats/
*/
#include "Arduino.h"
#include "QuickStats.h"
#include <math.h>
QuickStats::QuickStats(){/*nothing to construct*/}
QuickStats::~QuickStats(){/*nothing to destruct*/}
float QuickStats::average(float samples[],int m)
{
float avg1=0.0;
for(int i=0;i<m;i++){
avg1+=(samples[i]-avg1)/(i+1); // iterative mean calculated here
}
return avg1;
}
float QuickStats::g_average(float samples[],int m)
{
float avg1=0.0;
for(int i=0;i<m;i++){
avg1+=(log(samples[i])-avg1)/(i+1); // iterative geometric mean calculated here
}
return exp(avg1);
}
float QuickStats::minimum(float samples[],int m)
{
float sorted[m]; //Define and initialize sorted array
for(int i=0;i<m;i++){
sorted[i]=samples[i];
}
bubbleSort(sorted,m); // Sort the values
return(sorted[0]); // first element is the minimum
}
float QuickStats::maximum(float samples[],int m)
{
float sorted[m]; //Define and initialize sorted array
for(int i=0;i<m;i++){
sorted[i]=samples[i];
}
bubbleSort(sorted,m); // Sort the values
return(sorted[m-1]); // last element is the maximum
}
float QuickStats::stdev(float samples[],int m)
{
float avg=0.0;
float total2=0.0;
avg=average(samples,m);
for(int i=0;i<m;i++){
total2 = total2 + pow(samples[i] - avg,2);
}
return sqrt(total2/((float)m-1.0));
}
float QuickStats::stderror(float samples[],int m)
{
float temp1=0.0;
temp1=stdev(samples,m);
return (temp1/sqrt((float)m));
}
float QuickStats::CV(float samples[],int m) //Coefficient of variation (%RSD, or relative stdev)
{
float avg=0.0;
float sd=0.0;
avg=average(samples,m);
sd=stdev(samples,m);
return 100.0*sd/avg;
}
float QuickStats::SNR(float samples[],int m) //Signal-to-noise ratio
{
float avg=0.0;
float sd=0.0;
avg=average(samples,m);
sd=stdev(samples,m);
return avg/sd;
}
void QuickStats::bubbleSort(float A[],int len)
{
unsigned long newn;
unsigned long n=len;
float temp=0.0;
do {
newn=1;
for(int p=1;p<len;p++){
if(A[p-1]>A[p]){
temp=A[p]; //swap places in array
A[p]=A[p-1];
A[p-1]=temp;
newn=p;
} //end if
} //end for
n=newn;
} while(n>1);
}
float QuickStats::fabs(float sample) // calculate absolute value
{
if(sample<0.f){
return -sample;
}else{
return sample;
}
}
float QuickStats::median(float samples[],int m) //calculate the median
{
//First bubble sort the values: https://en.wikipedia.org/wiki/Bubble_sort
float sorted[m]; //Define and initialize sorted array.
float temp=0.0; //Temporary float for swapping elements
/*Serial.println("Before:");
for(int j=0;j<m;j++){
Serial.println(samples[j]);
}*/
for(int i=0;i<m;i++){
sorted[i]=samples[i];
}
bubbleSort(sorted,m); // Sort the values
/*Serial.println("After:");
for(int i=0;i<m;i++){
Serial.println(sorted[i]);
}*/
if (bitRead(m,0)==1) { //If the last bit of a number is 1, it's odd. This is equivalent to "TRUE". Also use if m%2!=0.
return sorted[m/2]; //If the number of data points is odd, return middle number.
} else {
return (sorted[(m/2)-1]+sorted[m/2])/2; //If the number of data points is even, return avg of the middle two numbers.
}
}
float QuickStats::mode(float samples[],int m,float epsilon) //calculate the mode.
//epsilon is the tolerance for two measurements to be equivalent.
{
//First bubble sort the values: https://en.wikipedia.org/wiki/Bubble_sort
float sorted[m]; //Temporary array to sort values.
float temp=0; //Temporary float for swapping elements
float unique[m]; //Temporary array to store unique values
int uniquect[m]; //Temporary array to store unique counts
/*Serial.println("Before:");
for(int i=0;i<m;i++){
Serial.println(samples[i]);
}*/
for(int i=0;i<m;i++){
sorted[i]=samples[i];
}
bubbleSort(sorted,m); // Sort the values
/*Serial.println("Sorted:");
for(int i=0;i<m;i++){
Serial.println(sorted[i]);
}*/
// Now count the number of times each unique number appears in the sorted array.
unique[0]=sorted[0]; // first unique number is the first number
uniquect[0]=1; // first count is 1
int p=0; // array index for last unique number
int maxp=0;
int maxidx=0;
for(int i=1;i<m;i++){
if(fabs(sorted[i]-unique[p])<epsilon){ // if number same within tolerance
uniquect[p]++; // add to count of this number
if(uniquect[p]>maxp){ // if this exceeds the largest frequency
maxp=uniquect[p]; // record this number, it's the new mode
maxidx=p; // store the index of the number with the highest freq
}
} else {
p++; // found a new unique number
unique[p]=sorted[i];
uniquect[p]=1;
}
}
/*for(int i=0;i<p+1;i++){ //uncomment this section for debugging
Serial.print("Num: ");
Serial.print(unique[i],4);
Serial.print(" Count: ");
Serial.println(uniquect[i]);
}*/
if (maxp>1) {
return unique[maxidx]; //If there is more than one mode, return the lowest one.
} else {
return 0.0; //If there is no mode, return a zero.
}
}
float QuickStats::gmdn(float samples[], int m, float epsilon) //geothmetic meandian (https://bebac.at/articles/A-Rant-About-Nice-Numbers.phtml)
{ //epsilon is tolerance of conversion (where mean, geometric mean, and median considered equal)
float c1[3]={0.0, 0.0, 0.0}; // to hold mean, geometric mean, median
float c2[3]={0.0, 0.0, 0.0}; // to hold mean, geometric mean, median
float delta=0.0;
c1[0]=average(samples,m); // calculate the mean of the samples
c1[1]=g_average(samples,m); // calculate the geometric mean of the samples
c1[2]=median(samples,m); // calculate the median of the samples
do{
c2[0]=average(c1,3); // calculate the mean of the three (mean, geometric mean, median)
c2[1]=g_average(c1,3); // calculate the geometric mean of the three (mean, geometric mean, median)
c2[2]=median(c1,3); // calculate the median
delta=(c2[0]-c2[1])+(c2[0]-c2[2]); // calculate the difference between stats
for(byte i=0;i<3;i++){ // write back c2 to c1
c1[i]=c2[i];
}
}while(delta>epsilon); // did these numbers converge?
return c2[0]; // return converged value
}
float QuickStats::slope(float x[],float samples[],int m) //calculate the slope (dsamples/dx)
{
float xavg=average(x,m);
float yavg=average(samples,m);
float numerator = 0.0;
float denominator = 0.0;
for(int i=0;i<m;i++){
if(x[i]-xavg!=0.0){ // protect against dividing by zero
numerator = numerator + (x[i]-xavg)*(samples[i]-yavg);
denominator = denominator + ((x[i]-xavg)*(x[i]-xavg));
}
}
return numerator/denominator;
}
float QuickStats::intercept(float x[],float samples[],int m) //calculate the intercept (dsamples/dx)
{
float xavg=average(x,m);
float yavg=average(samples,m);
float beta=slope(x,samples,m);
return yavg-(beta*xavg);
}
float QuickStats::rsq(float x[],float samples[],int m) //calculate the rsq value
{
float yavg=average(samples,m); // get average of samples
float s=slope(x,samples,m); // get slope (dsamples/dx)
float b=intercept(x,samples,m); // get intercept
float fi[m]; // to hold model values of samples
float j[m]; // to hold (y-fi)^2 values
float k[m]; // to hold (y=yavg)^2 values
float SSres=0.0; // to hold sum of squares of residuals
float SStot=0.0; // to hold total sum of squares
for(int i=0;i<m;i++){
fi[i]=s*x[i]+b;
j[m]=pow(samples[i]-fi[i],2);
SSres+=j[m];
k[m]=pow(samples[i]-yavg,2);
SStot+=k[m];
}
return 1.0-(SSres/SStot); // calculate and return the rsq value
}
float QuickStats::rsq_adj(float x[],float samples[],int m) //calculate the adjusted rsq value
{
float a=rsq(x,samples,m);
return 1.0-(1.0-a)*(m-1.0)/(m-2.0); // calculate and return the adjusted rsq value
}
void QuickStats::filternan(float samples[],int &m) //removes nan values and returns size of filtered matrix (destructive)
{
int duds=0; //keep track of #nans
int nums=0; //keep track of numbers
float filtered[m];
for(int i=0;i<m;i++){
if(isnan(samples[i])||isinf(samples[i])){
duds++; // found a nan
}else{
filtered[nums]=samples[i];
nums++; // found a number
}
}
for(int i=0;i<nums;i++){
samples[i]=filtered[i]; //overwrite sample matrix with filtered matrix
}
m=nums; //overwrite matrix size
}
void QuickStats::f_round(float samples[], int m, int p) //round float variable to a given # decimals, p
{
float precision=pow(10.0,p);
for(int i=0;i<m;i++){
samples[i]=round(samples[i]*precision)/precision;
}
}