Paparazzi UAS  v5.12_stable-4-g9b43e9b
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
pprz_stat.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 Kirk Scheper
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
27 #include "math/pprz_stat.h"
28 
29 /*********
30  * Integer implementations
31  *********/
32 
39 int32_t mean_i(int32_t *array, uint32_t n_elements)
40 {
41  // determine the mean for the vector:
42  float sum = 0.f;
43  uint32_t i;
44  for (i = 0; i < n_elements; i++) {
45  sum += (float)array[i];
46  }
47 
48  return (int32_t)(sum / n_elements);
49 }
50 
60 int32_t variance_i(int32_t *array, uint32_t n_elements)
61 {
62  return (covariance_i(array, array, n_elements));
63 }
64 
74 int32_t covariance_i(int32_t *array1, int32_t *array2, uint32_t n_elements)
75 {
76  // Determine means for each vector:
77  float sumX = 0.f, sumY = 0.f, sumXY = 0.f;
78 
79  // Determine the covariance:
80  uint32_t i;
81  for (i = 0; i < n_elements; i++) {
82  sumX += (float)array1[i];
83  sumY += (float)array2[i];
84  sumXY += (float)(array1[i]) * (float)(array2[i]);
85  }
86 
87  return (int32_t)(sumXY / n_elements - sumX * sumY / (n_elements * n_elements));
88 }
89 
90 /*********
91  * Float implementations
92  *********/
93 
99 float mean_f(float *array, uint32_t n_elements)
100 {
101  // determine the mean for the vector:
102  float sum = 0.f;
103  uint32_t i;
104  for (i = 0; i < n_elements; i++) {
105  sum += array[i];
106  }
107 
108  return (sum / n_elements);
109 }
110 
119 float variance_f(float *array, uint32_t n_elements)
120 {
121  return covariance_f(array, array, n_elements);
122 }
123 
132 float covariance_f(float *arr1, float *arr2, uint32_t n_elements)
133 {
134  // Determine means for each vector:
135  float sumX = 0.f, sumY = 0.f, sumXY = 0.f;
136 
137  // Determine the covariance:
138  uint32_t i;
139  for (i = 0; i < n_elements; i++) {
140  sumX += arr1[i];
141  sumY += arr2[i];
142  sumXY += arr1[i] * arr2[i];
143  }
144 
145  return (sumXY / n_elements - sumX * sumY / (n_elements * n_elements));
146 }
int32_t variance_i(int32_t *array, uint32_t n_elements)
Compute the variance of an array of values (integer).
Definition: pprz_stat.c:60
float mean_f(float *array, uint32_t n_elements)
Compute the mean value of an array (float)
Definition: pprz_stat.c:99
unsigned long uint32_t
Definition: types.h:18
int32_t covariance_i(int32_t *array1, int32_t *array2, uint32_t n_elements)
Compute the covariance of two arrays V(X) = E[(X-E[X])(Y-E[Y])] = E[XY] - E[X]E[Y] where E[X] is the ...
Definition: pprz_stat.c:74
signed long int32_t
Definition: types.h:19
float covariance_f(float *arr1, float *arr2, uint32_t n_elements)
Compute the covariance of two arrays V(X) = E[(X-E[X])(Y-E[Y])] = E[XY] - E[X]E[Y] where E[X] is the ...
Definition: pprz_stat.c:132
float variance_f(float *array, uint32_t n_elements)
Compute the variance of an array of values (float).
Definition: pprz_stat.c:119
Statistics functions.
int32_t mean_i(int32_t *array, uint32_t n_elements)
Compute the mean value of an array This is implemented using floats to handle scaling of all variable...
Definition: pprz_stat.c:39