Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
realtime_stats.c File Reference

Compute statistics for data entered in rreal-time. More...

#include <assert.h>
#include <math.h>
#include <stdio.h>
Include dependency graph for realtime_stats.c:

Functions

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...
 

Detailed Description

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.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Main function.

129{
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]));
132
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;
135
136 printf("Enter data. Any non-numeric data will terminate the data input.\n");
137
138 while (1)
139 {
140 float val;
141 printf("Enter number: ");
142
143 // check for failure to read input. Happens for
144 // non-numeric data
145 if (!scanf("%f", &val))
146 break;
147
148 stats_computer1(val, &s1_mean, &s1_variance, &s1_std);
149 stats_computer2(val, &s2_mean, &s2_variance, &s2_std);
150
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);
155 }
156
157 return 0;
158}
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
Here is the call graph for this function:

◆ 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]xnew value added to data set
[out]meanif not NULL, mean returns mean of data set
[out]varianceif not NULL, mean returns variance of data set
[out]stdif not NULL, mean returns standard deviation of data set
25{
26 /* following variables declared static becuase they need to be remembered
27 * when updating for next sample, when received.
28 */
29 static unsigned int n = 0;
30 static float Ex = 0.f, Ex2 = 0.f;
31 static float K = 0.f;
32
33 if (n == 0)
34 K = x;
35 n++;
36 float tmp = x - K;
37 Ex += tmp;
38 Ex2 += tmp * tmp;
39
40 /* return sample mean computed till last sample */
41 if (mean != NULL)
42 *mean = K + Ex / n;
43
44 /* return data variance computed till last sample */
45 if (variance != NULL)
46 *variance = (Ex2 - (Ex * Ex) / n) / (n - 1);
47
48 /* return sample standard deviation computed till last sample */
49 if (std != NULL)
50 *std = sqrtf(*variance);
51}

◆ 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]xnew value added to data set
[out]meanif not NULL, mean returns mean of data set
[out]varianceif not NULL, mean returns variance of data set
[out]stdif not NULL, mean returns standard deviation of data set
62{
63 /* following variables declared static becuase they need to be remembered
64 * when updating for next sample, when received.
65 */
66 static unsigned int n = 0;
67 static float mu = 0, M = 0;
68
69 n++;
70 float delta = x - mu;
71 mu += delta / n;
72 float delta2 = x - mu;
73 M += delta * delta2;
74
75 /* return sample mean computed till last sample */
76 if (mean != NULL)
77 *mean = mu;
78
79 /* return data variance computed till last sample */
80 if (variance != NULL)
81 *variance = M / n;
82
83 /* return sample standard deviation computed till last sample */
84 if (std != NULL)
85 *std = sqrtf(*variance);
86}

◆ test_function()

void test_function ( const float *  test_data,
const int  number_of_samples 
)

Test the algorithm implementation.

Parameters
[in]test_dataarray of data to test the algorithms
[in]number_of_samplesnumber of samples of data
93{
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;
97
98 for (int i = 0; i < number_of_samples; i++)
99 {
100 stats_computer1(test_data[i], &s1_mean, &s1_variance, &s1_std);
101 stats_computer2(test_data[i], &s2_mean, &s2_variance, &s2_std);
102 ref_mean += test_data[i];
103 }
104 ref_mean /= number_of_samples;
105
106 for (int i = 0; i < number_of_samples; i++)
107 {
108 float temp = test_data[i] - ref_mean;
109 ref_variance += temp * temp;
110 }
111 ref_variance /= number_of_samples;
112
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);
119
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);
123
124 printf("(Tests passed)\n\n");
125}
Here is the call graph for this function: