Emg filter function script for a uni project. Made by Teun van der Molen

Dependencies:   HIDScope MODSERIAL mbed

Committer:
teunman
Date:
Mon Sep 21 09:57:12 2015 +0000
Revision:
4:1baefd1397d6
Parent:
3:499c71ca30a0
Child:
5:56725d9362ee
low-pass (fc = 5hz)  and high pass (fc = 1 hz) filter in cascade working. Using A0 to read signal and sending to HIDscopen;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
teunman 0:674026fdd982 1 #include "mbed.h"
teunman 0:674026fdd982 2 #include "HIDScope.h"
teunman 0:674026fdd982 3 #include "math.h"
teunman 0:674026fdd982 4 // Define the HIDScope and Ticker object
teunman 4:1baefd1397d6 5 HIDScope scope(3);
teunman 0:674026fdd982 6 Ticker scopeTimer;
teunman 1:75f61e111ed0 7 Ticker biquadTicker;
teunman 1:75f61e111ed0 8
teunman 4:1baefd1397d6 9 double v1=0, v2=0, u=0, y=0, fy=0, fv1=0, fv2=0, fu=0;
teunman 1:75f61e111ed0 10
teunman 4:1baefd1397d6 11 //const double b0 = 0.9999999999999999,b1 = 1.9999999999999998,b2 = 0.9999999999999999, a1 = 1.9999999999999998 ,a2 = 0.9999999999999998; //low-pass Fc = 50hz
teunman 4:1baefd1397d6 12 const double fb0 = 0.02008333102602092 ,fb1 = 0.04016666205204184 ,fb2 = 0.02008333102602092, fa1 = -1.5610153912536877 ,fa2 = 0.6413487153577715; //low-pass Fc = 5hz
teunman 4:1baefd1397d6 13 //const double b0 = 0.8005910266528649,b1 = -1.6011820533057297,b2 = 0.8005910266528649,a1 = -1.5610153912536877,a2 = 0.6413487153577715; //high-pass Fc = 5hz
teunman 4:1baefd1397d6 14 //const double b0 = 0.007820199259120319,b1 = 0.015640398518240638,b2 = 0.007820199259120319,a1 = -1.7347238224240125,a2 = 0.7660046194604936; //low-pass Fc = 3hz
teunman 4:1baefd1397d6 15 //const double b0 = 0.0009446914586925257,b1 = 0.0018893829173850514,b2 = 0.0009446914586925257,a1 = -1.911196288237583,a2 = 0.914975054072353; //low-pass Fc = 1hz
teunman 4:1baefd1397d6 16 const double b0 = 0.956542835577484, b1 = -1.913085671154968, b2 = 0.956542835577484, a1 = -1.911196288237583, a2 = 0.914975054072353; //high-pass Fc = 1hz
teunman 3:499c71ca30a0 17
teunman 3:499c71ca30a0 18
teunman 3:499c71ca30a0 19
teunman 3:499c71ca30a0 20
teunman 3:499c71ca30a0 21
teunman 3:499c71ca30a0 22 // Read the analog input
teunman 3:499c71ca30a0 23 AnalogIn an_in(A0);
teunman 3:499c71ca30a0 24
teunman 3:499c71ca30a0 25 AnalogOut an_out(DAC0_OUT);
teunman 3:499c71ca30a0 26 // The data read and send function
teunman 3:499c71ca30a0 27 void scopeSend()
teunman 3:499c71ca30a0 28 {
teunman 3:499c71ca30a0 29 scope.set(0,y);
teunman 4:1baefd1397d6 30 scope.set(1,fy);
teunman 4:1baefd1397d6 31 scope.set(2,an_in);
teunman 3:499c71ca30a0 32
teunman 3:499c71ca30a0 33 scope.send();
teunman 3:499c71ca30a0 34 }
teunman 3:499c71ca30a0 35
teunman 3:499c71ca30a0 36 void computeBiquad(){
teunman 3:499c71ca30a0 37 double v = an_in - a1*v1 - a2*v2;
teunman 1:75f61e111ed0 38 y= b0*v + b1*v1 +b2*v2;
teunman 1:75f61e111ed0 39 v2=v1;
teunman 1:75f61e111ed0 40 v1=v;
teunman 4:1baefd1397d6 41
teunman 4:1baefd1397d6 42 double fv = y - fa1*fv1 - fa2*fv2;
teunman 4:1baefd1397d6 43 fy= fb0*fv + fb1*fv1 + fb2*fv2;
teunman 4:1baefd1397d6 44 fv2=fv1;
teunman 4:1baefd1397d6 45 fv1=fv;
teunman 1:75f61e111ed0 46 }
teunman 3:499c71ca30a0 47
teunman 0:674026fdd982 48
teunman 0:674026fdd982 49 int main()
teunman 0:674026fdd982 50 {
teunman 0:674026fdd982 51 // Attach the data read and send function at 100 Hz
teunman 0:674026fdd982 52 scopeTimer.attach_us(&scopeSend, 1e4);
teunman 3:499c71ca30a0 53 biquadTicker.attach(&computeBiquad,0.01f);
teunman 3:499c71ca30a0 54 //float i = 1;
teunman 0:674026fdd982 55 while(1) {
teunman 4:1baefd1397d6 56
teunman 0:674026fdd982 57 }
teunman 0:674026fdd982 58 }