a 10-Point moving average filter that can return an int or a float depending on the function used
Revision 1:fbc57eb4e61d, committed 2013-05-05
- Comitter:
- KarimAzzouz
- Date:
- Sun May 05 13:31:02 2013 +0000
- Parent:
- 0:c1b48befe066
- Commit message:
- initial commit
Changed in this revision
MAF.cpp | Show annotated file Show diff for this revision Revisions of this file |
MAF.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r c1b48befe066 -r fbc57eb4e61d MAF.cpp --- a/MAF.cpp Wed Dec 26 10:44:34 2012 +0000 +++ b/MAF.cpp Sun May 05 13:31:02 2013 +0000 @@ -1,20 +1,59 @@ -#include "MAF.h" - - MAF::MAF(){ - _result = 0; - _k[0]=0 ; - _k[1]=0 ; - _k[2]=0 ; - _k[3]=0 ; - } - - float MAF::update(float data){ - - _k[0] = data; - _result = _k[0]*0.25 + _k[1]*0.25 + _k[2]*0.25 + _k[3]*0.25; - _k[3] = _k[2]; - _k[2] = _k[1]; - _k[1] = _k[0]; - - return _result; +/* Copyright (c) <2012> MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * @section DESCRIPTION + * + * 10-Point Moving Average Filter + */ + +#include "MAF.h" + + MAF::MAF(){} + + float MAF::update(float data){ + + _k[0] = data; + _result = _k[0]*0.1 + _k[1]*0.1 + _k[2]*0.1 + _k[3]*0.1 + _k[4]*0.1 + _k[5]*0.1 + _k[6]*0.1 + _k[7]*0.1 + _k[8]*0.1 + _k[9]*0.1 ; + _k[9] = _k[8]; + _k[8] = _k[7]; + _k[7] = _k[6]; + _k[6] = _k[5]; + _k[5] = _k[4]; + _k[4] = _k[3]; + _k[3] = _k[2]; + _k[2] = _k[1]; + _k[1] = _k[0]; + + return _result; + } + + int MAF::updateInt(int data){ + + _a[0] = data; + _result = _a[0]*0.1 + _a[1]*0.1 + _a[2]*0.1 + _a[3]*0.1 + _a[4]*0.1 + _a[5]*0.1 + _a[6]*0.1 + _a[7]*0.1 + _a[8]*0.1 + _a[9]*0.1 ; + _a[9] = _a[8]; + _a[8] = _a[7]; + _a[7] = _a[6]; + _a[6] = _a[5]; + _a[5] = _a[4]; + _a[4] = _a[3]; + _a[3] = _a[2]; + _a[2] = _a[1]; + _a[1] = _a[0]; + + return _resultInt; } \ No newline at end of file
diff -r c1b48befe066 -r fbc57eb4e61d MAF.h --- a/MAF.h Wed Dec 26 10:44:34 2012 +0000 +++ b/MAF.h Sun May 05 13:31:02 2013 +0000 @@ -1,17 +1,85 @@ +/* Copyright (c) <2012> MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * @section DESCRIPTION + * + * Moving Average Filter + * + * + */ + #ifndef MAF_H #define MAF_H #include "mbed.h" +/** 10-Point Moving Average filter + * + * Example: + * @code + * // Update the filter with gyroscope readings + * #include "mbed.h" + * #include "ITG3200.h" + * #include "MAF.h" + * + * Serial pc(USBTX, USBRX); + * ITG3200 gyro(p9, p10); + * MAF Filter(); + * int Average; + * + * int main() { + * + * gyro.setLpBandwidth(LPFBW_42HZ); + * + * while(1){ + * + * Average = Filter.updateInt(gyro.getGyroX()); + * pc.printf("%i\n",Average); + * wait_ms(50); + * } + * } + * @endcode + */ class MAF { public: + + /* Creates a 10-Point Moving Average Filter object + * + */ MAF(void); - float update(float data); + + /* Updates the filter + * @param data new data input for the filter,returns the float average + */ + float update(float data); + + /* Updates the filter + * @param data new data input for the filter, returns the integer average + */ + int updateInt(int data); private : - float _k[4]; + + float _k[10]; + int _a[10]; + float _result; + int _resultInt; }; #endif \ No newline at end of file