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:

## Classes

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

## Namespaces

| 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

## Function Documentation

## main()

int main | ( | int | argc,
|  | char ** | argv
| ) |  |

Main function

{
const float test_data1[] = {3, 4, 5, -1.4, -3.6, 1.9, 1.};
test_function(test_data1, sizeof(test_data1) / sizeof(test_data1[0]));
std::cout
<< "Enter data. Any non-numeric data will terminate the data input."
<< std::endl;
stats_computer1<float> stats1;
stats_computer2<float> stats2;
while (1) {
double val;
std::cout << "Enter number: ";
std::cin >> val;
// check for failure to read input. Happens for
// non-numeric data
if (std::cin.fail())
break;
stats1.new_val(val);
stats2.new_val(val);
std::cout << "\tMethod 1:"
<< "\tMean: " << stats1.mean()
<< "\t Variance: " << stats1.variance()
<< "\t Std: " << stats1.std() << std::endl;
std::cout << "\tMethod 2:"
<< "\tMean: " << stats2.mean()
<< "\t Variance: " << stats2.variance()
<< "\t Std: " << stats2.std() << std::endl;
}
return 0;
}
std::cin
std::cout
statistics::stats_computer1
Definition: realtime_stats.cpp:27
statistics::stats_computer1::variance
double variance() const
Definition: realtime_stats.cpp:45
statistics::stats_computer1::mean
double mean() const
Definition: realtime_stats.cpp:42
statistics::stats_computer1::new_val
void new_val(T x)
Definition: realtime_stats.cpp:32
statistics::stats_computer1::std
double std() const
Definition: realtime_stats.cpp:48
statistics::stats_computer2
Definition: realtime_stats.cpp:72
statistics::stats_computer2::mean
double mean() const
Definition: realtime_stats.cpp:86
statistics::stats_computer2::std
double std() const
Definition: realtime_stats.cpp:92
statistics::stats_computer2::new_val
void new_val(T x)
Definition: realtime_stats.cpp:77
statistics::stats_computer2::variance
double variance() const
Definition: realtime_stats.cpp:89
std::endl
T endl(T... args)
test_function
void test_function(const float *test_data, const int number_of_samples)
Definition: realtime_stats.cpp:118
Here is the call graph for this function:

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

{
float mean = 0.f, variance = 0.f;
stats_computer1<float> stats01;
stats_computer2<float> stats02;
for (int i = 0; i < number_of_samples; i++) {
stats01.new_val(test_data[i]);
stats02.new_val(test_data[i]);
mean += test_data[i];
}
mean /= number_of_samples;
for (int i = 0; i < number_of_samples; i++) {
float temp = test_data[i] - mean;
variance += temp * temp;
}
variance /= number_of_samples;
std::cout << "<<<<<<<< Test Function >>>>>>>>" << std::endl
<< "Expected: Mean: " << mean << "\t Variance: " << variance
<< std::endl;
std::cout << "\tMethod 1:"
<< "\tMean: " << stats01.mean()
<< "\t Variance: " << stats01.variance()
<< "\t Std: " << stats01.std() << std::endl;
std::cout << "\tMethod 2:"
<< "\tMean: " << stats02.mean()
<< "\t Variance: " << stats02.variance()
<< "\t Std: " << stats02.std() << std::endl;
assert(std::abs(stats01.mean() - mean) < 0.01);
assert(std::abs(stats02.mean() - mean) < 0.01);
assert(std::abs(stats02.variance() - variance) < 0.01);
std::cout << "(Tests passed)" << std::endl;
}
Here is the call graph for this function: