statistics.cpp
Go to the documentation of this file.
1 /*
2  * statistics.c
3  *
4  * Created on: Jul 30, 2013
5  * Author: Walt Johnson
6  */
7 
8 #define _MATH_DEFINES_DEFINED
9 #include <math.h>
10 #include "filters.h"
11 #include "statistics.h"
12 
13 
14 //_____ M A C R O S ________________________________________________________
15 
16 //_____ D E F I N I T I O N S ______________________________________________
17 
18 //_____ G L O B A L S ______________________________________________________
19 
20 //_____ P R O T O T Y P E S ________________________________________________
21 
22 
23 
24 f_t mean( f_t *input, int size, int byteIncrement )
25 {
26  int i;
27  f_t sum = 0;
28  char *ptr = (char*)input;
29 
30  // Validate size
31  if( size <= 0 )
32  return 0;
33 
34  // Find Average
35  for(i=0; i<size; i++)
36  {
37 // sum += input[i];
38  sum += *((f_t*)ptr);
39  ptr += byteIncrement;
40  }
41 
42  return sum/size;
43 }
44 
45 double mean_d(double *input, int size, int byteIncrement)
46 {
47  int i;
48  double sum = 0;
49  char *ptr = (char*)input;
50 
51  // Validate size
52  if (size <= 0)
53  return 0;
54 
55  // Find Average
56  for (i = 0; i < size; i++)
57  {
58  // sum += input[i];
59  sum += *((double*)ptr);
60  ptr += byteIncrement;
61  }
62 
63  return sum / size;
64 }
65 
66 
67 f_t mean_int32(int32_t *input, int size, int byteIncrement)
68 {
69  int i;
70  f_t sum = 0;
71  char *ptr = (char*)input;
72 
73  // Validate size
74  if (size <= 0)
75  return 0;
76 
77  // Find Average
78  for (i = 0; i < size; i++)
79  {
80  // sum += input[i];
81  sum += *((int32_t*)ptr);
82  ptr += byteIncrement;
83  }
84 
85  return sum / size;
86 }
87 
88 
89 double mean_int64(int64_t *input, int size, int byteIncrement)
90 {
91  int i;
92  double sum = 0;
93  char *ptr = (char*)input;
94 
95  // Validate size
96  if (size <= 0)
97  return 0;
98 
99  // Find Average
100  for (i = 0; i < size; i++)
101  {
102  // sum += input[i];
103  sum += *((int64_t*)ptr);
104  ptr += byteIncrement;
105  }
106 
107  return sum / size;
108 }
109 
110 
111 f_t variance( f_t *input, int size, int byteIncrement )
112 {
113  int i;
114  f_t dev;
115  f_t sum = 0;
116  f_t ave;
117  char *ptr = (char*)input;
118 
119  // Validate size
120  if( size <= 0 )
121  return 0;
122 
123  ave = mean(input, size, byteIncrement);
124 
125  for(i=0; i<size; i++)
126  {
127  dev = (*((f_t*)ptr)) - ave;
128  sum += dev * dev;
129  ptr += byteIncrement;
130  }
131 
132  return sum /= size;
133 }
134 
135 
136 double variance_d(double *input, int size, int byteIncrement)
137 {
138  int i;
139  double dev;
140  double sum = 0;
141  double ave;
142  char *ptr = (char*)input;
143 
144  // Validate size
145  if (size <= 0)
146  return 0;
147 
148  ave = mean_d(input, size, byteIncrement);
149 
150  for (i = 0; i < size; i++)
151  {
152  dev = (*((double*)ptr)) - ave;
153  sum += dev * dev;
154  ptr += byteIncrement;
155  }
156 
157  return sum /= size;
158 }
159 
160 
161 f_t variance_int32(int32_t *input, int size, int byteIncrement)
162 {
163  int i;
164  f_t dev;
165  f_t sum = 0;
166  f_t ave;
167  char *ptr = (char*)input;
168 
169  // Validate size
170  if (size <= 0)
171  return 0;
172 
173  ave = mean_int32(input, size, byteIncrement);
174 
175  for (i = 0; i < size; i++)
176  {
177  dev = ((f_t)(*((int32_t*)ptr))) - ave;
178  sum += dev * dev;
179  ptr += byteIncrement;
180  }
181 
182  return sum /= size;
183 }
184 
185 
186 double variance_int64(int64_t *input, int size, int byteIncrement)
187 {
188  int i;
189  double dev;
190  double sum = 0;
191  double ave;
192  char *ptr = (char*)input;
193 
194  // Validate size
195  if (size <= 0)
196  return 0;
197 
198  ave = mean_int64(input, size, byteIncrement);
199 
200  for (i = 0; i < size; i++)
201  {
202  dev = ((double)(*((int64_t*)ptr))) - ave;
203  sum += dev * dev;
204  ptr += byteIncrement;
205  }
206 
207  return sum /= size;
208 }
209 
210 
211 f_t variance_mean( f_t *input, f_t *ave, int size, int byteIncrement )
212 {
213  int i;
214  f_t dev;
215  f_t sum = 0;
216  char *ptr = (char*)input;
217 
218  // Validate size
219  if (size <= 0)
220  {
221  return 0;
222  }
223 
224  *ave = mean(input, size, byteIncrement);
225 
226  for(i=0; i<size; i++)
227  {
228  dev = (*((f_t*)ptr)) - *ave;
229  sum += dev * dev;
230  ptr += byteIncrement;
231  }
232 
233  return sum /= size;
234 }
235 
236 
237 f_t standard_deviation( f_t *input, int size, int byteIncrement )
238 {
239  // Validate size
240  if (size <= 0)
241  {
242  return 0;
243  }
244 
245  return _SQRT( variance( input, size, byteIncrement ) );
246 }
247 
248 double standard_deviation_d(double *input, int size, int byteIncrement)
249 {
250  // Validate size
251  if (size <= 0)
252  return 0;
253 
254  return sqrt(variance_d(input, size, byteIncrement));
255 }
256 
257 
258 f_t standard_deviation_int32(int32_t *input, int size, int byteIncrement)
259 {
260  // Validate size
261  if (size <= 0)
262  {
263  return 0;
264  }
265 
266  return _SQRT(variance_int32(input, size, byteIncrement));
267 }
268 
269 
270 double standard_deviation_int64(int64_t *input, int size, int byteIncrement)
271 {
272  // Validate size
273  if (size <= 0)
274  {
275  return 0;
276  }
277 
278  return sqrt(variance_int64(input, size, byteIncrement));
279 }
280 
281 
282 f_t standard_deviation_mean( f_t *input, f_t *mean, int size, int byteIncrement )
283 {
284  // Validate size
285  if (size <= 0)
286  {
287  return 0;
288  }
289 
290  return _SQRT( variance_mean( input, mean, size, byteIncrement ) );
291 }
292 
293 
294 void standard_deviation_Vec3( ixVector3 result, ixVector3 input, int size, int byteIncrement )
295 {
296  // Validate size
297  if (size <= 0)
298  {
299  return;
300  }
301 
302  result[0] = _SQRT( variance( &input[0], size, byteIncrement ) );
303  result[1] = _SQRT( variance( &input[1], size, byteIncrement ) );
304  result[2] = _SQRT( variance( &input[2], size, byteIncrement ) );
305 }
306 
307 
308 void stardard_deviation_mean_Vec3( ixVector3 result, ixVector3 input, ixVector3 mean, int size, int byteIncrement )
309 {
310  // Validate size
311  if (size <= 0)
312  {
313  return;
314  }
315 
316  result[0] = _SQRT( variance_mean( &input[0], &mean[0], size, byteIncrement ) );
317  result[1] = _SQRT( variance_mean( &input[1], &mean[1], size, byteIncrement ) );
318  result[2] = _SQRT( variance_mean( &input[2], &mean[2], size, byteIncrement ) );
319 }
320 
321 
322 void mean_Vec3( ixVector3 ave, ixVector3 input, int size, int byteIncrement )
323 {
324  ave[0] = mean(&input[0], size, byteIncrement);
325  ave[1] = mean(&input[1], size, byteIncrement);
326  ave[2] = mean(&input[2], size, byteIncrement);
327 }
328 
329 
330 // Initialize Alpha Filter alpha and beta values
331 void init_realtime_std_dev_Vec3( sRTSDVec3 *s, float dt, float aveCornerFreqHz, float varCornerFreqHz, ixVector3 initVal )
332 {
333  memset( s, 0, sizeof(sRTSDVec3) );
334  cpy_Vec3_Vec3( s->ave, initVal );
335 
336 // f_t dc;
337 // dc = dt * cornerFreqHz;
338 // s->alpha = dc / (1.0f + dc);
339 // s->beta = 1.0f - s->alpha;
340 
341  lpf_alpha_beta( dt, aveCornerFreqHz, &s->aveAlph, &s->aveBeta );
342  lpf_alpha_beta( dt, varCornerFreqHz, &s->varAlph, &s->varBeta );
343 }
344 
345 
346 // This does not produce as accurate of a standard deviation and is more processing expensive because it must run every
347 // iteration whereas standard_deviation_Vec3() does not. It does require less memory.
349 {
350  // Mean - LPF of input
351  O0_LPF_Vec3( s->ave, input, s->aveAlph, s->aveBeta );
352 
353  // Variance
354  ixVector3 dev;
355  ixVector3 var;
356  sub_Vec3_Vec3( dev, input, s->ave );
357  mul_Vec3_Vec3( var, dev, dev );
358  O0_LPF_Vec3( s->var, var, s->varAlph, s->varBeta );
359 
360  // Standard Deviation
361  sqrt_Vec3( s->std, s->var );
362 }
363 
364 
365 f_t root_mean_squared(f_t *input, int size, int byteIncrement, float ave)
366 {
367  // Validate size
368  if (size <= 0)
369  return 0;
370 
371  return _SQRT(delta_mean(input, size, byteIncrement, ave));
372 }
373 
374 
375 f_t delta_mean(f_t *input, int size, int byteIncrement, float ave)
376 {
377  int i;
378  f_t dev;
379  f_t sum = 0;
380  char *ptr = (char*)input;
381 
382  // Validate size
383  if (size <= 0)
384  return 0;
385 
386  for (i = 0; i < size; i++)
387  {
388  dev = (*((f_t*)ptr)) - ave;
389  sum += dev * dev;
390  ptr += byteIncrement;
391  }
392 
393  return sum /= size;
394 }
f_t variance_int32(int32_t *input, int size, int byteIncrement)
Definition: statistics.cpp:161
ixVector3 std
Definition: statistics.h:22
float varAlph
Definition: statistics.h:25
f_t ixVector3[3]
Definition: ISConstants.h:791
float f_t
Definition: ISConstants.h:786
void mul_Vec3_Vec3(ixVector3 result, const ixVector3 v1, const ixVector3 v2)
Definition: ISMatrix.c:492
float aveBeta
Definition: statistics.h:24
f_t standard_deviation_mean(f_t *input, f_t *mean, int size, int byteIncrement)
Definition: statistics.cpp:282
double variance_d(double *input, int size, int byteIncrement)
Definition: statistics.cpp:136
f_t standard_deviation(f_t *input, int size, int byteIncrement)
Definition: statistics.cpp:237
void standard_deviation_Vec3(ixVector3 result, ixVector3 input, int size, int byteIncrement)
Definition: statistics.cpp:294
void mean_Vec3(ixVector3 ave, ixVector3 input, int size, int byteIncrement)
Definition: statistics.cpp:322
static __inline void lpf_alpha_beta(float dt, float cornerFreq, float *alpha, float *beta)
Find alpha and beta parameters for single pole Low-Pass filter.
Definition: filters.h:497
f_t mean_int32(int32_t *input, int size, int byteIncrement)
Definition: statistics.cpp:67
double standard_deviation_int64(int64_t *input, int size, int byteIncrement)
Definition: statistics.cpp:270
ixVector3 ave
Definition: statistics.h:20
void sqrt_Vec3(ixVector3 result, const ixVector3 v)
Definition: ISMatrix.c:507
void sub_Vec3_Vec3(ixVector3 result, const ixVector3 v1, const ixVector3 v2)
Definition: ISMatrix.c:712
double mean_d(double *input, int size, int byteIncrement)
Definition: statistics.cpp:45
#define _SQRT
Definition: ISConstants.h:776
double mean_int64(int64_t *input, int size, int byteIncrement)
Definition: statistics.cpp:89
float varBeta
Definition: statistics.h:26
void init_realtime_std_dev_Vec3(sRTSDVec3 *s, float dt, float aveCornerFreqHz, float varCornerFreqHz, ixVector3 initVal)
Definition: statistics.cpp:331
double standard_deviation_d(double *input, int size, int byteIncrement)
Definition: statistics.cpp:248
f_t root_mean_squared(f_t *input, int size, int byteIncrement, float ave)
Definition: statistics.cpp:365
void realtime_std_dev_Vec3(f_t *input, sRTSDVec3 *s)
Definition: statistics.cpp:348
static __inline void O0_LPF_Vec3(ixVector3 result, const ixVector3 input, f_t alph, f_t beta)
Definition: ISMatrix.h:730
ixVector3 var
Definition: statistics.h:21
f_t variance_mean(f_t *input, f_t *ave, int size, int byteIncrement)
Definition: statistics.cpp:211
f_t mean(f_t *input, int size, int byteIncrement)
Definition: statistics.cpp:24
double variance_int64(int64_t *input, int size, int byteIncrement)
Definition: statistics.cpp:186
void stardard_deviation_mean_Vec3(ixVector3 result, ixVector3 input, ixVector3 mean, int size, int byteIncrement)
Definition: statistics.cpp:308
f_t delta_mean(f_t *input, int size, int byteIncrement, float ave)
Definition: statistics.cpp:375
f_t standard_deviation_int32(int32_t *input, int size, int byteIncrement)
Definition: statistics.cpp:258
f_t variance(f_t *input, int size, int byteIncrement)
Definition: statistics.cpp:111
static __inline void cpy_Vec3_Vec3(ixVector3 result, const ixVector3 v)
Definition: ISMatrix.h:426
float aveAlph
Definition: statistics.h:23


inertial_sense_ros
Author(s):
autogenerated on Sun Feb 28 2021 03:17:58