Biorobotics / Robot-Software

Dependencies:   HIDScope MODSERIAL QEI biquadFilter mbed Servo

Branch:
bla
Revision:
17:1f93c83e211f
Parent:
0:4cb1de41d049
Child:
25:734a26538711
--- a/help_functions/processing_chain_emg.h	Thu Oct 25 17:50:26 2018 +0000
+++ b/help_functions/processing_chain_emg.h	Thu Oct 25 22:01:12 2018 +0000
@@ -0,0 +1,59 @@
+#include "mbed.h"
+#include "BiQuad.h"
+
+//Define objects
+
+// filter chains for high pass, low pass and notch filters
+BiQuadChain bqc;
+
+// these are for sampling frequency 1000 Hz
+BiQuad bqlow( 0.1311, 0.2622, 0.1311, -0.7478, 0.2722);
+BiQuad bqhigh( 0.9150, -1.8299, 0.9150, -1.8227, 0.8372);
+BiQuad bqnotch( 0.9695, -1.8442, 0.9695, -1.8442, 0.9391);
+
+// old filters
+//BiQuad bqlow( 0.3767, 0.7533, 0.3767, 0.3172, 0.1894);
+//BiQuad bqhigh( 0.6458, -1.2917, 0.6458, -1.1620, 0.4213);
+//BiQuad bqnotch( 0.5, 0, 0.5, 0, 0);
+
+const int k = 300;      // number of moving average samples
+double movavg0[k]= { }; // array with samples emg0
+double movavg1[k]= { }; // array with samples emg1
+int n = 0;              // counter
+bool full = 0;          // boolean to check if we have already had 300 samples
+
+void processing_chain_emg(double &raw_emg_0, double &raw_emg_1, double &process_emg_0, double &process_emg_1) {
+    
+    // we first filter high pass and notch, then rectifier and after that low pass
+    movavg0[n] = bqlow.step(fabs(bqc.step( raw_emg_0 )));
+    movavg1[n] = bqlow.step(fabs(bqc.step( raw_emg_1 )));
+    
+    // moving average 
+    double emgavg0 = 0, emgavg1 = 0;
+    if(full == 0){
+        for(int i=0;i<k;i++){
+            emgavg0 = emgavg0 + movavg0[i];
+            emgavg1 = emgavg1 + movavg1[i];
+        }
+        emgavg0 = emgavg0/(n+1);
+        emgavg1 = emgavg1/(n+1);
+    }
+    else{
+        for(int i=0;i<k;i++){
+            emgavg0 = emgavg0 + movavg0[i];
+            emgavg1 = emgavg1 + movavg1[i];
+        }
+        emgavg0 = emgavg0/k;
+        emgavg1 = emgavg1/k;    
+    }
+    n++;        
+    if(n == k){
+        n = 0;      // reset counter when end array reached
+        full = 1;   // assures we are past 300 samples
+    }
+    
+    // outputs
+    process_emg_0 = emgavg0;
+    process_emg_1 = emgavg1;
+}
+