Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
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 
40 {
41 
42  if (n_elements == 0) {
43  // Note that something else is wrong if you want the mean of 0 samples.
44  return 0;
45  }
46  // determine the mean for the vector:
47  float sum = 0.f;
48  uint32_t i;
49  for (i = 0; i < n_elements; i++) {
50  sum += (float)array[i];
51  }
52  return (int32_t)(sum / n_elements);
53 }
54 
65 {
66  return (covariance_i(array, array, n_elements));
67 }
68 
78 int32_t covariance_i(int32_t *array1, int32_t *array2, uint32_t n_elements)
79 {
80  if (n_elements == 0) {
81  // Note that something else is wrong if you want the covariance of 0 samples.
82  return 0;
83  }
84  // Determine means for each vector:
85  float sumX = 0.f, sumY = 0.f, sumXY = 0.f;
86 
87  // Determine the covariance:
88  uint32_t i;
89  for (i = 0; i < n_elements; i++) {
90  sumX += (float)array1[i];
91  sumY += (float)array2[i];
92  sumXY += (float)(array1[i]) * (float)(array2[i]);
93  }
94  return (int32_t)(sumXY / n_elements - sumX * sumY / (n_elements * n_elements));
95 }
96 
97 /*********
98  * Float implementations
99  *********/
100 
106 float sum_f(float *array, uint32_t n_elements)
107 {
108  // determine the mean for the vector:
109  float sum = 0.f;
110  uint32_t i;
111  for (i = 0; i < n_elements; i++) {
112  sum += array[i];
113  }
114  return sum;
115 }
116 
122 float mean_f(float *array, uint32_t n_elements)
123 {
124  if (n_elements == 0) {
125  // Note that something else is wrong if you want the mean of 0 samples.
126  return 0.f;
127  }
128  return (sum_f(array, n_elements) / n_elements);
129 }
130 
139 float variance_f(float *array, uint32_t n_elements)
140 {
141  return covariance_f(array, array, n_elements);
142 }
143 
152 float covariance_f(float *arr1, float *arr2, uint32_t n_elements)
153 {
154  if (n_elements == 0) {
155  // Note that something else is wrong if you want the covariance of 0 samples.
156  return 0.f;
157  }
158  // Determine means for each vector:
159  float sumX = 0.f, sumY = 0.f, sumXY = 0.f;
160 
161  // Determine the covariance:
162  uint32_t i;
163  for (i = 0; i < n_elements; i++) {
164  sumX += arr1[i];
165  sumY += arr2[i];
166  sumXY += arr1[i] * arr2[i];
167  }
168  return (sumXY / n_elements - sumX * sumY / (n_elements * n_elements));
169 }
variance_i
int32_t variance_i(int32_t *array, uint32_t n_elements)
Compute the variance of an array of values (integer).
Definition: pprz_stat.c:64
uint32_t
unsigned long uint32_t
Definition: types.h:18
array
int * array
Definition: snake_gate_detection.c:94
covariance_i
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:78
sum_f
float sum_f(float *array, uint32_t n_elements)
Compute the sum array elements (float)
Definition: pprz_stat.c:106
mean_i
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
pprz_stat.h
Statistics functions.
covariance_f
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:152
mean_f
float mean_f(float *array, uint32_t n_elements)
Compute the mean value of an array (float)
Definition: pprz_stat.c:122
variance_f
float variance_f(float *array, uint32_t n_elements)
Compute the variance of an array of values (float).
Definition: pprz_stat.c:139
int32_t
signed long int32_t
Definition: types.h:19