Moving Average
Dependents: MovingAverage_HelloWorld Levitator EMG_Realtime_Filter EMG_Calibration ... more
Fork of MoyenneMobile by
MovingAverage.h
- Committer:
- fblanc
- Date:
- 2014-07-07
- Revision:
- 1:b310d132db09
- Parent:
- 0:226202c7ea37
- Child:
- 4:ad69440a9bef
File content as of revision 1:b310d132db09:
#ifndef MOVING_AVERAGE_H #define MOVING_AVERAGE_H template <class T> class MovingAverage { private: T* Element; T Average; unsigned char NextElement; unsigned char MaxLength; public: MovingAverage(unsigned char maxLength, T defaultValue); T GetAverage(); void Insert(T value); }; template<class T> MovingAverage<T>::MovingAverage(unsigned char maxLength, T defaultValue){ MaxLength = maxLength; Element = new T[MaxLength]; for(int i = 0; i<MaxLength;i++) { Element[i] = defaultValue; } Average = defaultValue; } template<class T> T MovingAverage<T>::GetAverage(){ return Average; } template<class T> void MovingAverage<T>::Insert(T value){ Average = value/MaxLength + Average - Element[++NextElement]/MaxLength; Element[NextElement] = value; if(NextElement>=(MaxLength-1)) NextElement=0; } #endif