Working with all filters, all filter results are low amplitude.

Dependencies:   HIDScope biquadFilter mbed

Fork of frdm_calibratie_maximum by Marieke M

Committer:
GerhardBerman
Date:
Fri Oct 21 15:13:03 2016 +0000
Revision:
4:9f4501eb226b
Parent:
3:339b19905505
Working with good filter function: total filtered signal is low

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Marieke 0:4d69864f1002 1 #include "mbed.h"
Marieke 2:27081b83a58e 2 #include "BiQuad.h"
Marieke 3:339b19905505 3 #include "HIDScope.h"
Marieke 3:339b19905505 4
Marieke 3:339b19905505 5 //#define SERIAL_BAUD 115200
Marieke 0:4d69864f1002 6
Marieke 0:4d69864f1002 7 AnalogIn emg0( A0 );
Marieke 0:4d69864f1002 8 AnalogIn emg1( A1 );
Marieke 3:339b19905505 9 //Serial pc(USBTX,USBRX);
Marieke 0:4d69864f1002 10
Marieke 0:4d69864f1002 11 Ticker sample_timer, average_timer, filter_timer, t;
Marieke 3:339b19905505 12 HIDScope scope( 6 );
Marieke 0:4d69864f1002 13 DigitalOut led1(LED_RED);
Marieke 0:4d69864f1002 14 DigitalOut led2(LED_BLUE);
Marieke 0:4d69864f1002 15
Marieke 0:4d69864f1002 16 volatile int time_passed = 0;
Marieke 0:4d69864f1002 17 volatile bool filter_timer_go=false;
Marieke 1:278677bb6b99 18
Marieke 2:27081b83a58e 19 double EMGright, EMGleft, inR;
Marieke 2:27081b83a58e 20 double averageEMGr =0;
Marieke 2:27081b83a58e 21 double averageEMGl =0;
GerhardBerman 4:9f4501eb226b 22 double outRnotch2 = 0;
GerhardBerman 4:9f4501eb226b 23 double outLnotch2 = 0;
Marieke 3:339b19905505 24
Marieke 2:27081b83a58e 25 void filter_timer_act(){filter_timer_go=true;};
Marieke 0:4d69864f1002 26
Marieke 3:339b19905505 27 BiQuadChain bcq1;
Marieke 3:339b19905505 28 BiQuadChain bcq2;
Marieke 0:4d69864f1002 29 // Notch filter wo=50; bw=wo/35
Marieke 3:339b19905505 30 BiQuad bq1(9.9821e-01,-1.9807e+00,9.9821e-01,-1.9807e+00,9.9642e-01);
Marieke 0:4d69864f1002 31 // High pass Butterworth filter 2nd order, Fc=10;
Marieke 3:339b19905505 32 BiQuad bq2(9.8239e-01,-1.9648e+00,9.8239e-01,-1.9645e+00,9.6508e-01);
Marieke 0:4d69864f1002 33 // Low pass Butterworth filter 2nd order, Fc = 8;
Marieke 3:339b19905505 34 BiQuad bq3(5.6248e-05,1.1250e-04,5.6248e-05,-1.9787e+00,9.7890e-01);
Marieke 0:4d69864f1002 35 // Low pass Butterworth filter 4th order, Fc = 450; plited up in 2x2nd order
Marieke 3:339b19905505 36 BiQuad bq4(1.0000e+00,2.0000e+00,1.0000e+00,-4.6382e-01,8.9354e-02);
Marieke 3:339b19905505 37 BiQuad bq5(1.0000e+00,2.0000e+00,1.0000e+00,-6.3254e-01,4.8559e-01);
Marieke 0:4d69864f1002 38
GerhardBerman 4:9f4501eb226b 39 // Low pass Butterworth filter 2th order, Fc = 450;
GerhardBerman 4:9f4501eb226b 40 BiQuad bq6(1.7509e-01,3.5018e-01,1.7509e-01,-5.1930e-01,2.1965e-01);
GerhardBerman 4:9f4501eb226b 41
GerhardBerman 4:9f4501eb226b 42
Marieke 0:4d69864f1002 43 void KeepTrackOfTime()
Marieke 0:4d69864f1002 44 {
Marieke 0:4d69864f1002 45 time_passed++;
Marieke 0:4d69864f1002 46 }
Marieke 0:4d69864f1002 47
Marieke 0:4d69864f1002 48 // In the following: R is used for right arm, L is used for left arm!
Marieke 3:339b19905505 49 void FilteredSample()
Marieke 0:4d69864f1002 50 {
Marieke 3:339b19905505 51 double inR = emg0.read();
Marieke 3:339b19905505 52 double inL = emg1.read();
Marieke 0:4d69864f1002 53
GerhardBerman 4:9f4501eb226b 54 double outRnotch = bcq1.step(inR); //notch and highpass for detrend
GerhardBerman 4:9f4501eb226b 55 double outRrect = fabs(outRnotch); //calculate abs with fabs
GerhardBerman 4:9f4501eb226b 56 double outRlow = bcq2.step(outRrect); //lowpass for envelope
GerhardBerman 4:9f4501eb226b 57
GerhardBerman 4:9f4501eb226b 58 double outLnotch = bcq1.step(inL); //notch and highpass for detrend
GerhardBerman 4:9f4501eb226b 59 double outLrect = fabs(outLnotch); //calculate abs with fabs
GerhardBerman 4:9f4501eb226b 60 double outLlow = bcq2.step(outLrect); //lowpass for envelope
Marieke 2:27081b83a58e 61
Marieke 3:339b19905505 62 //pc.printf("Detrend EMG = %f\n\r",inR);
Marieke 3:339b19905505 63 //pc.printf("EMG signal= %f\n\r",emg0.read());
Marieke 3:339b19905505 64 //pc.printf("average EMG right = %f\n\r",averageEMGr);
Marieke 2:27081b83a58e 65
GerhardBerman 4:9f4501eb226b 66 /*
GerhardBerman 4:9f4501eb226b 67 scope.set(0, inR); //emgsignal
GerhardBerman 4:9f4501eb226b 68 scope.set(1, outRnotch); //highpass+notch filtered
GerhardBerman 4:9f4501eb226b 69 scope.set(2, outRnotch2); //abscalc ifelse
GerhardBerman 4:9f4501eb226b 70 scope.set(3, outRrect2); //abscalc fabs
GerhardBerman 4:9f4501eb226b 71 scope.set(4, outRlow); //abs ifelse + lowpass
GerhardBerman 4:9f4501eb226b 72 scope.set(5, outRlow2); //abs fabs + lowpass
GerhardBerman 4:9f4501eb226b 73 */
Marieke 0:4d69864f1002 74
GerhardBerman 4:9f4501eb226b 75 scope.set(0, inR); //emgsignal right
GerhardBerman 4:9f4501eb226b 76 scope.set(1, outRlow); //emg filtered: highpass, notch, abs, lowpass
GerhardBerman 4:9f4501eb226b 77 scope.set(2, inL); //emgsignal left
GerhardBerman 4:9f4501eb226b 78 scope.set(3, outLlow); //emg filtered: highpass, notch, abs, lowpass
Marieke 0:4d69864f1002 79 scope.send();
Marieke 2:27081b83a58e 80 // To indicate that the function is working, the LED is toggled*/
Marieke 3:339b19905505 81 led2 = !led2;
Marieke 0:4d69864f1002 82 }
Marieke 0:4d69864f1002 83
Marieke 0:4d69864f1002 84 int main()
Marieke 0:4d69864f1002 85 {
Marieke 3:339b19905505 86 //pc.baud(115200);
Marieke 0:4d69864f1002 87 led1=1;
Marieke 0:4d69864f1002 88 led2=1;
Marieke 3:339b19905505 89 led2=!led2;
Marieke 3:339b19905505 90 //t.attach(&KeepTrackOfTime,1.0); //taking the time in seconds
Marieke 0:4d69864f1002 91
Marieke 3:339b19905505 92 bcq1.add(&bq1).add(&bq2);
GerhardBerman 4:9f4501eb226b 93 bcq2.add(&bq6).add(&bq3); //bcq2.add(&bq3).add(&bq4).add(&bq5); //450Hz Lowpass does not work
Marieke 3:339b19905505 94
Marieke 0:4d69864f1002 95
Marieke 3:339b19905505 96 filter_timer.attach(&filter_timer_act, 0.0004); //2500Hz (same as with filter coefficients on matlab!!! Thus adjust!)
Marieke 3:339b19905505 97 //pc.printf("\rMain-loop\n\r");
Marieke 2:27081b83a58e 98 //pc.printf("Detrend EMG = %f\n\r",inR);
Marieke 0:4d69864f1002 99
Marieke 3:339b19905505 100 while(1)
Marieke 3:339b19905505 101 {
Marieke 3:339b19905505 102 if (filter_timer_go){
Marieke 0:4d69864f1002 103 filter_timer_go=false;
Marieke 3:339b19905505 104 FilteredSample();}
Marieke 2:27081b83a58e 105 }
Marieke 0:4d69864f1002 106 }