A 10-Point moving average filter that returns a float each time a new value is added to the moving average array.
Fork of MovingAverageFilter by
MAF.h@1:fbc57eb4e61d, 2013-05-05 (annotated)
- Committer:
- KarimAzzouz
- Date:
- Sun May 05 13:31:02 2013 +0000
- Revision:
- 1:fbc57eb4e61d
- Parent:
- 0:c1b48befe066
- Child:
- 2:21b70641f866
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
KarimAzzouz | 1:fbc57eb4e61d | 1 | /* Copyright (c) <2012> MIT License |
KarimAzzouz | 1:fbc57eb4e61d | 2 | * |
KarimAzzouz | 1:fbc57eb4e61d | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
KarimAzzouz | 1:fbc57eb4e61d | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
KarimAzzouz | 1:fbc57eb4e61d | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
KarimAzzouz | 1:fbc57eb4e61d | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
KarimAzzouz | 1:fbc57eb4e61d | 7 | * furnished to do so, subject to the following conditions: |
KarimAzzouz | 1:fbc57eb4e61d | 8 | * |
KarimAzzouz | 1:fbc57eb4e61d | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
KarimAzzouz | 1:fbc57eb4e61d | 10 | * substantial portions of the Software. |
KarimAzzouz | 1:fbc57eb4e61d | 11 | * |
KarimAzzouz | 1:fbc57eb4e61d | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
KarimAzzouz | 1:fbc57eb4e61d | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
KarimAzzouz | 1:fbc57eb4e61d | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
KarimAzzouz | 1:fbc57eb4e61d | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
KarimAzzouz | 1:fbc57eb4e61d | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
KarimAzzouz | 1:fbc57eb4e61d | 17 | * |
KarimAzzouz | 1:fbc57eb4e61d | 18 | * @section DESCRIPTION |
KarimAzzouz | 1:fbc57eb4e61d | 19 | * |
KarimAzzouz | 1:fbc57eb4e61d | 20 | * Moving Average Filter |
KarimAzzouz | 1:fbc57eb4e61d | 21 | * |
KarimAzzouz | 1:fbc57eb4e61d | 22 | * |
KarimAzzouz | 1:fbc57eb4e61d | 23 | */ |
KarimAzzouz | 1:fbc57eb4e61d | 24 | |
KarimAzzouz | 0:c1b48befe066 | 25 | #ifndef MAF_H |
KarimAzzouz | 0:c1b48befe066 | 26 | #define MAF_H |
KarimAzzouz | 0:c1b48befe066 | 27 | |
KarimAzzouz | 0:c1b48befe066 | 28 | #include "mbed.h" |
KarimAzzouz | 0:c1b48befe066 | 29 | |
KarimAzzouz | 1:fbc57eb4e61d | 30 | /** 10-Point Moving Average filter |
KarimAzzouz | 1:fbc57eb4e61d | 31 | * |
KarimAzzouz | 1:fbc57eb4e61d | 32 | * Example: |
KarimAzzouz | 1:fbc57eb4e61d | 33 | * @code |
KarimAzzouz | 1:fbc57eb4e61d | 34 | * // Update the filter with gyroscope readings |
KarimAzzouz | 1:fbc57eb4e61d | 35 | * #include "mbed.h" |
KarimAzzouz | 1:fbc57eb4e61d | 36 | * #include "ITG3200.h" |
KarimAzzouz | 1:fbc57eb4e61d | 37 | * #include "MAF.h" |
KarimAzzouz | 1:fbc57eb4e61d | 38 | * |
KarimAzzouz | 1:fbc57eb4e61d | 39 | * Serial pc(USBTX, USBRX); |
KarimAzzouz | 1:fbc57eb4e61d | 40 | * ITG3200 gyro(p9, p10); |
KarimAzzouz | 1:fbc57eb4e61d | 41 | * MAF Filter(); |
KarimAzzouz | 1:fbc57eb4e61d | 42 | * int Average; |
KarimAzzouz | 1:fbc57eb4e61d | 43 | * |
KarimAzzouz | 1:fbc57eb4e61d | 44 | * int main() { |
KarimAzzouz | 1:fbc57eb4e61d | 45 | * |
KarimAzzouz | 1:fbc57eb4e61d | 46 | * gyro.setLpBandwidth(LPFBW_42HZ); |
KarimAzzouz | 1:fbc57eb4e61d | 47 | * |
KarimAzzouz | 1:fbc57eb4e61d | 48 | * while(1){ |
KarimAzzouz | 1:fbc57eb4e61d | 49 | * |
KarimAzzouz | 1:fbc57eb4e61d | 50 | * Average = Filter.updateInt(gyro.getGyroX()); |
KarimAzzouz | 1:fbc57eb4e61d | 51 | * pc.printf("%i\n",Average); |
KarimAzzouz | 1:fbc57eb4e61d | 52 | * wait_ms(50); |
KarimAzzouz | 1:fbc57eb4e61d | 53 | * } |
KarimAzzouz | 1:fbc57eb4e61d | 54 | * } |
KarimAzzouz | 1:fbc57eb4e61d | 55 | * @endcode |
KarimAzzouz | 1:fbc57eb4e61d | 56 | */ |
KarimAzzouz | 0:c1b48befe066 | 57 | class MAF { |
KarimAzzouz | 0:c1b48befe066 | 58 | |
KarimAzzouz | 0:c1b48befe066 | 59 | public: |
KarimAzzouz | 1:fbc57eb4e61d | 60 | |
KarimAzzouz | 1:fbc57eb4e61d | 61 | /* Creates a 10-Point Moving Average Filter object |
KarimAzzouz | 1:fbc57eb4e61d | 62 | * |
KarimAzzouz | 1:fbc57eb4e61d | 63 | */ |
KarimAzzouz | 0:c1b48befe066 | 64 | MAF(void); |
KarimAzzouz | 1:fbc57eb4e61d | 65 | |
KarimAzzouz | 1:fbc57eb4e61d | 66 | /* Updates the filter |
KarimAzzouz | 1:fbc57eb4e61d | 67 | * @param data new data input for the filter,returns the float average |
KarimAzzouz | 1:fbc57eb4e61d | 68 | */ |
KarimAzzouz | 1:fbc57eb4e61d | 69 | float update(float data); |
KarimAzzouz | 1:fbc57eb4e61d | 70 | |
KarimAzzouz | 1:fbc57eb4e61d | 71 | /* Updates the filter |
KarimAzzouz | 1:fbc57eb4e61d | 72 | * @param data new data input for the filter, returns the integer average |
KarimAzzouz | 1:fbc57eb4e61d | 73 | */ |
KarimAzzouz | 1:fbc57eb4e61d | 74 | int updateInt(int data); |
KarimAzzouz | 0:c1b48befe066 | 75 | |
KarimAzzouz | 0:c1b48befe066 | 76 | private : |
KarimAzzouz | 1:fbc57eb4e61d | 77 | |
KarimAzzouz | 1:fbc57eb4e61d | 78 | float _k[10]; |
KarimAzzouz | 1:fbc57eb4e61d | 79 | int _a[10]; |
KarimAzzouz | 1:fbc57eb4e61d | 80 | |
KarimAzzouz | 0:c1b48befe066 | 81 | float _result; |
KarimAzzouz | 1:fbc57eb4e61d | 82 | int _resultInt; |
KarimAzzouz | 0:c1b48befe066 | 83 | }; |
KarimAzzouz | 0:c1b48befe066 | 84 | |
KarimAzzouz | 0:c1b48befe066 | 85 | #endif |