a biquad working
Dependencies: HIDScope mbed QEI
Diff: main.cpp
- Revision:
- 0:fc207e186e8b
- Child:
- 1:ee877d784c40
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Oct 27 09:57:27 2015 +0000 @@ -0,0 +1,136 @@ +#include "mbed.h" +//#include "read_filter_emg.h" +//included for fabs() function +#include <math.h> +#include "HIDScope.h" +#include <iostream> + +Ticker HIDScope_timer; +Ticker Filteren_timer; +HIDScope scope(2); + +// defining flags +volatile bool Flag_filteren = false; +volatile bool Flag_HIDScope = false; + +// making function flags. +void Go_flag_filteren() +{ + Flag_filteren = true; +} + +void Go_flag_HIDScope() +{ + Flag_HIDScope = true; +} + +AnalogIn analog_emg_left(A0); +//AnalogIn analog_emg_right(A1); +double input = 0; +double filter_signal_hid = 0; +//double input_right = 0; + +double v1=0; +double v2=0; +//double v1_right=0; +//double v2_right=0; + +double filter_left; +double filter_right; + +//general biquad filter that can be called in all the filter functions +double biquad(double u, double &v1, double &v2, const double a1, + const double a2, const double b0, const double b1, const double b2) +{ + double v = u - a1*v1 - a2*v2; + double y = b0*v + b1*v1 + b2*v2; + //values of v2 and v1 are updated, as they are passed by reference + //they update globally + v2 = v1; + v1 = v; + return y; +} + + + +/* lowpass filter consists of three cascaded biquads +below the coefficients for those three biquads */ +//first high pass biquad +const double lowp1_a1 = -1.75927361117; +const double lowp1_a2 = 0.78528639300; +const double lowp1_b0 = 1.00000000000; +const double lowp1_b1 = 2.00000000000; +const double lowp1_b2 = 1.00000000000; + +/* +//second high pass biquad +const double lowp2_a1 = -1.16338171052; +const double lowp2_a2 = 0.42191097989; +const double lowp2_b0 = 1.00000000000; +const double lowp2_b1 = 2.00000000000; +const double lowp2_b2 = 1.00000000000; + +*/ + + + + + + + +//highpass + + + +/* +//rectifier +double rectify(double y6) +{ + y6 = fabs(y6); + return y6; +} +*/ +//lowpass + + + + + // double y8 = biquad(y7, v1, v2, lowp2_a1, lowp2_a2, lowp2_b0, lowp2_b1, lowp2_b2); + + + + + + +double Filteren() +{ + input = analog_emg_left.read(); + //input = input-0.45; //FIRST SUBTRACT MEAN THEN FILTER + //input_right = analog_emg_right.read(); + double filter_signal = biquad(input, v1, v2, lowp1_a1, lowp1_a2, lowp1_b0, lowp1_b1, lowp1_b2); + //filter_right = filter(input_right, v1_right, v2_right); + +return(filter_signal); +} +void HIDScope_kijken() +{ + scope.set(0, input); + scope.set(1, filter_signal_hid); + scope.send(); +} +int main() +{ + HIDScope_timer.attach(&Go_flag_HIDScope, 0.002); + Filteren_timer.attach(&Go_flag_filteren,0.004); + while(1){ + if(Flag_filteren) { + Flag_filteren = false; + filter_signal_hid = Filteren(); + } + + if(Flag_HIDScope) { + Flag_HIDScope = false; + HIDScope_kijken(); + } + } +} \ No newline at end of file