![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Emg filter function script for a uni project. Made by Teun van der Molen
Dependencies: HIDScope MODSERIAL mbed
Fork of frdm_EMG by
Diff: main.cpp
- Revision:
- 11:d8dd024f9784
- Parent:
- 10:b11eacb391ea
--- a/main.cpp Fri Sep 25 08:53:19 2015 +0000 +++ b/main.cpp Mon Oct 19 09:58:49 2015 +0000 @@ -2,20 +2,28 @@ #include "HIDScope.h" #include "math.h" // Define the HIDScope and Ticker object -HIDScope scope(3); +HIDScope scope(4); Ticker scopeTimer; -Ticker biquadTicker; +Ticker FilterTicker; +///////////////////////////////////////////////////MBED PORTS//////////////////////////////////////////////////////////////////// + DigitalOut light(LED2); DigitalIn knop(SW3); AnalogIn an_in(A0); //The input from the emg board, make sure that the jumper pin on the EMG board corresponds with the value in the brackets here!!! (see bb pptx for details on jumper placement, default is A0) - +/////////////////////////////////////////FILTER VALUES/////////////////////////////////////////////////////////////////////////////////// - light = 1; //Light is off by default +const float lag = 2500; //NO. of data points for moving average filter + float values [2500] = { }; // The array that stores the values for Movavg !!!!! the value between "[]" MUST BE THE SAME AS LAG!!!!!!!!! (needs to be a numerical value so can't fill in lag here :( + int n_avg = 0; // Counter for movavg + float y_avg = 0; // value for movavg + + double ffy = 0; //movavg filterd output + double c = 5; //calibration value (in volts) double n = 0; // counter for calibration bool calib = false; //changes into false when calibration is done (reset program for new calibration); @@ -49,17 +57,17 @@ //const double fb0 = 0.0036216786873927774,fb1 = 0.007243357374785555,fb2 = 0.0036216786873927774,fa1 = -1.8226935021735358,fa2 = 0.8371802169231065; ///Low-pass Fc = 20 Hz Fs = 1000Hz - +////////////////////////////////////////////////////////////////////HID SCOPE FUNCTION////////////////////////////////////////////////////////////////////////////// // The data read and send function for HIDscope (can send any value to the HIDscope for read out) void scopeSend() { - scope.set(0,c); + scope.set(0,an_in); scope.set(1,fy); - scope.set(2,an_in); - + scope.set(2,ffy); + scope.set(3,c); scope.send(); @@ -67,6 +75,9 @@ } + + ///////////////////////////////////////////////////////////////////////FILTER CASCADE//////////////////////////////////////////////////////////////////////// + void computeBiquad(){ //The filter function (is called at the same frequency of the scope function (not sure if timing is fully correct here) and does the filter calculations) double nv = an_in - na1*nv1 - na2*nv2; //notch filter at 50Hz for laptop adapter ny is the filtered output @@ -88,29 +99,53 @@ fv1=fv; //fy = abs(fy); //Optional final rectifier (default should be off) -} + + values [n_avg] = fy; //Moving Average filter (see filters to check lag etc.) + n_avg = n_avg + 1; + + if (n_avg == lag){ + + for (int n_divide = 0 ;n_divide <= lag; n_divide = n_divide + 1){ + y_avg = y_avg + values [n_divide]; + + } + + n_avg = 0; + ffy = y_avg/lag; + y_avg = 0; + } + + + +} //end compute biquad - + ////////////////////////////////////////////////////////////////////////MAIN FUNCTION///////////////////////////////////////////////////////////// int main() { + light = 1; //Light is off by default + + + /////////////////////////////////////////////////////////////////TICKERS/////////////////////////////////////////////////////////////////////// // Attach the data read and send function at 1000 Hz scopeTimer.attach_us(&scopeSend, 1e5); // Attach the filter calculation function at 1000 Hz - biquadTicker.attach(&computeBiquad,0.0001f); + FilterTicker.attach(&computeBiquad,0.0001f); while(1) { //the everlasting while loop - if (fy >= c and calib == false){ //if there is no calibration going on and the filtered emg is above calibration the light goes on + /////////////////////////////////////////////////////CALIBRATION AND TRIGGER //////////////////////////////////////////// + + if (ffy >= c and calib == false){ //if there is no calibration going on and the filtered emg is above calibration the light goes on light = 0; } @@ -124,7 +159,7 @@ for(;calib == true and n <= 20.00; n = n + 1) { //this loop ads the calibration value from the output of the filter, this also decides the time of calibration (see wait in combination with the n maximum) - c = c + fy; + c = (c + ffy); wait(0.1); }//end for @@ -133,7 +168,7 @@ light = 1; // light is off for a while after calibration calib = false; // shuts of calibration - c = c/n; //take the average of the emg output during calibration and set the calibration value + c = (c/n) * 0.3; //take the average of the emg output during calibration and set the calibration value wait(5); // you can only calibrate every this amount of seconds