filters aangepast en bepaling threshold

Dependencies:   HIDScope MODSERIAL biquadFilter mbed

Fork of EMGV3 by Margreeth de Breij

Revision:
29:c43e9f78db37
Parent:
28:38d21b78c6aa
--- a/main.cpp	Wed Oct 14 12:28:49 2015 +0000
+++ b/main.cpp	Thu Oct 15 10:54:01 2015 +0000
@@ -12,8 +12,9 @@
     Ticker      serial;
     Ticker      MovingAverageLeft;
     Ticker      MovingAverageRight;
-    HIDScope    scope(4);
+    HIDScope    scope(6);
     DigitalOut led(LED_RED);
+    DigitalOut ledG(LED_GREEN);
     MODSERIAL pc(USBTX, USBRX);
     
 
@@ -24,20 +25,14 @@
     double EMG_left_f1;
     double EMG_left_f2;
     double EMG_left_f3;
-    double EMG_left_f4;
-    double EMG_left_f5;
-    double EMG_left_f6;
-    double EMG_left_abs;
+      double EMG_left_abs;
 
     double EMG_right_value;
     double EMG_right_f1;
     double EMG_right_f2;
     double EMG_right_f3;
-    double EMG_right_f4;
-    double EMG_right_f5;
-    double EMG_right_f6;
     double EMG_right_abs;
-    double Threshold1 = 0.02;
+    double Threshold1;
     double Threshold2 = 0.06;
     
     int N = 50;
@@ -47,44 +42,33 @@
     double EMG_right_MAF;
     
 // coëfficiënten
-    const double BiGainEMG_H1 = 0.795375, BiGainEMG_H2 = 0.895763;
-    const double EMGH1_a1 = -1.56308931068, EMGH1_a2 = 0.61765749583, EMGH1_b0 = 1.0*BiGainEMG_H1, EMGH1_b1 = -1.99909075151*BiGainEMG_H1, EMGH1_b2 = 1.0*BiGainEMG_H1; //coefficients for high-pass filter
-    const double EMGH2_a1 = -1.75651417587, EMGH2_a2 = 0.82183182692, EMGH2_b0 = 1.0*BiGainEMG_H2, EMGH2_b1 = -1.99470632157*BiGainEMG_H2, EMGH2_b2 = 1.0*BiGainEMG_H2; //coefficients for high-pass filter
+    const double BiGainEMG_H1 = 0.796821; 
+    const double EMGH1_a1 = -1.47500228332, EMGH1_a2 = 0.55273994299, EMGH1_b0 = 1.0*BiGainEMG_H1, EMGH1_b1 = -1.99922446977*BiGainEMG_H1, EMGH1_b2 = 1.0*BiGainEMG_H1; //coefficients for high-pass filter
     
-    const double BiGainEMG_L1=0.959332, BiGainEMG_L2 = 0.223396;
-    const double EMGL1_a1 = -1.55576653052, EMGL1_a2 = 0.61374320375, EMGL1_b0 = 1.0*BiGainEMG_L1, EMGL1_b1 = -0.90928276835*BiGainEMG_L1, EMGL1_b2 = 1.0*BiGainEMG_L1; // coefficients for low-pass filter
-    const double EMGL2_a1 = -1.79696141922, EMGL2_a2 = 0.85096669383, EMGL2_b0 = 1.0*BiGainEMG_L2, EMGL2_b1 = -1.75825311060*BiGainEMG_L2, EMGL2_b2 = 1.0*BiGainEMG_L2; // coefficients for low-pass filter
-
-    const double BiGainEMG_N1 = 1.0, BiGainEMG_N2 = 0.965081;
-    const double EMGN1_a1 = -1.56858163035, EMGN1_a2 = 0.96424138362, EMGN1_b0 = 1.0*BiGainEMG_N1, EMGN1_b1 = -1.61854514265*BiGainEMG_N1, EMGN1_b2 = 1.0*BiGainEMG_N1; //coefficients for notch filter
-    const double EMGN2_a1 = -1.61100357722, EMGN2_a2 = 0.96592170538, EMGN2_b0 = 1.0*BiGainEMG_N2, EMGN2_b1 = -1.61854514265*BiGainEMG_N2, EMGN2_b2 = 1.0*BiGainEMG_N2; //coefficients for notch filter
+    const double BiGainEMG_L1= 0.001041;
+    const double EMGL1_a1 = -1.87506717001, EMGL1_a2 = 0.87923101463, EMGL1_b0 = 1.0*BiGainEMG_L1, EMGL1_b1 = 2.00000000000*BiGainEMG_L1, EMGL1_b2 = 1.0*BiGainEMG_L1; // coefficients for low-pass filter
+   
+    const double BiGainEMG_N1 = 1.0;
+    const double EMGN1_a1 = -1.58174308681, EMGN1_a2 = 0.96540248979, EMGN1_b0 = 1.0*BiGainEMG_N1, EMGN1_b1 = -1.61816176147*BiGainEMG_N1, EMGN1_b2 = 1.0*BiGainEMG_N1; //coefficients for notch filter
    
 // Filter creation    
     biquadFilter EMG_highpass1 (EMGH1_a1, EMGH1_a2, EMGH1_b0, EMGH1_b1, EMGH1_b2);        // creates the high pass filter
-    biquadFilter EMG_highpass2 (EMGH2_a1, EMGH2_a2, EMGH2_b0, EMGH2_b1, EMGH2_b2);
-    biquadFilter EMG_lowpass1 (EMGL1_a1, EMGL1_a2, EMGL1_b0, EMGL1_b1, EMGL1_b2);         // creates the low pass filter
-    biquadFilter EMG_lowpass2 (EMGL2_a1, EMGL2_a2, EMGL2_b0, EMGL2_b1, EMGL2_b2);  
+    biquadFilter EMG_lowpass1 (EMGL1_a1, EMGL1_a2, EMGL1_b0, EMGL1_b1, EMGL1_b2);         // creates the low pass filter 
     biquadFilter EMG_notch1 (EMGN1_a1, EMGN1_a2, EMGN1_b0, EMGN1_b1, EMGN1_b2);           // creates the notch filter
-    biquadFilter EMG_notch2 (EMGN2_a1, EMGN2_a2, EMGN2_b0, EMGN2_b1, EMGN2_b2);
-   
+    
     biquadFilter EMG_highpass1R (EMGH1_a1, EMGH1_a2, EMGH1_b0, EMGH1_b1, EMGH1_b2);        // creates the high pass filter
-    biquadFilter EMG_highpass2R (EMGH2_a1, EMGH2_a2, EMGH2_b0, EMGH2_b1, EMGH2_b2);
-    biquadFilter EMG_lowpass1R (EMGL1_a1, EMGL1_a2, EMGL1_b0, EMGL1_b1, EMGL1_b2);         // creates the low pass filter
-    biquadFilter EMG_lowpass2R (EMGL2_a1, EMGL2_a2, EMGL2_b0, EMGL2_b1, EMGL2_b2);  
+    biquadFilter EMG_lowpass1R (EMGL1_a1, EMGL1_a2, EMGL1_b0, EMGL1_b1, EMGL1_b2);         // creates the low pass filter  
     biquadFilter EMG_notch1R (EMGN1_a1, EMGN1_a2, EMGN1_b0, EMGN1_b1, EMGN1_b2);           // creates the notch filter
-    biquadFilter EMG_notch2R (EMGN2_a1, EMGN2_a2, EMGN2_b0, EMGN2_b1, EMGN2_b2);
+
             
 // EMG filtering function
 void EMGfilterLeft()
 {
     EMG_left_value = EMG_left.read();
     EMG_left_f1 = EMG_highpass1.step(EMG_left_value);
-    EMG_left_f2 = EMG_highpass2.step(EMG_left_f1);
-    EMG_left_abs = fabs(EMG_left_f2);
-    EMG_left_f3 = EMG_lowpass1.step(EMG_left_abs);
-    EMG_left_f4 = EMG_lowpass2.step(EMG_left_f3);
-    EMG_left_f5 = EMG_notch1.step(EMG_left_f4);
-    EMG_left_f6 = EMG_notch1.step(EMG_left_f5);
+    EMG_left_abs = fabs(EMG_left_f1);
+    EMG_left_f2 = EMG_lowpass1.step(EMG_left_abs);
+    EMG_left_f3 = EMG_notch1.step(EMG_left_f2);
 
 }
 
@@ -92,12 +76,9 @@
 {
     EMG_right_value = EMG_right.read();
     EMG_right_f1 = EMG_highpass1R.step(EMG_right_value);
-    EMG_right_f2 = EMG_highpass2R.step(EMG_right_f1);
-    EMG_right_abs = fabs(EMG_right_f2);
-    EMG_right_f3 = EMG_lowpass1R.step(EMG_right_abs);
-    EMG_right_f4 = EMG_lowpass2R.step(EMG_right_f3);
-    EMG_right_f5 = EMG_notch1R.step(EMG_right_f4);
-    EMG_right_f6 = EMG_notch1R.step(EMG_right_f5);
+    EMG_right_abs = fabs(EMG_right_f1);
+    EMG_right_f2 = EMG_lowpass1R.step(EMG_right_abs);
+    EMG_right_f3 = EMG_notch1R.step(EMG_right_f2);
 }
 
 // Movingaverage Filter
@@ -106,7 +87,7 @@
         EMG_left_MAF = (MAF_left[0]+MAF_left[1]+MAF_left[2]+MAF_left[3]+MAF_left[4]+MAF_left[5]+MAF_left[6]+MAF_left[7]+MAF_left[8]+MAF_left[9]+MAF_left[10]+MAF_left[11]+MAF_left[12]+MAF_left[13]+MAF_left[14]+MAF_left[15]+MAF_left[16]+MAF_left[17]+MAF_left[18]+MAF_left[19]+MAF_left[20]+MAF_left[21]+MAF_left[22]+MAF_left[23]+MAF_left[24]+MAF_left[25]+MAF_left[26]+MAF_left[27]+MAF_left[28]+MAF_left[29]+MAF_left[30]+MAF_left[31]+MAF_left[32]+MAF_left[33]+MAF_left[34]+MAF_left[35]+MAF_left[36]+MAF_left[37]+MAF_left[38]+MAF_left[39]+MAF_left[40]+MAF_left[41]+MAF_left[42]+MAF_left[43]+MAF_left[44]+MAF_left[45]+MAF_left[46]+MAF_left[47]+MAF_left[48]+MAF_left[49])/N;
         MAF_left[49] = MAF_left[48], MAF_left[48] = MAF_left[47], MAF_left[47] = MAF_left[46], MAF_left[46] = MAF_left[45], MAF_left[45] = MAF_left[44], MAF_left[44] = MAF_left[43], MAF_left[43] = MAF_left[42], MAF_left[42] = MAF_left[41], MAF_left[41] = MAF_left[40], MAF_left[40] = MAF_left[39], MAF_left[39] = MAF_left[38], MAF_left[38] = MAF_left[37], MAF_left[37] = MAF_left[36], MAF_left[36] = MAF_left[35], MAF_left[35] = MAF_left[34], MAF_left[34] = MAF_left[33], MAF_left[33] = MAF_left[32], MAF_left[32] = MAF_left[31], MAF_left[31] = MAF_left[30], MAF_left[30] = MAF_left[29], MAF_left[29] = MAF_left[28], MAF_left[28] = MAF_left[27], MAF_left[27] = MAF_left[26], MAF_left[26] = MAF_left[25];
         MAF_left[25] = MAF_left[24], MAF_left[24] = MAF_left[23], MAF_left[23] = MAF_left[22], MAF_left[22] = MAF_left[21], MAF_left[21] = MAF_left[20], MAF_left[20] = MAF_left[19], MAF_left[19] = MAF_left[18], MAF_left[18] = MAF_left[17], MAF_left[17] = MAF_left[16], MAF_left[16] = MAF_left[15], MAF_left[15] = MAF_left[14], MAF_left[14] = MAF_left[13], MAF_left[13] = MAF_left[12], MAF_left[12] = MAF_left[11], MAF_left[11] = MAF_left[10], MAF_left[10] = MAF_left[9], MAF_left[9] = MAF_left[8], MAF_left[8] = MAF_left[7], MAF_left[7] = MAF_left[6], MAF_left[6] = MAF_left[5], MAF_left[5] = MAF_left[4], MAF_left[4] = MAF_left[3], MAF_left[3] = MAF_left[2], MAF_left[2] = MAF_left[1], MAF_left[1] = MAF_left[0];
-        MAF_left[0] = EMG_left_f6;
+        MAF_left[0] = EMG_left_f3;
     }
     
     void MovingAverageFilterRight()
@@ -114,7 +95,7 @@
         EMG_right_MAF = (MAF_right[0]+MAF_right[1]+MAF_right[2]+MAF_right[3]+MAF_right[4]+MAF_right[5]+MAF_right[6]+MAF_right[7]+MAF_right[8]+MAF_right[9]+MAF_right[10]+MAF_right[11]+MAF_right[12]+MAF_right[13]+MAF_right[14]+MAF_right[15]+MAF_right[16]+MAF_right[17]+MAF_right[18]+MAF_right[19]+MAF_right[20]+MAF_right[21]+MAF_right[22]+MAF_right[23]+MAF_right[24]+MAF_right[25]+MAF_right[26]+MAF_right[27]+MAF_right[28]+MAF_right[29]+MAF_right[30]+MAF_right[31]+MAF_right[32]+MAF_right[33]+MAF_right[34]+MAF_right[35]+MAF_right[36]+MAF_right[37]+MAF_right[38]+MAF_right[39]+MAF_right[40]+MAF_right[41]+MAF_right[42]+MAF_right[43]+MAF_right[44]+MAF_right[45]+MAF_right[46]+MAF_right[47]+MAF_right[48]+MAF_right[49])/N;
         MAF_right[49] = MAF_right[48], MAF_right[48] = MAF_right[47], MAF_right[47] = MAF_right[46], MAF_right[46] = MAF_right[45], MAF_right[45] = MAF_right[44], MAF_right[44] = MAF_right[43], MAF_right[43] = MAF_right[42], MAF_right[42] = MAF_right[41], MAF_right[41] = MAF_right[40], MAF_right[40] = MAF_right[39], MAF_right[39] = MAF_right[38], MAF_right[38] = MAF_right[37], MAF_right[37] = MAF_right[36], MAF_right[36] = MAF_right[35], MAF_right[35] = MAF_right[34], MAF_right[34] = MAF_right[33], MAF_right[33] = MAF_right[32], MAF_right[32] = MAF_right[31], MAF_right[31] = MAF_right[30], MAF_right[30] = MAF_right[29], MAF_right[29] = MAF_right[28], MAF_right[28] = MAF_right[27], MAF_right[27] = MAF_right[26], MAF_right[26] = MAF_right[25];
         MAF_right[25] = MAF_right[24], MAF_right[24] = MAF_right[23], MAF_right[23] = MAF_right[22], MAF_right[22] = MAF_right[21], MAF_right[21] = MAF_right[20], MAF_right[20] = MAF_right[19], MAF_right[19] = MAF_right[18], MAF_right[18] = MAF_right[17], MAF_right[17] = MAF_right[16], MAF_right[16] = MAF_right[15], MAF_right[15] = MAF_right[14], MAF_right[14] = MAF_right[13], MAF_right[13] = MAF_right[12], MAF_right[12] = MAF_right[11], MAF_right[11] = MAF_right[10], MAF_right[10] = MAF_right[9], MAF_right[9] = MAF_right[8], MAF_right[8] = MAF_right[7], MAF_right[7] = MAF_right[6], MAF_right[6] = MAF_right[5], MAF_right[5] = MAF_right[4], MAF_right[4] = MAF_right[3], MAF_right[3] = MAF_right[2], MAF_right[2] = MAF_right[1], MAF_right[1] = MAF_right[0];
-        MAF_right[0] = EMG_right_f6;
+        MAF_right[0] = EMG_right_f3;
     }
 
 
@@ -122,9 +103,12 @@
     void ScopeSend()
     {
         scope.set(0, EMG_left_value);
-        scope.set(1, EMG_left_MAF);
-        scope.set(2, EMG_right_value);
-        scope.set(3, EMG_right_MAF);
+        scope.set(1, EMG_left_f1);
+        scope.set(2, EMG_left_abs);
+        scope.set(3, EMG_left_f2);
+        scope.set(4, EMG_left_f3);
+        scope.set(5, EMG_left_MAF);
+        
         scope.send();
     }
     
@@ -135,9 +119,19 @@
     ScopeTimer.attach(&ScopeSend, 0.002);
     MovingAverageLeft.attach(&MovingAverageFilterLeft, 0.005);
     MovingAverageRight.attach(&MovingAverageFilterRight, 0.005);
+    pc.baud(115200);
+    
+    ledG.write(1);
+    wait(20);
+    ledG.write(0);
+    wait(2);
+    Threshold1 = 0.75*EMG_left_MAF;
+    pc.printf("%f\n", Threshold1); 
+    ledG.write(1);
+    
     while(1) 
     {
-        if (EMG_left_f6 > Threshold1)
+        if (EMG_left_f3 > Threshold1)
         {
             led.write(0);
         }