Filter for EMG signals The signal will be filtered using a notch, highpass and lowpass filter. The filtered signal will be compared to a preset threshold and according to the strength of the signal the program will perform an action. In this case it will assign a colour to a led.
Dependencies: HIDScope MODSERIAL mbed
Fork of EMGfilter24 by
main.cpp@0:41226c0fd285, 2016-10-24 (annotated)
- Committer:
- Iknowright
- Date:
- Mon Oct 24 14:46:20 2016 +0000
- Revision:
- 0:41226c0fd285
- Child:
- 1:6081dc1ecd1f
EMGfilter24_2;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Iknowright | 0:41226c0fd285 | 1 | #include "mbed.h" |
Iknowright | 0:41226c0fd285 | 2 | #include "BiQuad.h" |
Iknowright | 0:41226c0fd285 | 3 | #include "HIDScope.h" |
Iknowright | 0:41226c0fd285 | 4 | #include "MODSERIAL.h" |
Iknowright | 0:41226c0fd285 | 5 | |
Iknowright | 0:41226c0fd285 | 6 | MODSERIAL pc(USBTX, USBRX); |
Iknowright | 0:41226c0fd285 | 7 | DigitalOut ledje(LED_GREEN); |
Iknowright | 0:41226c0fd285 | 8 | |
Iknowright | 0:41226c0fd285 | 9 | //High Pass Filter 1 |
Iknowright | 0:41226c0fd285 | 10 | const double hf1_b0=0.9033; |
Iknowright | 0:41226c0fd285 | 11 | const double hf1_b1=-0.9023; |
Iknowright | 0:41226c0fd285 | 12 | const double hf1_b2=0; |
Iknowright | 0:41226c0fd285 | 13 | const double hf1_a1=-0.9391; |
Iknowright | 0:41226c0fd285 | 14 | const double hf1_a2=0; |
Iknowright | 0:41226c0fd285 | 15 | |
Iknowright | 0:41226c0fd285 | 16 | //High Pass Filter 2 |
Iknowright | 0:41226c0fd285 | 17 | const double hf2_b0=1; |
Iknowright | 0:41226c0fd285 | 18 | const double hf2_b1=-2.0018; |
Iknowright | 0:41226c0fd285 | 19 | const double hf2_b2=1.0018; |
Iknowright | 0:41226c0fd285 | 20 | const double hf2_a1=-1.8996; |
Iknowright | 0:41226c0fd285 | 21 | const double hf2_a2=0.9033; |
Iknowright | 0:41226c0fd285 | 22 | |
Iknowright | 0:41226c0fd285 | 23 | //High Pass Filter 3 |
Iknowright | 0:41226c0fd285 | 24 | const double hf3_b0=1; |
Iknowright | 0:41226c0fd285 | 25 | const double hf3_b1=-1.9993; |
Iknowright | 0:41226c0fd285 | 26 | const double hf3_b2=0.9993; |
Iknowright | 0:41226c0fd285 | 27 | const double hf3_a1=-1.9581; |
Iknowright | 0:41226c0fd285 | 28 | const double hf3_a2=0.9619; |
Iknowright | 0:41226c0fd285 | 29 | |
Iknowright | 0:41226c0fd285 | 30 | Ticker emgSampleTicker; |
Iknowright | 0:41226c0fd285 | 31 | AnalogIn emg(A0); |
Iknowright | 0:41226c0fd285 | 32 | |
Iknowright | 0:41226c0fd285 | 33 | //void emgSample() { |
Iknowright | 0:41226c0fd285 | 34 | //double emgFiltered = bqc_hf.step(emg.read()); |
Iknowright | 0:41226c0fd285 | 35 | //} |
Iknowright | 0:41226c0fd285 | 36 | |
Iknowright | 0:41226c0fd285 | 37 | int main(){ |
Iknowright | 0:41226c0fd285 | 38 | emgSampleTicker.attach(&emgSample, 0.01); |
Iknowright | 0:41226c0fd285 | 39 | |
Iknowright | 0:41226c0fd285 | 40 | double biquad_hf1(double u, double&v1, double&v2, const double hf1_a1, const double hf1_a2, const double hf1_b0, const double hf1_b1, const double hf1_b2); |
Iknowright | 0:41226c0fd285 | 41 | { |
Iknowright | 0:41226c0fd285 | 42 | double v = u - hf1_a1∗v1 - hf1_a2∗v2; |
Iknowright | 0:41226c0fd285 | 43 | hf1_y = hf1_b0∗v + hf1_b1∗v1 + hf1_b2∗v2; |
Iknowright | 0:41226c0fd285 | 44 | v2 = v1; |
Iknowright | 0:41226c0fd285 | 45 | v1 = v; |
Iknowright | 0:41226c0fd285 | 46 | return hf1_y |
Iknowright | 0:41226c0fd285 | 47 | } |
Iknowright | 0:41226c0fd285 | 48 | |
Iknowright | 0:41226c0fd285 | 49 | double biquad_hf2(double u, double&v1, double&v2, const double hf2_a1, const double hf2_a2, const double hf2_b0, const double hf2_b1, const double hf2_b2){ |
Iknowright | 0:41226c0fd285 | 50 | u = hf1_y; |
Iknowright | 0:41226c0fd285 | 51 | double v = u − hf2_a1∗v1 − hf2_a2∗v2; |
Iknowright | 0:41226c0fd285 | 52 | hf2_y = hf2_b0∗v + hf2_b1∗v1 + hf2_b2∗v2; |
Iknowright | 0:41226c0fd285 | 53 | v2 = v1; |
Iknowright | 0:41226c0fd285 | 54 | v1 = v; |
Iknowright | 0:41226c0fd285 | 55 | return hf2_y |
Iknowright | 0:41226c0fd285 | 56 | } |
Iknowright | 0:41226c0fd285 | 57 | |
Iknowright | 0:41226c0fd285 | 58 | double biquad_hf3(double u, double&v1, double&v2, const double hf3_a1, const double hf3_a2, const double hf3_b0, const double hf3_b1, const double hf3_b2){ |
Iknowright | 0:41226c0fd285 | 59 | u = hf2_y; |
Iknowright | 0:41226c0fd285 | 60 | double v = u − hf3_a1∗v1 − hf3_a2∗v2; |
Iknowright | 0:41226c0fd285 | 61 | hf3_y = hf3_b0∗v + hf3_b1∗v1 + hf3_b2∗v2; |
Iknowright | 0:41226c0fd285 | 62 | v2 = v1; |
Iknowright | 0:41226c0fd285 | 63 | v1 = v; |
Iknowright | 0:41226c0fd285 | 64 | return hf3_y |
Iknowright | 0:41226c0fd285 | 65 | } |
Iknowright | 0:41226c0fd285 | 66 | |
Iknowright | 0:41226c0fd285 | 67 | double printf(hf3_y); |
Iknowright | 0:41226c0fd285 | 68 | } |