MovingAverage template
Fork of MoyenneMobile by
MovingAverage.h@5:3521837ec075, 2016-01-05 (annotated)
- Committer:
- fblanc
- Date:
- Tue Jan 05 09:48:13 2016 +0000
- Revision:
- 5:3521837ec075
- Parent:
- 4:0f6a72e290ad
ok
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fblanc | 4:0f6a72e290ad | 1 | /* Copyright (c) 2014 LAAS-CNRS |
fblanc | 4:0f6a72e290ad | 2 | * |
fblanc | 4:0f6a72e290ad | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
fblanc | 4:0f6a72e290ad | 4 | * you may not use this file except in compliance with the License. |
fblanc | 4:0f6a72e290ad | 5 | * You may obtain a copy of the License at |
fblanc | 4:0f6a72e290ad | 6 | * |
fblanc | 4:0f6a72e290ad | 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
fblanc | 4:0f6a72e290ad | 8 | * |
fblanc | 4:0f6a72e290ad | 9 | * Unless required by applicable law or agreed to in writing, software |
fblanc | 4:0f6a72e290ad | 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
fblanc | 4:0f6a72e290ad | 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
fblanc | 4:0f6a72e290ad | 12 | * See the License for the specific language governing permissions and |
fblanc | 4:0f6a72e290ad | 13 | * limitations under the License. |
fblanc | 4:0f6a72e290ad | 14 | */ |
fblanc | 4:0f6a72e290ad | 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:0f6a72e290ad | 20 | /** class of moving average |
fblanc | 4:0f6a72e290ad | 21 | * Example: |
fblanc | 4:0f6a72e290ad | 22 | * @code |
fblanc | 4:0f6a72e290ad | 23 | * #include "mbed.h" |
fblanc | 4:0f6a72e290ad | 24 | * #include "MovingAverage.h" |
fblanc | 4:0f6a72e290ad | 25 | * #define NSAMPLE 100 |
fblanc | 4:0f6a72e290ad | 26 | * Ticker flipperADC; |
fblanc | 4:0f6a72e290ad | 27 | * AnalogIn ain(A0); |
fblanc | 4:0f6a72e290ad | 28 | * MovingAverage <float>vavg(NSAMPLE,0.0); |
fblanc | 4:0f6a72e290ad | 29 | * void flipADC() |
fblanc | 4:0f6a72e290ad | 30 | * { |
fblanc | 4:0f6a72e290ad | 31 | * vavg.Insert(ain.read()); |
fblanc | 4:0f6a72e290ad | 32 | * } |
fblanc | 4:0f6a72e290ad | 33 | * int main() |
fblanc | 4:0f6a72e290ad | 34 | * { |
fblanc | 4:0f6a72e290ad | 35 | * flipperADC.attach_us(&flipADC, 10000); |
fblanc | 4:0f6a72e290ad | 36 | * while (true) |
fblanc | 4:0f6a72e290ad | 37 | * printf("analog= %f \r\n",vavg.GetAverage()); |
fblanc | 4:0f6a72e290ad | 38 | * } |
fblanc | 4:0f6a72e290ad | 39 | * @endcode |
fblanc | 4:0f6a72e290ad | 40 | */ |
fblanc | 4:0f6a72e290ad | 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:0f6a72e290ad | 51 | /** Create moving average |
fblanc | 4:0f6a72e290ad | 52 | * @param maxLength is length of moving average |
fblanc | 4:0f6a72e290ad | 53 | * @param value is initial value |
fblanc | 4:0f6a72e290ad | 54 | */ |
Alegrowin | 0:226202c7ea37 | 55 | MovingAverage(unsigned char maxLength, T defaultValue); |
fblanc | 4:0f6a72e290ad | 56 | /** Get the value of the moving average |
fblanc | 4:0f6a72e290ad | 57 | * @return value of the moving average |
fblanc | 4:0f6a72e290ad | 58 | */ |
Alegrowin | 0:226202c7ea37 | 59 | T GetAverage(); |
fblanc | 4:0f6a72e290ad | 60 | /** Insert a element in moving average |
fblanc | 4:0f6a72e290ad | 61 | * @param value is the element inserted in moving average |
fblanc | 4:0f6a72e290ad | 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 | 5:3521837ec075 | 88 | |
fblanc | 5:3521837ec075 | 89 | Average = value/MaxLength + Average - Element[++NextElement]/MaxLength; |
fblanc | 1:b310d132db09 | 90 | Element[NextElement] = value; |
fblanc | 1:b310d132db09 | 91 | if(NextElement>=(MaxLength-1)) |
fblanc | 1:b310d132db09 | 92 | NextElement=0; |
fblanc | 5:3521837ec075 | 93 | |
fblanc | 1:b310d132db09 | 94 | } |
fblanc | 5:3521837ec075 | 95 | #endif //MOVING_AVERAGE_H |