ya kno it

Dependencies:   Filters

Committer:
Thijs12va
Date:
Tue Oct 31 09:42:15 2017 +0000
Revision:
3:cd11e6e02817
Parent:
2:02d31a0caac1
l

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Thijs12va 0:850bf2d90868 1 #include "emg.h"
Thijs12va 0:850bf2d90868 2
Thijs12va 0:850bf2d90868 3 emg_shield::emg_shield(PinName pin,float fs):emg_in(pin)
Thijs12va 0:850bf2d90868 4 {
Thijs12va 2:02d31a0caac1 5 float Ts = 1/fs;
Thijs12va 2:02d31a0caac1 6 float fc = 9.0;
Thijs12va 2:02d31a0caac1 7 highpass.a= 1/(2*3.1415*Ts*fc +1); // filter coefficient of high-pass filter
Thijs12va 3:cd11e6e02817 8 float lambda= 51.7126 *2.0*3.1415; // 50 Hz notch filter frequency in rad/s (Hz*2*pi)
Thijs12va 0:850bf2d90868 9 float b= 4.0 *2.0*3.1415; // 50 Hz notch filter bandwidth in rad/s (Hz*2*pi)
Thijs12va 0:850bf2d90868 10 notch50.lambda = lambda/(2.0*fs); // scaling for the 2/T multiplication which happens in the transform from s-domain to z-domain
Thijs12va 0:850bf2d90868 11 notch50.b = b/(2.0*fs); // scaling for the 2/T multiplication which happens in the transform from s-domain to z-domain
Thijs12va 3:cd11e6e02817 12 float lambda2=115.6328 *2.0*3.1415; // 100 Hz notch filter frequency in rad/s (Hz*2*pi)
Thijs12va 0:850bf2d90868 13 float b2= 4.0 *2.0*3.1415; // 100 Hz notch filter frequency in rad/s (Hz*2*pi)
Thijs12va 0:850bf2d90868 14 notch100.lambda = lambda2/(2.0*fs); // scaling for the 2/T multiplication which happens in the transform from s-domain to z-domain
Thijs12va 0:850bf2d90868 15 notch100.b = b2 /(2.0*fs); // scaling for the 2/T multiplication which happens in the transform from s-domain to z-domain
Thijs12va 2:02d31a0caac1 16 float wc = 5.0 *2.0*3.1415; // butterworth filter cutoff frequency in rad/s (Hz*2*pi)
Thijs12va 0:850bf2d90868 17 butter.w = wc/(2.0f*fs);
Thijs12va 0:850bf2d90868 18 Value=0;
Thijs12va 0:850bf2d90868 19 butter.yprev[0]=0;
Thijs12va 0:850bf2d90868 20 butter.yprev[1]=0;
Thijs12va 0:850bf2d90868 21 butter.xprev[2]=0;
Thijs12va 0:850bf2d90868 22 butter.xprev[0]=0;
Thijs12va 0:850bf2d90868 23 butter.xprev[1]=0;
Thijs12va 0:850bf2d90868 24 tick.attach(this,&emg_shield::tickFunction, 1.0f/fs);
Thijs12va 0:850bf2d90868 25 }
Thijs12va 0:850bf2d90868 26
Thijs12va 0:850bf2d90868 27 void emg_shield::tickFunction(){
Thijs12va 0:850bf2d90868 28 float raw_data = emg_in.read(); // obtain raw EMG data
Thijs12va 0:850bf2d90868 29
Thijs12va 0:850bf2d90868 30 float emg_notch = notch50.filter(raw_data); // notch filter to filter out the noise (especially from laptop charger)
Thijs12va 3:cd11e6e02817 31 emg_notch = notch100.filter(emg_notch);
Thijs12va 0:850bf2d90868 32
Thijs12va 0:850bf2d90868 33 float emg_hp = highpass.filter(emg_notch); // high-pass filter
Thijs12va 0:850bf2d90868 34
Thijs12va 0:850bf2d90868 35 float emg_hp_abs = fabs(emg_hp); // full wave rectification
Thijs12va 0:850bf2d90868 36
Thijs12va 0:850bf2d90868 37 //float emg_envelope = lowpass.filter(emg_hp_abs); // 2 1st order low-pass filters
Thijs12va 0:850bf2d90868 38 //emg_envelope = lowpass2.filter(emg_envelope);
Thijs12va 0:850bf2d90868 39 float emg_envelope = butter.filter(emg_hp_abs); // or 1 2nd order (butterworth) low-pass filter
Thijs12va 0:850bf2d90868 40
Thijs12va 0:850bf2d90868 41 Value = emg_envelope;
Thijs12va 0:850bf2d90868 42 }
Thijs12va 0:850bf2d90868 43
Thijs12va 0:850bf2d90868 44 float emg_shield::GetValue(){
Thijs12va 0:850bf2d90868 45 return Value;
Thijs12va 0:850bf2d90868 46 }
Thijs12va 0:850bf2d90868 47