EMG filtering script

Dependencies:   MODSERIAL mbed

Committer:
s1204246
Date:
Wed Jul 09 14:34:33 2014 +0000
Revision:
0:dca649a31d1c
EMG filtering Wouter Doppenberg

Who changed what in which revision?

UserRevisionLine numberNew contents of line
s1204246 0:dca649a31d1c 1 #include "mbed.h"
s1204246 0:dca649a31d1c 2 #include "MODSERIAL.h"
s1204246 0:dca649a31d1c 3
s1204246 0:dca649a31d1c 4 //Define objects
s1204246 0:dca649a31d1c 5 AnalogIn emg0(PTB0); //Analog input
s1204246 0:dca649a31d1c 6 DigitalOut blue(LED_BLUE); //Digi output
s1204246 0:dca649a31d1c 7 Ticker timer;
s1204246 0:dca649a31d1c 8 MODSERIAL pc(USBTX,USBRX);
s1204246 0:dca649a31d1c 9
s1204246 0:dca649a31d1c 10 double numh1,numh2,denh2; //High pass filter coeffs
s1204246 0:dca649a31d1c 11 double numl1,numl2,numl3,numl4,denl2,denl3,denl4; //Low pass filter coeffs
s1204246 0:dca649a31d1c 12 double numn1,numn2,numn3,denn2,denn3; //Notch filter coeffs
s1204246 0:dca649a31d1c 13 double raw,xbr,x1br,hbr,h1br,h2br,nbr,n1br,n2br,yabsbr,yabs1br,yabs2br,yabs3br,lbr,l1br,l2br,l3br,signal,emggain; //Signal intermediates
s1204246 0:dca649a31d1c 14
s1204246 0:dca649a31d1c 15 void looper()
s1204246 0:dca649a31d1c 16 {
s1204246 0:dca649a31d1c 17 /*put raw emg value both in red and in emg_value*/
s1204246 0:dca649a31d1c 18 blue = raw = emg0.read();
s1204246 0:dca649a31d1c 19 xbr=(raw-0.5)*2.0;
s1204246 0:dca649a31d1c 20 hbr=xbr*numh1+x1br*numh2-h1br*denh2;
s1204246 0:dca649a31d1c 21 nbr=hbr*numn1+h1br*numn2+h2br*numn3-n1br*denn2-n2br*denn3;
s1204246 0:dca649a31d1c 22 yabsbr=abs(nbr);
s1204246 0:dca649a31d1c 23 lbr=yabsbr*numl1+yabs1br*numl2+yabs2br*numl3+yabs3br*numl4-l1br*denl2-l2br*denl3-l3br*denl4;
s1204246 0:dca649a31d1c 24 x1br=xbr;
s1204246 0:dca649a31d1c 25 h2br=h1br;
s1204246 0:dca649a31d1c 26 h1br=hbr;
s1204246 0:dca649a31d1c 27 n2br=n1br;
s1204246 0:dca649a31d1c 28 n1br=nbr;
s1204246 0:dca649a31d1c 29 yabs3br=yabs2br;
s1204246 0:dca649a31d1c 30 yabs2br=yabs1br;
s1204246 0:dca649a31d1c 31 yabs1br=yabsbr;
s1204246 0:dca649a31d1c 32 l3br=l2br;
s1204246 0:dca649a31d1c 33 l2br=l1br;
s1204246 0:dca649a31d1c 34 l1br=lbr;
s1204246 0:dca649a31d1c 35 signal=lbr*emggain;
s1204246 0:dca649a31d1c 36 /*send value to PC. use 6 digits after decimal sign*/
s1204246 0:dca649a31d1c 37 pc.printf("%f \n",signal);
s1204246 0:dca649a31d1c 38 /**When not using the LED, the above could also have been done this way:
s1204246 0:dca649a31d1c 39 * pc.printf("%.6\n", emg0.read());
s1204246 0:dca649a31d1c 40 */
s1204246 0:dca649a31d1c 41 }
s1204246 0:dca649a31d1c 42
s1204246 0:dca649a31d1c 43 int main()
s1204246 0:dca649a31d1c 44
s1204246 0:dca649a31d1c 45 {
s1204246 0:dca649a31d1c 46 x1br=0;
s1204246 0:dca649a31d1c 47 h2br=0;
s1204246 0:dca649a31d1c 48 h1br=0;
s1204246 0:dca649a31d1c 49 n2br=0;
s1204246 0:dca649a31d1c 50 n1br=0;
s1204246 0:dca649a31d1c 51 yabs3br=0;
s1204246 0:dca649a31d1c 52 yabs2br=0;
s1204246 0:dca649a31d1c 53 yabs1br=0;
s1204246 0:dca649a31d1c 54 l3br=0;
s1204246 0:dca649a31d1c 55 l2br=0;
s1204246 0:dca649a31d1c 56 l1br=0;
s1204246 0:dca649a31d1c 57
s1204246 0:dca649a31d1c 58 //Filtercoëfficienten.
s1204246 0:dca649a31d1c 59 //High pass, 30Hz, 1e orde, 4 ms.
s1204246 0:dca649a31d1c 60 numh1=0.839799649112263;
s1204246 0:dca649a31d1c 61 numh2=-0.839799649112263;
s1204246 0:dca649a31d1c 62 //denh1=1;
s1204246 0:dca649a31d1c 63 denh2=-0.679599298224526;
s1204246 0:dca649a31d1c 64
s1204246 0:dca649a31d1c 65 //Notch filter, 50 Hz, -35 dB
s1204246 0:dca649a31d1c 66 numn1=0.991103635646810;
s1204246 0:dca649a31d1c 67 numn2=-1.603639368850131;
s1204246 0:dca649a31d1c 68 numn3=0.991103635646810;
s1204246 0:dca649a31d1c 69 //denn1=1;
s1204246 0:dca649a31d1c 70 denn2=-1.603639368850131;
s1204246 0:dca649a31d1c 71 denn3=0.982207271293620;
s1204246 0:dca649a31d1c 72
s1204246 0:dca649a31d1c 73 //Low pass, 3 Hz, 3e orde, 4 ms.
s1204246 0:dca649a31d1c 74 numl1=0.000001935454105;
s1204246 0:dca649a31d1c 75 numl2=0.000005806362315;
s1204246 0:dca649a31d1c 76 numl3=0.000005806362315;
s1204246 0:dca649a31d1c 77 numl4=0.000001935454105;
s1204246 0:dca649a31d1c 78 //denl1=1;
s1204246 0:dca649a31d1c 79 denl2=-2.949735839706348;
s1204246 0:dca649a31d1c 80 denl3=2.900726988355438;
s1204246 0:dca649a31d1c 81 denl4=-0.950975665016249;
s1204246 0:dca649a31d1c 82 //emg gain
s1204246 0:dca649a31d1c 83 emggain=5;
s1204246 0:dca649a31d1c 84
s1204246 0:dca649a31d1c 85 /*setup baudrate. Choose the same in your program on PC side*/
s1204246 0:dca649a31d1c 86 pc.baud(115200);
s1204246 0:dca649a31d1c 87 /*set the period for the PWM to the red LED*/
s1204246 0:dca649a31d1c 88 //red.period_ms(2);
s1204246 0:dca649a31d1c 89 /**Here you attach the 'void looper(void)' function to the Ticker object
s1204246 0:dca649a31d1c 90 * The looper() function will be called every 0.001 seconds.
s1204246 0:dca649a31d1c 91 * Please mind that the parentheses after looper are omitted when using attach.
s1204246 0:dca649a31d1c 92 */
s1204246 0:dca649a31d1c 93 timer.attach(looper, 0.001);
s1204246 0:dca649a31d1c 94 while(1) //Loop
s1204246 0:dca649a31d1c 95 {
s1204246 0:dca649a31d1c 96 /*Empty!*/
s1204246 0:dca649a31d1c 97 /*Everything is handled by the interrupt routine now!*/
s1204246 0:dca649a31d1c 98 }
s1204246 0:dca649a31d1c 99 }