TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
realtime_stats.cpp File Reference

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

#include <cassert>
#include <cmath>
#include <iostream>
Include dependency graph for realtime_stats.cpp:

Go to the source code of this file.

Classes

class  statistics::stats_computer1< T >
 
class  statistics::stats_computer2< T >
 

Namespaces

namespace  statistics
 Statistical algorithms.
 

Functions

void test_function (const float *test_data, const int number_of_samples)
 
int main (int argc, char **argv)
 

Detailed Description

Compute statistics for data entered in rreal-time.

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.

Author
Krishna Vedala

Definition in file realtime_stats.cpp.

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Main function

Definition at line 158 of file realtime_stats.cpp.

158 {
159 const float test_data1[] = {3, 4, 5, -1.4, -3.6, 1.9, 1.};
160 test_function(test_data1, sizeof(test_data1) / sizeof(test_data1[0]));
161
162 std::cout
163 << "Enter data. Any non-numeric data will terminate the data input."
164 << std::endl;
165
168
169 while (1) {
170 double val;
171 std::cout << "Enter number: ";
172 std::cin >> val;
173
174 // check for failure to read input. Happens for
175 // non-numeric data
176 if (std::cin.fail())
177 break;
178
179 stats1.new_val(val);
180 stats2.new_val(val);
181
182 std::cout << "\tMethod 1:"
183 << "\tMean: " << stats1.mean()
184 << "\t Variance: " << stats1.variance()
185 << "\t Std: " << stats1.std() << std::endl;
186 std::cout << "\tMethod 2:"
187 << "\tMean: " << stats2.mean()
188 << "\t Variance: " << stats2.variance()
189 << "\t Std: " << stats2.std() << std::endl;
190 }
191
192 return 0;
193}
void test_function(const float *test_data, const int number_of_samples)

◆ 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

Definition at line 118 of file realtime_stats.cpp.

118 {
119 float mean = 0.f, variance = 0.f;
120
123
124 for (int i = 0; i < number_of_samples; i++) {
125 stats01.new_val(test_data[i]);
126 stats02.new_val(test_data[i]);
127 mean += test_data[i];
128 }
129
130 mean /= number_of_samples;
131
132 for (int i = 0; i < number_of_samples; i++) {
133 float temp = test_data[i] - mean;
134 variance += temp * temp;
135 }
136 variance /= number_of_samples;
137
138 std::cout << "<<<<<<<< Test Function >>>>>>>>" << std::endl
139 << "Expected: Mean: " << mean << "\t Variance: " << variance
140 << std::endl;
141 std::cout << "\tMethod 1:"
142 << "\tMean: " << stats01.mean()
143 << "\t Variance: " << stats01.variance()
144 << "\t Std: " << stats01.std() << std::endl;
145 std::cout << "\tMethod 2:"
146 << "\tMean: " << stats02.mean()
147 << "\t Variance: " << stats02.variance()
148 << "\t Std: " << stats02.std() << std::endl;
149
150 assert(std::abs(stats01.mean() - mean) < 0.01);
151 assert(std::abs(stats02.mean() - mean) < 0.01);
152 assert(std::abs(stats02.variance() - variance) < 0.01);
153
154 std::cout << "(Tests passed)" << std::endl;
155}