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
Revision 3:faed8b7f6542, committed 2016-10-27
- Comitter:
- Nickname
- Date:
- Thu Oct 27 08:14:12 2016 +0000
- Parent:
- 2:6402a7071ba3
- Child:
- 4:fcada70891c5
- Commit message:
- Trial without error;
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Tue Oct 25 12:53:45 2016 +0000 +++ b/main.cpp Thu Oct 27 08:14:12 2016 +0000 @@ -2,14 +2,23 @@ #include "BiQuad.h" #include "HIDScope.h" #include "MODSERIAL.h" -#include <cmath> +#include <math.h> MODSERIAL pc(USBTX, USBRX); -Ticker emgSampleTicker; +Ticker sampleTicker; +Ticker goTicker; AnalogIn emg0(A0); AnalogIn emg1(A1); -DigitalOut ledje(LED_GREEN); -HIDScope scope(2); +DigitalOut ledG(LED_GREEN); +DigitalOut ledB(LED_BLUE); +DigitalOut ledR(LED_RED); +HIDScope scope(2); + +double no_b0 = 0.9911; +double no_b1 = -1.6036; +double no_b2 = 0.9911; +double no_a1 = -1.6036; +double no_a2 = 0.9822; double hf_b0 = 0.9355; double hf_b1 = -1.8711; @@ -23,59 +32,86 @@ double lf_a1 = -1.9733; double lf_a2 = 0.9737; -double v1 = 0, v2 = 0, u = 0; +double no_v1 = 0, no_v2 = 0; +double hf_v1 = 0, hf_v2 = 0; +double lf_v1 = 0, lf_v2 = 0; +double no_y; +double lf_y; +double hf_y; +double rect_y; +int go = 0; +const double threshold_value = 0.13; -/** Sample function - * this function samples the emg and sends it to HIDScope - **/ -void sample() +double biquad_no(double u, double&v1 , double&v2 , const double a1 , const double a2 , const double b0 , + const double b1 , const double b2 ) { - // Set the sampled emg values in channel 0 (the first channel) and 1 (the second channel) in the 'HIDScope' instance named 'scope' - scope.set(0, emg0.read() ); - /* Repeat the step above if required for more channels of required (channel 0 up to 5 = 6 channels) - * Ensure that enough channels are available (HIDScope scope( 2 )) - * Finally, send all channels to the PC at once */ - scope.send(); - // To indicate that the function is working, the LED is toggled - ledje = ledje; + double v = u - a1*v1 - a2*v2; + double y = b0*v + b1*v1 + b2*v2; + v2 = v1; + v1 = v; + return y; +} + +double biquad_hf(double u, double&v1 , double&v2 , const double a1 , const double a2 , const double b0 , + const double b1 , const double b2 ) +{ + double v = u - a1*v1 - a2*v2; + double y = b0*v + b1*v1 + b2*v2; + v2 = v1; + v1 = v; + return y; } +double biquad_lf(double u, double&v1 , double&v2 , const double a1 , const double a2 , const double b0 , + const double b1 , const double b2 ) +{ + double v = u - a1*v1 - a2*v2; + double y = b0*v + b1*v1 + b2*v2; + v2 = v1; + v1 = v; + return y; +} + +void scopeSend(void){ + no_y = biquad_no(emg0.read(), no_v1, no_v2, no_a1, no_a2, no_b0, no_b1, no_b2); + hf_y = biquad_hf(no_y, hf_v1, hf_v2, hf_a1, hf_a2, hf_b0, hf_b1, hf_b2); + rect_y = fabs(hf_y); + lf_y = biquad_lf(rect_y, lf_v1, lf_v2, lf_a1, lf_a2, lf_b0, lf_b1, lf_b2)/0.2; + scope.set(0, emg0.read()); + scope.set(1, lf_y); + scope.send(); + + } + +void threshold(double lf_y, const double threshold_value){ + if (lf_y > threshold_value){ + go = !go + } + /* if (lf_y > threshold_value){ + ledB = !ledB; + } + else{ + ledB = ledB; + } + } +*/ int main(){ /**Attach the 'sample' function to the timer 'sample_timer'. * this ensures that 'sample' is executed every... 0.002 seconds = 500 Hz */ - emgSampleTicker.attach(&sample, 0.002); + // emgSampleTicker.attach(&sample, 0.002); // empty loop, sample() is executed periodically - while(1) { - - double emgFilter(); - { - -//High Pass Filter (biquad) - double hf_v = u - hf_a1*v1 - hf_a2*v2; - double hf_y = hf_b0*hf_v + hf_b1*v1 + hf_b2*v2; - v2 = v1; - v1 = hf_v; - -//Rectification - double rect_y = fabs(hf_y); + + sampleTicker.attach(scopeSend,0.002); + goTicker.attach(threshold,0.002); -//Low Pass Filter (biquad) - double lf_v = rect_y - lf_a1*v1 - lf_a2*v2; - double lf_y = lf_b0*hf_v + lf_b1*v1 + lf_b2*v2; - v2 = v1; - v1 = lf_v; - -while(1){ - - scope.set(1, lf_y); - scope.send(); - + while(1) { + if (go == 0){ + ledB = ledB; + } + else{ + ledB = !ledB; } - - } - -} -} +} \ No newline at end of file