#ifndef MOVINGSTAT_H #define MOVINGSTAT_H #include class MovingStat { /** @short approximated moving average structure */ public: /** constructor \param nn number of samples parameter to be used */ MovingStat(int nn=1000) : n(nn), m_n(0) {} /** clears the moving average number of samples parameter, mean and standard deviation */ void Clear() { m_n = 0; m_newM=0; m_newM2=0; } /** clears the moving average number of samples parameter, mean and standard deviation */ void Set(double val, double rms=0, int m=-1) { if (m>=0) m_n = m; else m_n = n; m_newM=val*m_n; SetRMS(rms); } /** clears the moving average number of samples parameter, mean and standard deviation */ void SetRMS(double rms) { if (rms<=0) { m_newM2=m_newM*m_newM/n; m_n=0; } else { if (m_n>0) m_newM2=(m_n*rms*rms+m_newM*m_newM/m_n); else { m_newM2=(m_n*rms*rms+m_newM*m_newM/n); m_n=0; } } } /** sets number of samples parameter \param i number of samples parameter to be set */ int SetN(int i) {if (i>=1) n=i; return n;}; /** gets number of samples parameter \returns actual number of samples parameter */ int GetN() {return m_n;}; /** calculates the moving average i.e. adds if number of elements is lower than number of samples parameter, pushes otherwise \param x value to calculate the moving average */ inline void Calc(double x) { if (m_n 0) ? m_newM/m_n : 0.0; } /** returns the squared mean, 0 if no elements are inside \returns returns the squared average */ double M2() const { return ( (m_n > 1) ? m_newM2/m_n : 0.0 ); } /** returns the variance, 0 if no elements are inside \returns returns the variance */ inline double Variance() const { return ( (m_n > 1) ? (M2()-Mean()*Mean()) : 0.0 ); } /** returns the standard deviation, 0 if no elements are inside \returns returns the standard deviation */ inline double StandardDeviation() const { return ( (Variance() > 0) ? sqrt( Variance() ) : -1 ); } private: int n; /**< number of samples parameter */ int m_n; /**< current number of elements */ double m_newM; /**< accumulated average */ double m_newM2; /**< accumulated squared average */ }; #endif