statistics.h
Go to the documentation of this file.
00001 #ifndef STATISTICS_H
00002 #define STATISTICS_H
00003 
00004 #include <math.h>
00005 #include <stdio.h>
00006 #include <string.h>
00007 
00009 template <class T>
00010 class Statistics
00011 {
00012    public:
00013       Statistics(const char* name = "");
00014       
00015       void reset();
00016       void addMeasurement(T val);
00017 
00018       T getMean() const;
00019       T getStandardDeviation() const;
00020       int getNumMeasurements() const;
00021 
00022       T getMin() const { return _min; }
00023       T getMax() const { return _max; }
00024 
00025       void print(const double factor=1.0) const;
00026 
00027    protected:
00028       T _sumMeasurements;
00029       T _sumSquaredMeasurements;
00030       double _numMeasurements;
00031 
00032       T _min;
00033       T _max;
00034 
00035       const char* _name;
00036 };
00037 
00038 template <class T>
00039 Statistics<T>::Statistics(const char* name) : _name(name)
00040 {
00041    reset();
00042 }
00043       
00044 template <class T>
00045 void Statistics<T>::reset()
00046 {
00047    _sumMeasurements = static_cast<T>(0);
00048    _sumSquaredMeasurements = static_cast<T>(0);
00049    _numMeasurements = 0;
00050    _min = HUGE_VAL;     // this probably only works for doubles
00051    _max = -HUGE_VAL;
00052 }
00053 
00054 template <class T>
00055 void Statistics<T>::addMeasurement(T val)
00056 {
00057    _sumMeasurements += val;
00058    _sumSquaredMeasurements += (val * val);
00059    _numMeasurements += 1.0;
00060    if(val > _max)
00061       _max = val;
00062    if(val < _min)
00063       _min = val;
00064 }
00065 
00066 template <class T>
00067 T Statistics<T>::getMean() const
00068 {
00069    if(_numMeasurements == 0)
00070       return static_cast<T>(0);
00071    return _sumMeasurements/_numMeasurements;
00072 }
00073 
00074 template <class T>
00075 T Statistics<T>::getStandardDeviation() const
00076 {
00077    if(_numMeasurements < 2)
00078       return static_cast<T>(0);
00079 
00080    return sqrt( (_numMeasurements * _sumSquaredMeasurements - _sumMeasurements * _sumMeasurements) / (_numMeasurements * (_numMeasurements - 1)) );
00081 }
00082 
00083 template <class T>
00084 int Statistics<T>::getNumMeasurements() const
00085 {
00086    return (int)_numMeasurements;
00087 }
00088 
00089 template <class T>
00090 void Statistics<T>::print(const double factor) const
00091 {
00092     if(strlen(_name) == 0)
00093         printf("Mean %.2f Std: %.2f (Num: %.0f)\n", getMean()*factor, getStandardDeviation()*factor, _numMeasurements);
00094     else
00095         printf("%s: Mean %.2f Std: %.2f (Num: %.0f)\n", _name, getMean()*factor, getStandardDeviation()*factor, _numMeasurements);
00096 }
00097 
00098 #endif
00099 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


tfd_modules
Author(s): Maintained by Christian Dornhege (see AUTHORS file).
autogenerated on Tue Jan 22 2013 12:25:03