Working filters

Dependencies:   HIDScope MODSERIAL biquadFilter mbed

Fork of EMG by Tom Tom

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "HIDScope.h"
00003 #include "BiQuad.h"
00004 #include "MODSERIAL.h"
00005 
00006 //Define objects
00007 AnalogIn    emg1_in( A0 );
00008 AnalogIn    emg2_in( A1 );
00009 
00010 Ticker      sample_timer;
00011 HIDScope    scope( 3 );
00012 DigitalOut  red(LED_RED);
00013 DigitalOut  blue(LED_BLUE);
00014 DigitalOut  green(LED_GREEN);
00015 DigitalOut  emg1_out( D8 );
00016 DigitalOut  emg2_out( D9 );
00017 MODSERIAL   pc(USBTX, USBRX);
00018 
00019 
00020 // EMG variables
00021 double emg1;
00022 double emg1highfilter;
00023 double emg1notchfilter;
00024 double emg1abs;
00025 double emg1lowfilter;
00026 double emgpeak;
00027 
00028 double emg2;
00029 double emg2highfilter;
00030 double emg2notchfilter;
00031 double emg2abs;
00032 double emg2lowfilter;
00033 
00034 // Filter settings
00035 BiQuad filterhigh(9.704e-01,-1.9408,9.704e-01,-1.9389,9.427e-01);
00036 BiQuad filternotch(9.495e-01,-1.8062,9.495e-01,-1.8062,8.992e-01);
00037 BiQuad filterlow(1.439e-02,2.794e-02,1.397e-02,-1.7229,7.788e-01);
00038 BiQuad filterpeak(1.0878,-1.950,8.771e-01,-1.95032,9.5499e-01);
00039 
00040 // Filtering
00041 void filter() {
00042     emg1 = emg1_in.read();
00043     emg1highfilter = filterhigh.step(emg1);
00044     emg1notchfilter = filternotch.step(emg1highfilter);
00045     emg1abs = fabs(emg1notchfilter);
00046     emg1lowfilter = filterlow.step(emg1abs);
00047     emgpeak = filterpeak.step(emg1lowfilter);
00048 
00049     emg2 = emg2_in.read();
00050     emg2highfilter = filterhigh.step(emg2);
00051     emg2notchfilter = filternotch.step(emg2highfilter);
00052     emg2abs = fabs(emg2notchfilter);
00053     emg2lowfilter = filterlow.step(emg2abs);
00054 
00055     /* Set the sampled emg values in channel 0 (the first channel) and 1 (the second channel) in the 'HIDScope' instance named 'scope' */
00056     scope.set(0, emg1_in.read() );
00057     scope.set(1, emg1lowfilter );
00058     scope.set(2, emgpeak );
00059     /*  Finally, send all channels to the PC at once */
00060     scope.send();
00061     /* To indicate that the function is working, the LED is toggled */
00062     green = !green;
00063 }
00064 
00065 int main()
00066 {   
00067 
00068     sample_timer.attach(&filter, 0.001);
00069 
00070     while(1) {}
00071 }