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@8:a1ea1bf49944, 2015-10-19 (annotated)
- Committer:
- fredwsl
- Date:
- Mon Oct 19 08:36:31 2015 +0000
- Revision:
- 8:a1ea1bf49944
- Parent:
- 7:7493a0c5c6aa
Working low pass filter
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 | |
| fredwsl | 8:a1ea1bf49944 | 7 | AnalogIn EMG(A1); |
| 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 | |
| fredwsl | 6:c20a8c0f90ed | 17 | // Define moving average variables |
| fredwsl | 8:a1ea1bf49944 | 18 | const int N = 125; |
| fredwsl | 6:c20a8c0f90ed | 19 | double a; |
| Peasofcake | 2:e30dbfec6d1e | 20 | |
| Peasofcake | 2:e30dbfec6d1e | 21 | // Define the storage variables and filter coeicients for two filters |
| Peasofcake | 2:e30dbfec6d1e | 22 | double f1_v1 = 0, f1_v2 = 0, f2_v1 = 0, f2_v2 = 0; |
| fredwsl | 8:a1ea1bf49944 | 23 | const double f1_a1 = -1.822694925196308, f1_a2 = 0.837181651256022, f1_b0 = 0.914969144113083, f1_b1 = -1.829938288226165, f1_b2 = 0.914969144113083; |
| fredwsl | 8:a1ea1bf49944 | 24 | const double f2_a1 = -1.964460580205232, f2_a2 = 0.965081173899135, f2_b0 = 0.000155148423475721, f2_b1 = 0.000310296846951441, f2_b2 = 0.000155148423475721; |
| Peasofcake | 2:e30dbfec6d1e | 25 | |
| Peasofcake | 3:04f3cca82b22 | 26 | 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 | 27 | { |
| Peasofcake | 3:04f3cca82b22 | 28 | double v = u-a1*v1-a2*v2; |
| Peasofcake | 3:04f3cca82b22 | 29 | double y = b0*v + b1*v1 + b2*v2; |
| Peasofcake | 2:e30dbfec6d1e | 30 | v2 = v1; v1 = v; |
| Peasofcake | 2:e30dbfec6d1e | 31 | return y; |
| Peasofcake | 2:e30dbfec6d1e | 32 | } |
| Peasofcake | 2:e30dbfec6d1e | 33 | |
| fredwsl | 6:c20a8c0f90ed | 34 | double sliding_average(double u,const int f_N) |
| fredwsl | 6:c20a8c0f90ed | 35 | { |
| fredwsl | 6:c20a8c0f90ed | 36 | double f_x[f_N]; |
| fredwsl | 6:c20a8c0f90ed | 37 | double f_sum=0; |
| fredwsl | 8:a1ea1bf49944 | 38 | f_x[1]=abs(u); |
| fredwsl | 6:c20a8c0f90ed | 39 | for (int i=f_N; i>=1; i--){ |
| fredwsl | 6:c20a8c0f90ed | 40 | f_x[i]=f_x[i-1]; |
| fredwsl | 6:c20a8c0f90ed | 41 | } |
| fredwsl | 6:c20a8c0f90ed | 42 | |
| fredwsl | 6:c20a8c0f90ed | 43 | |
| fredwsl | 6:c20a8c0f90ed | 44 | for (int i=f_N; i>=0; i--){ |
| fredwsl | 6:c20a8c0f90ed | 45 | f_sum=f_x[i]+f_sum; |
| fredwsl | 6:c20a8c0f90ed | 46 | } |
| fredwsl | 7:7493a0c5c6aa | 47 | a=f_sum/double(f_N); |
| fredwsl | 6:c20a8c0f90ed | 48 | f_sum=0; |
| fredwsl | 6:c20a8c0f90ed | 49 | return a; |
| fredwsl | 6:c20a8c0f90ed | 50 | } |
| fredwsl | 6:c20a8c0f90ed | 51 | |
| Peasofcake | 2:e30dbfec6d1e | 52 | |
| Peasofcake | 2:e30dbfec6d1e | 53 | |
| Peasofcake | 2:e30dbfec6d1e | 54 | |
| Peasofcake | 2:e30dbfec6d1e | 55 | void scopeSend(){ |
| fredwsl | 6:c20a8c0f90ed | 56 | double u1 = EMG.read() ; //filter 1 input |
| Peasofcake | 3:04f3cca82b22 | 57 | double y1 = biquad( u1, f1_v1, f1_v2, f1_a1, f1_a2, f1_b0, f1_b1, f1_b2 ); |
| fredwsl | 8:a1ea1bf49944 | 58 | double y2 = biquad( fabs(y1), f2_v1, f2_v2, f2_a1, f2_a2, f2_b0, f2_b1, f2_b2 ); |
| fredwsl | 8:a1ea1bf49944 | 59 | //double z1 = 3*sliding_average(y2, N); |
| fredwsl | 8:a1ea1bf49944 | 60 | scope.set(0,y2); |
| Peasofcake | 2:e30dbfec6d1e | 61 | scope.send(); |
| Peasofcake | 2:e30dbfec6d1e | 62 | } |
| Peasofcake | 3:04f3cca82b22 | 63 | |
| Peasofcake | 0:c8a6e00fbdc3 | 64 | int main() |
| Peasofcake | 3:04f3cca82b22 | 65 | { |
| fredwsl | 6:c20a8c0f90ed | 66 | scopeTimer.attach(&scopeSend,0.002); |
| Peasofcake | 2:e30dbfec6d1e | 67 | while(1){} |
| Peasofcake | 1:f6110f80fa45 | 68 | |
| Peasofcake | 0:c8a6e00fbdc3 | 69 | } |
