The moving average purpose is to filter some data that comes from a source that may be unprecise. See more information about Moving Average here: http://en.wikipedia.org/wiki/Moving_average

MovingAverage.cpp

Committer:
Alegrowin
Date:
2013-04-14
Revision:
0:226202c7ea37

File content as of revision 0:226202c7ea37:

/*****************************************************/
/*           _                              _        */
/*     /\   | |                            (_)       */
/*    /  \  | | ___  __ _ _ __ _____      ___ _ __   */
/*   / /\ \ | |/ _ \/ _` | '__/ _ \ \ /\ / / | '_ \  */
/*  / ____ \| |  __/ (_| | | | (_) \ V  V /| | | | | */
/* /_/    \_\_|\___|\__, |_|  \___/ \_/\_/ |_|_| |_| */
/*                   __/ |                           */
/*                  |___/                            */
/*                          Created on April 2013    */
/*****************************************************/


/*

for(int i = 0; i < FILTRE_RANGE; i++) {
        FiltreMobile[i] = cmd[0];
    }
    
    Temp = Temp - FiltreMobile[Temp_next]/FILTRE_RANGE + cmd[0]/FILTRE_RANGE;
    FiltreMobile[Temp_next++] = cmd[0];

    Temp_next = Temp_next&0x07;
}*/
#include "MovingAverage.h"

template<class T>
MovingAverage<T>::MovingAverage(unsigned char maxLength, T defaultValue){
    MaxLength = maxLength;
    
    AverageFilter = new T[MaxLength];
    
    for(int i = 0; i<MaxLength;i++)
    {
        AverageFilter[i] = defaultValue;
    }
    Average = defaultValue;
}

template<class T>
T MovingAverage<T>::GetAverage(){
    return Average;
}
template<class T>
void MovingAverage<T>::Insert(T value){
    
    Average = Average - AverageFilter[NextElement]/MaxLength + value/MaxLength;
    FiltreMobile[NextElement++] = value;

    NextElement = NextElement&MaxLength;
}