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 Steven Spoolder

Revision:
1:6081dc1ecd1f
Parent:
0:41226c0fd285
Child:
2:6402a7071ba3
diff -r 41226c0fd285 -r 6081dc1ecd1f main.cpp
--- a/main.cpp	Mon Oct 24 14:46:20 2016 +0000
+++ b/main.cpp	Tue Oct 25 12:03:32 2016 +0000
@@ -2,67 +2,80 @@
 #include "BiQuad.h"
 #include "HIDScope.h"
 #include "MODSERIAL.h"
-
-MODSERIAL pc(USBTX, USBRX);
-DigitalOut ledje(LED_GREEN);
+#include <cmath>
 
-//High Pass Filter 1
-const double hf1_b0=0.9033;
-const double hf1_b1=-0.9023;
-const double hf1_b2=0;
-const double hf1_a1=-0.9391;
-const double hf1_a2=0;
+MODSERIAL   pc(USBTX, USBRX);
+Ticker      emgSampleTicker;
+AnalogIn    emg0(A0);
+AnalogIn    emg1(A1);
+DigitalOut  ledje(LED_GREEN);
+HIDScope    scope(2);        
+
+double hf_b0 = 0.9355;
+double hf_b1 = -1.8711;
+double hf_b2 = 0.9355;
+double hf_a1 = -1.8669;
+double hf_a2 = 0.8752;
 
-//High Pass Filter 2
-const double hf2_b0=1;
-const double hf2_b1=-2.0018;
-const double hf2_b2=1.0018;
-const double hf2_a1=-1.8996;
-const double hf2_a2=0.9033;
+double lf_b0 = 8.7656e-5;
+double lf_b1 = 1.17531e-4;
+double lf_b2 = 8.7656e-5;
+double lf_a1 = -1.9733;
+double lf_a2 = 0.9737;
+
+double v1 = 0, v2 = 0, u = 0;
 
-//High Pass Filter 3
-const double hf3_b0=1;
-const double hf3_b1=-1.9993;
-const double hf3_b2=0.9993;
-const double hf3_a1=-1.9581;
-const double hf3_a2=0.9619;
+/** Sample function
+ * this function samples the emg and sends it to HIDScope
+ **/
+void sample()
+{
+    // 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() );
+//    scope.set(1, emg1.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;
+}
 
-Ticker emgSampleTicker;
-AnalogIn emg(A0);
-
-//void emgSample() {
-//double emgFiltered = bqc_hf.step(emg.read());
-//}
 
 int main(){
-    emgSampleTicker.attach(&emgSample, 0.01);
+   
+  /**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);
+    // empty loop, sample() is executed periodically
+    while(1) {
 
-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);
-{
-    double v = u - hf1_a1∗v1 - hf1_a2∗v2;
-    hf1_y = hf1_b0∗v + hf1_b1∗v1 + hf1_b2∗v2;
-    v2 = v1;
-    v1 = v;
-    return hf1_y
-}
+        double emgFilter();
+    {
 
-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){
-    u = hf1_y;
-    double v = u − hf2_a1∗v1 − hf2_a2∗v2;
-    hf2_y = hf2_b0∗v + hf2_b1∗v1 + hf2_b2∗v2;
+//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 = v;
-    return hf2_y
-}
+    v1 = hf_v;
 
-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){
-    u = hf2_y;
-    double v = u − hf3_a1∗v1 − hf3_a2∗v2;
-    hf3_y = hf3_b0∗v + hf3_b1∗v1 + hf3_b2∗v2;
+//Rectification
+    double rect_y = fabs(hf_y);
+    
+//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 = v;
-    return hf3_y
+    v1 = lf_v;
+    
+while(1){
+    
+    scope.set(1, lf_y);
+    
+    }
+    
+    }
+
 }
-
-double printf(hf3_y);
-}
\ No newline at end of file
+}