Compute statistics for data entered in rreal-time.
More...
#include <assert.h>
#include <math.h>
#include <stdio.h>
|
void | stats_computer1 (float x, float *mean, float *variance, float *std) |
| continuous mean and variance computance using first value as an approximation for the mean. More...
|
|
void | stats_computer2 (float x, float *mean, float *variance, float *std) |
| continuous mean and variance computance using Welford's algorithm (very accurate) More...
|
|
void | test_function (const float *test_data, const int number_of_samples) |
| Test the algorithm implementation. More...
|
|
int | main (int argc, char **argv) |
| Main function. More...
|
|
Compute statistics for data entered in rreal-time.
- Author
- Krishna Vedala
This algorithm is really beneficial to compute statistics on data read in realtime. For example, devices reading biometrics data. The algorithm is simple enough to be easily implemented in an embedded system.
◆ main()
int main |
( |
int |
argc, |
|
|
char ** |
argv |
|
) |
| |
Main function.
130 const float test_data1[] = {3, 4, 5, -1.4, -3.6, 1.9, 1.};
131 test_function(test_data1,
sizeof(test_data1) /
sizeof(test_data1[0]));
133 float s1_mean = 0.f, s1_variance = 0.f, s1_std = 0.f;
134 float s2_mean = 0.f, s2_variance = 0.f, s2_std = 0.f;
136 printf(
"Enter data. Any non-numeric data will terminate the data input.\n");
141 printf(
"Enter number: ");
145 if (!scanf(
"%f", &val))
151 printf(
"\tMethod 1:\tMean: %.4f\t Variance: %.4f\t Std: %.4f\n",
152 s1_mean, s1_variance, s1_std);
153 printf(
"\tMethod 2:\tMean: %.4f\t Variance: %.4f\t Std: %.4f\n",
154 s2_mean, s2_variance, s2_std);
void stats_computer2(float x, float *mean, float *variance, float *std)
continuous mean and variance computance using Welford's algorithm (very accurate)
Definition: realtime_stats.c:61
void stats_computer1(float x, float *mean, float *variance, float *std)
continuous mean and variance computance using first value as an approximation for the mean.
Definition: realtime_stats.c:24
void test_function(const float *test_data, const int number_of_samples)
Test the algorithm implementation.
Definition: realtime_stats.c:92
◆ stats_computer1()
void stats_computer1 |
( |
float |
x, |
|
|
float * |
mean, |
|
|
float * |
variance, |
|
|
float * |
std |
|
) |
| |
continuous mean and variance computance using first value as an approximation for the mean.
If the first number is much far form the mean, the algorithm becomes very inaccurate to compute variance and standard deviation.
- Parameters
-
[in] | x | new value added to data set |
[out] | mean | if not NULL, mean returns mean of data set |
[out] | variance | if not NULL, mean returns variance of data set |
[out] | std | if not NULL, mean returns standard deviation of data set |
29 static unsigned int n = 0;
30 static float Ex = 0.f, Ex2 = 0.f;
46 *variance = (Ex2 - (Ex * Ex) / n) / (n - 1);
50 *std = sqrtf(*variance);
◆ stats_computer2()
void stats_computer2 |
( |
float |
x, |
|
|
float * |
mean, |
|
|
float * |
variance, |
|
|
float * |
std |
|
) |
| |
continuous mean and variance computance using Welford's algorithm (very accurate)
- Parameters
-
[in] | x | new value added to data set |
[out] | mean | if not NULL, mean returns mean of data set |
[out] | variance | if not NULL, mean returns variance of data set |
[out] | std | if not NULL, mean returns standard deviation of data set |
66 static unsigned int n = 0;
67 static float mu = 0, M = 0;
72 float delta2 = x - mu;
85 *std = sqrtf(*variance);
◆ test_function()
void test_function |
( |
const float * |
test_data, |
|
|
const int |
number_of_samples |
|
) |
| |
Test the algorithm implementation.
- Parameters
-
[in] | test_data | array of data to test the algorithms |
[in] | number_of_samples | number of samples of data |
94 float ref_mean = 0.f, ref_variance = 0.f;
95 float s1_mean = 0.f, s1_variance = 0.f, s1_std = 0.f;
96 float s2_mean = 0.f, s2_variance = 0.f, s2_std = 0.f;
98 for (
int i = 0; i < number_of_samples; i++)
102 ref_mean += test_data[i];
104 ref_mean /= number_of_samples;
106 for (
int i = 0; i < number_of_samples; i++)
108 float temp = test_data[i] - ref_mean;
109 ref_variance += temp * temp;
111 ref_variance /= number_of_samples;
113 printf(
"<<<<<<<< Test Function >>>>>>>>\n");
114 printf(
"Expected: Mean: %.4f\t Variance: %.4f\n", ref_mean, ref_variance);
115 printf(
"\tMethod 1:\tMean: %.4f\t Variance: %.4f\t Std: %.4f\n", s1_mean,
116 s1_variance, s1_std);
117 printf(
"\tMethod 2:\tMean: %.4f\t Variance: %.4f\t Std: %.4f\n", s2_mean,
118 s2_variance, s2_std);
120 assert(fabs(s1_mean - ref_mean) < 0.01);
121 assert(fabs(s2_mean - ref_mean) < 0.01);
122 assert(fabs(s2_variance - ref_variance) < 0.01);
124 printf(
"(Tests passed)\n\n");