Moving Average

Dependents:   MovingAverage_HelloWorld Levitator EMG_Realtime_Filter EMG_Calibration ... more

Fork of MoyenneMobile by Alexandre Proulx

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MovingAverage.h Source File

MovingAverage.h

00001  /* Copyright (c) 2014 LAAS-CNRS
00002  *
00003  * Licensed under the Apache License, Version 2.0 (the "License");
00004  * you may not use this file except in compliance with the License.
00005  * You may obtain a copy of the License at
00006  *
00007  *     http://www.apache.org/licenses/LICENSE-2.0
00008  *
00009  * Unless required by applicable law or agreed to in writing, software
00010  * distributed under the License is distributed on an "AS IS" BASIS,
00011  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012  * See the License for the specific language governing permissions and
00013  * limitations under the License.
00014  */
00015  
00016 #ifndef MOVING_AVERAGE_H
00017 #define MOVING_AVERAGE_H
00018 
00019 template <class T>
00020 /** class of moving average
00021 * Example:
00022  * @code
00023  * #include "mbed.h"
00024  * #include "MovingAverage.h"
00025  * #define NSAMPLE 100
00026  * Ticker flipperADC;
00027  * AnalogIn   ain(A0);
00028  * MovingAverage <float>vavg(NSAMPLE,0.0);
00029  * void flipADC()
00030  * {
00031  *     vavg.Insert(ain.read());
00032  * }
00033  * int main()
00034  * { 
00035  *   flipperADC.attach_us(&flipADC, 10000);
00036  *   while (true) 
00037  *      printf("analog= %f \r\n",vavg.GetAverage()); 
00038  * }
00039  * @endcode
00040  */
00041  
00042 class MovingAverage
00043 {
00044 private:
00045     T* Element;
00046     T Average;
00047     
00048     unsigned char NextElement;
00049     unsigned char MaxLength;
00050 public:
00051     /** Create  moving average
00052      * @param maxLength is length of moving average
00053      * @param value is initial value
00054      */
00055     MovingAverage(unsigned char maxLength, T defaultValue);
00056     /** Get the value of the moving average
00057      * @return value of the moving average
00058      */
00059     T GetAverage();
00060     /** Insert a element in moving average
00061      * @param value is the element inserted in moving average
00062      */
00063 
00064     void Insert(T value);
00065 };
00066 
00067 template<class T>
00068 MovingAverage<T>::MovingAverage(unsigned char maxLength, T defaultValue){
00069     MaxLength = maxLength;
00070     
00071     Element = new T[MaxLength];
00072     
00073     for(int i = 0; i<MaxLength;i++)
00074     {
00075         Element[i] = defaultValue;
00076     }
00077     Average = defaultValue;
00078 }
00079 
00080 template<class T>
00081 T MovingAverage<T>::GetAverage(){
00082     return Average;
00083 }
00084 
00085 
00086 template<class T>
00087 void MovingAverage<T>::Insert(T value){
00088     Average = value/MaxLength + Average - Element[++NextElement]/MaxLength;
00089     Element[NextElement] = value;
00090     if(NextElement>=(MaxLength-1))   
00091         NextElement=0;
00092     
00093 }
00094 #endif //MOVING_AVERAGE_H