EMG signalen uitlezen met filters

Dependencies:   HIDScope biquadFilter mbed

Revision:
1:0d62559f43af
Parent:
0:5759df060d75
Child:
2:5df98e0832d6
--- a/main.cpp	Thu Oct 27 10:59:47 2016 +0000
+++ b/main.cpp	Thu Oct 27 14:43:21 2016 +0000
@@ -1,40 +1,54 @@
 #include "mbed.h"
 #include "HIDScope.h"
-
+#include "BiQuad.h"
 
-//Define objects
+//Define EMG input 
 AnalogIn    emg0( A0 );
 AnalogIn    emg1( A1 );
 AnalogIn    emg2( A2 );
 
 Ticker      sample_timer;
-HIDScope    scope( 3 );     // Instantize a 3-channel HIDScope object
-DigitalOut  led(LED1);      // Instantize the timer for sending data to the PC 
+HIDScope    scope( 3 );     // 3-channel HIDScope object
+
+// Filter coordinates of lowpass/highpass filter before rectifier and lowpassfilter for the envelope
+const double b0_low = 0.2929, b1_low = 0.5858, b2_low = 0.2929, a1_low = 0, a2_low = 0.1716;
+const double b0_high = 0.9912, b1_high = -1.9823, b2_high = 0.9912, a1_high = -1.9822, a2_high = 0.9824;
+const double b0_envelope = 6.1006E-5, b1_envelope = 1.2201E-4, b2_envelope = 6.1006E-5, a1_envelope = -1.9776, a2_envelope = 0.9780;
+
+double y1, y2, y3;          // Gefilterde output
 
-/** Sample function
- * this function samples the emg and sends it to HIDScope
- **/
-void sample()
+// Biquad filters (lowpass and highpass become a chain in the int main)
+BiQuad lowpass(b0_low, b1_low , b2_low, a1_low, a2_low);
+BiQuad highpass(b0_high, b1_high , b2_high, a1_high, a2_high);
+BiQuad envelope(b0_envelope, b1_envelope , b2_envelope, a1_envelope, a2_envelope);
+BiQuadChain bandpass;
+
+// Sample function, this function samples the emg and sends it to HIDScope
+
+void filtering()
 {
-    /* Set the sampled emg values in channel 0/1/2 (the first/second/third channel) in the 'HIDScope' instance named 'scope' */
-    scope.set(0, emg0.read() );
-    scope.set(1, emg1.read() );
-    scope.set(2, emg2.read() );
+   y1 = bandpass.step(emg0);
+   y2 = bandpass.step(emg1);
+   y3 = bandpass.step(emg2);
+   y1 = fabs(y1);
+   y2 = fabs(y2);
+   y3 = fabs(y3);
+   y1 = envelope.step(y1); 
+   y2 = envelope.step(y2); 
+   y3 = envelope.step(y3); 
     
+    // Set the sampled and filtered emg values in channel 0/1/2 in the 'HIDScope' instance named 'scope' 
+    scope.set(0, y1 );
+    scope.set(1, y2 );
+    scope.set(2, y3 );
     
-    /*  Finally, send all channels to the PC at once */
-    scope.send();
-    /* To indicate that the function is working, the LED is toggled */
-    led = !led;
+    scope.send();                           // Sends all channels to the PC at once
 }
 
 int main()
 {   
-    /**Attach the 'sample' function to the timer 'sample_timer'.
-    * this ensures that 'sample' is executed every... 0.001 seconds = 1000 Hz
-    */
-    sample_timer.attach(&sample, 0.001);
-
-    /*empty loop, sample() is executed periodically*/
-    while(1) {}
+    bandpass.add(&lowpass).add(&highpass);
+    sample_timer.attach(&filtering, 0.001);    // Attach the 'sample' function to the timer 'sample_timer'. This ensures that 'sample' is executed every 0.001 seconds = 1000 Hz
+    
+    while(1) {}                             // Empty loop, sample() is executed periodically
 }
\ No newline at end of file