EMG filtered

Dependencies:   HIDScope mbed

Fork of EMG by Tom Tom

Revision:
21:173af3b4367d
Parent:
20:97059009a491
Child:
22:c7ccfe13d168
--- a/main.cpp	Thu Sep 22 08:53:50 2016 +0000
+++ b/main.cpp	Mon Oct 09 10:06:21 2017 +0000
@@ -2,21 +2,38 @@
 #include "HIDScope.h"
 
 //Define objects
-AnalogIn    emg0( A0 );
+AnalogIn    EMGData1( A0 );
 AnalogIn    emg1( A1 );
 
 Ticker      sample_timer;
 HIDScope    scope( 2 );
 DigitalOut  led(LED1);
+volatile float EMGData_f1=0;
+double f_v1_EMG=0, f_v2_EMG=0; // set filter variables EMG 1
+const double M1_F_A1_EMG=-1.561018075800718, M1_F_A2_EMG=0.641351538057563, M1_F_B0_EMG=0.020083365564211, 
+M1_F_B1_EMG=0.040166731128423, M1_F_B2_EMG=0.020083365564211;// set EMG filter coefficients 
 
 /** Sample function
  * this function samples the emg and sends it to HIDScope
  **/
+ 
+//IIRFilter
+double IIRFilter(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 sample()
 {
+    EMGData_f1 = IIRFilter(EMGData1, f_v1_EMG, f_v2_EMG, M1_F_A1_EMG, M1_F_A2_EMG, M1_F_B0_EMG, M1_F_B1_EMG, M1_F_B2_EMG);
+
     /* 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() );
+    scope.set(0, EMGData1);                            // set scope channel 0 EMG
+    scope.set(1, EMGData_f1);                           // set scope channel 1 EMG filtered
     /* 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 */
@@ -30,7 +47,7 @@
     /**Attach the 'sample' function to the timer 'sample_timer'.
     * this ensures that 'sample' is executed every... 0.002 seconds = 500 Hz
     */
-    sample_timer.attach(&sample, 0.002);
+    sample_timer.attach(&sample, 0.005);
 
     /*empty loop, sample() is executed periodically*/
     while(1) {}