ya kno it
emg.cpp@0:850bf2d90868, 2017-10-25 (annotated)
- Committer:
- Thijs12va
- Date:
- Wed Oct 25 12:35:29 2017 +0000
- Revision:
- 0:850bf2d90868
- Child:
- 2:02d31a0caac1
a
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:850bf2d90868 | 5 | highpass.a=0.05; // filter coefficient of high-pass filter |
Thijs12va | 0:850bf2d90868 | 6 | float lambda= 51.6 *2.0*3.1415; // 50 Hz notch filter frequency in rad/s (Hz*2*pi) |
Thijs12va | 0:850bf2d90868 | 7 | float b= 4.0 *2.0*3.1415; // 50 Hz notch filter bandwidth in rad/s (Hz*2*pi) |
Thijs12va | 0:850bf2d90868 | 8 | 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 | 9 | notch50.b = b/(2.0*fs); // scaling for the 2/T multiplication which happens in the transform from s-domain to z-domain |
Thijs12va | 0:850bf2d90868 | 10 | float lambda2=103.2 *2.0*3.1415; // 100 Hz notch filter frequency in rad/s (Hz*2*pi) |
Thijs12va | 0:850bf2d90868 | 11 | float b2= 4.0 *2.0*3.1415; // 100 Hz notch filter frequency in rad/s (Hz*2*pi) |
Thijs12va | 0:850bf2d90868 | 12 | 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 | 13 | notch100.b = b2 /(2.0*fs); // scaling for the 2/T multiplication which happens in the transform from s-domain to z-domain |
Thijs12va | 0:850bf2d90868 | 14 | float wc = 10 *2.0*3.1415; // butterworth filter cutoff frequency in rad/s (Hz*2*pi) |
Thijs12va | 0:850bf2d90868 | 15 | butter.w = wc/(2.0f*fs); |
Thijs12va | 0:850bf2d90868 | 16 | Value=0; |
Thijs12va | 0:850bf2d90868 | 17 | butter.yprev[0]=0; |
Thijs12va | 0:850bf2d90868 | 18 | butter.yprev[1]=0; |
Thijs12va | 0:850bf2d90868 | 19 | butter.xprev[2]=0; |
Thijs12va | 0:850bf2d90868 | 20 | butter.xprev[0]=0; |
Thijs12va | 0:850bf2d90868 | 21 | butter.xprev[1]=0; |
Thijs12va | 0:850bf2d90868 | 22 | tick.attach(this,&emg_shield::tickFunction, 1.0f/fs); |
Thijs12va | 0:850bf2d90868 | 23 | } |
Thijs12va | 0:850bf2d90868 | 24 | |
Thijs12va | 0:850bf2d90868 | 25 | void emg_shield::tickFunction(){ |
Thijs12va | 0:850bf2d90868 | 26 | float raw_data = emg_in.read(); // obtain raw EMG data |
Thijs12va | 0:850bf2d90868 | 27 | |
Thijs12va | 0:850bf2d90868 | 28 | float emg_notch = notch50.filter(raw_data); // notch filter to filter out the noise (especially from laptop charger) |
Thijs12va | 0:850bf2d90868 | 29 | |
Thijs12va | 0:850bf2d90868 | 30 | float emg_hp = highpass.filter(emg_notch); // high-pass filter |
Thijs12va | 0:850bf2d90868 | 31 | |
Thijs12va | 0:850bf2d90868 | 32 | float emg_hp_abs = fabs(emg_hp); // full wave rectification |
Thijs12va | 0:850bf2d90868 | 33 | |
Thijs12va | 0:850bf2d90868 | 34 | //float emg_envelope = lowpass.filter(emg_hp_abs); // 2 1st order low-pass filters |
Thijs12va | 0:850bf2d90868 | 35 | //emg_envelope = lowpass2.filter(emg_envelope); |
Thijs12va | 0:850bf2d90868 | 36 | float emg_envelope = butter.filter(emg_hp_abs); // or 1 2nd order (butterworth) low-pass filter |
Thijs12va | 0:850bf2d90868 | 37 | |
Thijs12va | 0:850bf2d90868 | 38 | Value = emg_envelope; |
Thijs12va | 0:850bf2d90868 | 39 | } |
Thijs12va | 0:850bf2d90868 | 40 | |
Thijs12va | 0:850bf2d90868 | 41 | float emg_shield::GetValue(){ |
Thijs12va | 0:850bf2d90868 | 42 | return Value; |
Thijs12va | 0:850bf2d90868 | 43 | } |
Thijs12va | 0:850bf2d90868 | 44 |