Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: HIDScope MODSERIAL mbed
Fork of Signal Filter by
main.cpp@3:04f3cca82b22, 2015-09-21 (annotated)
- Committer:
- Peasofcake
- Date:
- Mon Sep 21 10:30:38 2015 +0000
- Revision:
- 3:04f3cca82b22
- Parent:
- 2:e30dbfec6d1e
- Child:
- 4:d6a3b318c744
No errors filter, with Filtercoefficients for 50Hz
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Peasofcake | 0:c8a6e00fbdc3 | 1 | #include "mbed.h" |
Peasofcake | 1:f6110f80fa45 | 2 | #include "MODSERIAL.h" |
Peasofcake | 2:e30dbfec6d1e | 3 | #include "math.h" |
Peasofcake | 1:f6110f80fa45 | 4 | #include "HIDScope.h" |
Peasofcake | 2:e30dbfec6d1e | 5 | |
Peasofcake | 2:e30dbfec6d1e | 6 | |
Peasofcake | 2:e30dbfec6d1e | 7 | AnalogIn EMG(A0); |
Peasofcake | 1:f6110f80fa45 | 8 | MODSERIAL pc(USBTX, USBRX); |
Peasofcake | 0:c8a6e00fbdc3 | 9 | |
Peasofcake | 2:e30dbfec6d1e | 10 | //const int baudrate = 115200; |
Peasofcake | 1:f6110f80fa45 | 11 | //const int ms_wait = 100; |
Peasofcake | 0:c8a6e00fbdc3 | 12 | |
Peasofcake | 1:f6110f80fa45 | 13 | // Define the HIDScope and Ticker objects |
Peasofcake | 2:e30dbfec6d1e | 14 | HIDScope scope(1); |
Peasofcake | 2:e30dbfec6d1e | 15 | Ticker scopeTimer; |
Peasofcake | 2:e30dbfec6d1e | 16 | // Define the storage variables and filter coeicients for two filters |
Peasofcake | 2:e30dbfec6d1e | 17 | |
Peasofcake | 2:e30dbfec6d1e | 18 | |
Peasofcake | 2:e30dbfec6d1e | 19 | // Define the storage variables and filter coeicients for two filters |
Peasofcake | 2:e30dbfec6d1e | 20 | double f1_v1 = 0, f1_v2 = 0, f2_v1 = 0, f2_v2 = 0; |
Peasofcake | 3:04f3cca82b22 | 21 | const double f1_a1 = 0.25071442433, f1_a2 = 0.21711875780, f1_b0 = 1.0, f1_b1 = -1.62432585007, f1_b2 = 1.0; |
Peasofcake | 3:04f3cca82b22 | 22 | const double f2_a1 = -1.77682226139, f2_a2 = 0.80213897411, f2_b0 = 1.0, f2_b1 = -1.62432585007, f2_b2 = 1.0; |
Peasofcake | 2:e30dbfec6d1e | 23 | |
Peasofcake | 3:04f3cca82b22 | 24 | double biquad( double u, double &v1, double &v2, const double a1, const double a2, const double b0, const double b1, const double b2 ) |
Peasofcake | 3:04f3cca82b22 | 25 | { |
Peasofcake | 3:04f3cca82b22 | 26 | double v = u-a1*v1-a2*v2; |
Peasofcake | 3:04f3cca82b22 | 27 | double y = b0*v + b1*v1 + b2*v2; |
Peasofcake | 2:e30dbfec6d1e | 28 | v2 = v1; v1 = v; |
Peasofcake | 2:e30dbfec6d1e | 29 | return y; |
Peasofcake | 2:e30dbfec6d1e | 30 | } |
Peasofcake | 2:e30dbfec6d1e | 31 | |
Peasofcake | 2:e30dbfec6d1e | 32 | // This is your controller, call it using a Ticker |
Peasofcake | 3:04f3cca82b22 | 33 | // void myController() { |
Peasofcake | 3:04f3cca82b22 | 34 | //double u1 = EMG, u2 = y1 ; |
Peasofcake | 3:04f3cca82b22 | 35 | //double y1 = biquad( u1, f1_v1, f1_v2, f1_a1, f1_a2, f1_b0, f1_b1, f1_b2 ); |
Peasofcake | 3:04f3cca82b22 | 36 | //double y2 = biquad( u2, f2_v1, f2_v2, f2_a1, f2_a2, f2_b0, f2_b1, f2_b2 ); |
Peasofcake | 3:04f3cca82b22 | 37 | //} |
Peasofcake | 2:e30dbfec6d1e | 38 | |
Peasofcake | 2:e30dbfec6d1e | 39 | |
Peasofcake | 2:e30dbfec6d1e | 40 | |
Peasofcake | 2:e30dbfec6d1e | 41 | |
Peasofcake | 2:e30dbfec6d1e | 42 | void scopeSend(){ |
Peasofcake | 3:04f3cca82b22 | 43 | double u1 = EMG ; |
Peasofcake | 3:04f3cca82b22 | 44 | double y1 = biquad( u1, f1_v1, f1_v2, f1_a1, f1_a2, f1_b0, f1_b1, f1_b2 ); |
Peasofcake | 3:04f3cca82b22 | 45 | double u2 = y1; |
Peasofcake | 3:04f3cca82b22 | 46 | double y2 = biquad( u2, f2_v1, f2_v2, f2_a1, f2_a2, f2_b0, f2_b1, f2_b2 ); |
Peasofcake | 3:04f3cca82b22 | 47 | scope.set(0,y2); |
Peasofcake | 2:e30dbfec6d1e | 48 | scope.send(); |
Peasofcake | 2:e30dbfec6d1e | 49 | } |
Peasofcake | 3:04f3cca82b22 | 50 | |
Peasofcake | 0:c8a6e00fbdc3 | 51 | int main() |
Peasofcake | 3:04f3cca82b22 | 52 | { |
Peasofcake | 3:04f3cca82b22 | 53 | |
Peasofcake | 3:04f3cca82b22 | 54 | scopeTimer.attach_us(&scopeSend,1e4); |
Peasofcake | 2:e30dbfec6d1e | 55 | while(1){} |
Peasofcake | 1:f6110f80fa45 | 56 | |
Peasofcake | 0:c8a6e00fbdc3 | 57 | } |