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

Dependencies:   HIDScope MODSERIAL mbed

Fork of frdm_EMG by Teun van der Molen

Committer:
teunman
Date:
Mon Sep 21 08:23:37 2015 +0000
Revision:
2:ce5ead27b7cc
Parent:
1:75f61e111ed0
Child:
3:499c71ca30a0
Testing filters with one wire (DAC0_OUT) is output and (A0) is input. 21 september;

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 0:674026fdd982 5 HIDScope scope(1);
teunman 0:674026fdd982 6 Ticker scopeTimer;
teunman 1:75f61e111ed0 7 Ticker biquadTicker;
teunman 1:75f61e111ed0 8
teunman 1:75f61e111ed0 9 double v1=0, v2=0, u=0, y=0;
teunman 1:75f61e111ed0 10 const double
teunman 2:ce5ead27b7cc 11 b0 = 0.9999999999999999,b1 = 1.9999999999999998,b2 = 0.9999999999999999, a1 = 1.9999999999999998 ,a2 = 0.9999999999999998; //low-pass Fc = 50hz
teunman 2:ce5ead27b7cc 12 //b0 = 0.02008333102602092 ,b1 = 0.04016666205204184 ,b2 = 0.02008333102602092, a1 = -1.5610153912536877 ,a2 = 0.6413487153577715; //low-pass Fc = 5hz
teunman 2:ce5ead27b7cc 13 //b0 = 0.8005910266528649,b1 = -1.6011820533057297,b2 = 0.8005910266528649,a1 = -1.5610153912536877,a2 = 0.6413487153577715; //high-pass Fc = 5hz
teunman 1:75f61e111ed0 14
teunman 1:75f61e111ed0 15 void computeBiquad(){
teunman 1:75f61e111ed0 16 double v = u - a1*v1 - a2*v2;
teunman 1:75f61e111ed0 17 y= b0*v + b1*v1 +b2*v2;
teunman 1:75f61e111ed0 18 v2=v1;
teunman 1:75f61e111ed0 19 v1=v;
teunman 1:75f61e111ed0 20 }
teunman 0:674026fdd982 21 // Read the analog input
teunman 0:674026fdd982 22 AnalogIn an_in(A0);
teunman 0:674026fdd982 23 AnalogOut an_out(DAC0_OUT);
teunman 0:674026fdd982 24 // The data read and send function
teunman 0:674026fdd982 25 void scopeSend()
teunman 0:674026fdd982 26 {
teunman 0:674026fdd982 27 scope.set(0,an_in.read());
teunman 0:674026fdd982 28 scope.send();
teunman 0:674026fdd982 29 }
teunman 0:674026fdd982 30
teunman 0:674026fdd982 31 int main()
teunman 0:674026fdd982 32 {
teunman 0:674026fdd982 33 // Attach the data read and send function at 100 Hz
teunman 0:674026fdd982 34 scopeTimer.attach_us(&scopeSend, 1e4);
teunman 1:75f61e111ed0 35 //biquadTicker.attach(&computeBiquad,0.01f);
teunman 0:674026fdd982 36 float i = 1;
teunman 0:674026fdd982 37 while(1) {
teunman 0:674026fdd982 38 // sinewave output
teunman 2:ce5ead27b7cc 39 float u = (0.25 + 0.1*sin(i) + (0.25 + 0.1*sin(100*i)));
teunman 1:75f61e111ed0 40 double v = u - a1*v1 - a2*v2;
teunman 1:75f61e111ed0 41 y= b0*v + b1*v1 +b2*v2;
teunman 1:75f61e111ed0 42 v2=v1;
teunman 1:75f61e111ed0 43 v1=v;
teunman 1:75f61e111ed0 44
teunman 0:674026fdd982 45 i = i+0.01;
teunman 1:75f61e111ed0 46 an_out = y;
teunman 1:75f61e111ed0 47 wait(0.01);
teunman 1:75f61e111ed0 48
teunman 0:674026fdd982 49 }
teunman 0:674026fdd982 50 }