Moving Average

Dependents:   MovingAverage_HelloWorld Levitator EMG_Realtime_Filter EMG_Calibration ... more

Fork of MoyenneMobile by Alexandre Proulx

Committer:
fblanc
Date:
Tue Dec 16 08:27:15 2014 +0000
Revision:
4:ad69440a9bef
Parent:
1:b310d132db09
test ok

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fblanc 4:ad69440a9bef 1 /* Copyright (c) 2014 LAAS-CNRS
fblanc 4:ad69440a9bef 2 *
fblanc 4:ad69440a9bef 3 * Licensed under the Apache License, Version 2.0 (the "License");
fblanc 4:ad69440a9bef 4 * you may not use this file except in compliance with the License.
fblanc 4:ad69440a9bef 5 * You may obtain a copy of the License at
fblanc 4:ad69440a9bef 6 *
fblanc 4:ad69440a9bef 7 * http://www.apache.org/licenses/LICENSE-2.0
fblanc 4:ad69440a9bef 8 *
fblanc 4:ad69440a9bef 9 * Unless required by applicable law or agreed to in writing, software
fblanc 4:ad69440a9bef 10 * distributed under the License is distributed on an "AS IS" BASIS,
fblanc 4:ad69440a9bef 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
fblanc 4:ad69440a9bef 12 * See the License for the specific language governing permissions and
fblanc 4:ad69440a9bef 13 * limitations under the License.
fblanc 4:ad69440a9bef 14 */
fblanc 4:ad69440a9bef 15
Alegrowin 0:226202c7ea37 16 #ifndef MOVING_AVERAGE_H
Alegrowin 0:226202c7ea37 17 #define MOVING_AVERAGE_H
Alegrowin 0:226202c7ea37 18
Alegrowin 0:226202c7ea37 19 template <class T>
fblanc 4:ad69440a9bef 20 /** class of moving average
fblanc 4:ad69440a9bef 21 * Example:
fblanc 4:ad69440a9bef 22 * @code
fblanc 4:ad69440a9bef 23 * #include "mbed.h"
fblanc 4:ad69440a9bef 24 * #include "MovingAverage.h"
fblanc 4:ad69440a9bef 25 * #define NSAMPLE 100
fblanc 4:ad69440a9bef 26 * Ticker flipperADC;
fblanc 4:ad69440a9bef 27 * AnalogIn ain(A0);
fblanc 4:ad69440a9bef 28 * MovingAverage <float>vavg(NSAMPLE,0.0);
fblanc 4:ad69440a9bef 29 * void flipADC()
fblanc 4:ad69440a9bef 30 * {
fblanc 4:ad69440a9bef 31 * vavg.Insert(ain.read());
fblanc 4:ad69440a9bef 32 * }
fblanc 4:ad69440a9bef 33 * int main()
fblanc 4:ad69440a9bef 34 * {
fblanc 4:ad69440a9bef 35 * flipperADC.attach_us(&flipADC, 10000);
fblanc 4:ad69440a9bef 36 * while (true)
fblanc 4:ad69440a9bef 37 * printf("analog= %f \r\n",vavg.GetAverage());
fblanc 4:ad69440a9bef 38 * }
fblanc 4:ad69440a9bef 39 * @endcode
fblanc 4:ad69440a9bef 40 */
fblanc 4:ad69440a9bef 41
Alegrowin 0:226202c7ea37 42 class MovingAverage
Alegrowin 0:226202c7ea37 43 {
Alegrowin 0:226202c7ea37 44 private:
fblanc 1:b310d132db09 45 T* Element;
Alegrowin 0:226202c7ea37 46 T Average;
Alegrowin 0:226202c7ea37 47
Alegrowin 0:226202c7ea37 48 unsigned char NextElement;
Alegrowin 0:226202c7ea37 49 unsigned char MaxLength;
Alegrowin 0:226202c7ea37 50 public:
fblanc 4:ad69440a9bef 51 /** Create moving average
fblanc 4:ad69440a9bef 52 * @param maxLength is length of moving average
fblanc 4:ad69440a9bef 53 * @param value is initial value
fblanc 4:ad69440a9bef 54 */
Alegrowin 0:226202c7ea37 55 MovingAverage(unsigned char maxLength, T defaultValue);
fblanc 4:ad69440a9bef 56 /** Get the value of the moving average
fblanc 4:ad69440a9bef 57 * @return value of the moving average
fblanc 4:ad69440a9bef 58 */
Alegrowin 0:226202c7ea37 59 T GetAverage();
fblanc 4:ad69440a9bef 60 /** Insert a element in moving average
fblanc 4:ad69440a9bef 61 * @param value is the element inserted in moving average
fblanc 4:ad69440a9bef 62 */
fblanc 1:b310d132db09 63
Alegrowin 0:226202c7ea37 64 void Insert(T value);
Alegrowin 0:226202c7ea37 65 };
Alegrowin 0:226202c7ea37 66
fblanc 1:b310d132db09 67 template<class T>
fblanc 1:b310d132db09 68 MovingAverage<T>::MovingAverage(unsigned char maxLength, T defaultValue){
fblanc 1:b310d132db09 69 MaxLength = maxLength;
fblanc 1:b310d132db09 70
fblanc 1:b310d132db09 71 Element = new T[MaxLength];
fblanc 1:b310d132db09 72
fblanc 1:b310d132db09 73 for(int i = 0; i<MaxLength;i++)
fblanc 1:b310d132db09 74 {
fblanc 1:b310d132db09 75 Element[i] = defaultValue;
fblanc 1:b310d132db09 76 }
fblanc 1:b310d132db09 77 Average = defaultValue;
fblanc 1:b310d132db09 78 }
fblanc 1:b310d132db09 79
fblanc 1:b310d132db09 80 template<class T>
fblanc 1:b310d132db09 81 T MovingAverage<T>::GetAverage(){
fblanc 1:b310d132db09 82 return Average;
fblanc 1:b310d132db09 83 }
fblanc 1:b310d132db09 84
fblanc 1:b310d132db09 85
fblanc 1:b310d132db09 86 template<class T>
fblanc 1:b310d132db09 87 void MovingAverage<T>::Insert(T value){
fblanc 1:b310d132db09 88 Average = value/MaxLength + Average - Element[++NextElement]/MaxLength;
fblanc 1:b310d132db09 89 Element[NextElement] = value;
fblanc 1:b310d132db09 90 if(NextElement>=(MaxLength-1))
fblanc 1:b310d132db09 91 NextElement=0;
fblanc 1:b310d132db09 92
fblanc 1:b310d132db09 93 }
fblanc 4:ad69440a9bef 94 #endif //MOVING_AVERAGE_H