Working moving average filter. IIR filters (low pass, high pass) still have to be implemented via biquad.

Dependencies:   HIDScope 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 "BiQuad.h"
00003 #include "HIDScope.h"
00004 
00005 //Define objects
00006 HIDScope scope(2); // We’re going to send 2 channels of data
00007 AnalogIn    emg0( A0 );
00008 AnalogIn    emg1( A1 );
00009 
00010 Ticker emgSampleTicker;
00011 
00012 // filter chains for high pass, low pass and notch filters
00013 BiQuadChain bqc;
00014 BiQuad bqlow( 0.0128, 0.0256, 0.0128, -1.6556, 0.7068);
00015 BiQuad bqhigh( 0.6458, -1.2917, 0.6458, -1.1620, 0.4213);
00016 BiQuad bqnotch( 0.5, 0, 0.5, 0, 0);
00017 volatile double y[50]= { };
00018 volatile int n = 0;
00019 volatile bool full = 0;
00020 
00021 void emgSample() {
00022     double emgRaw = emg0.read();
00023     scope.set(0,emgRaw);
00024     
00025     double emgFiltered = bqc.step( emgRaw );
00026     scope.set(1,emgFiltered);
00027     
00028     // moving average and rectifier
00029     y[n] = abs(emgFiltered - 0.45);
00030     double emgavg = 0;
00031     if(full == 0){
00032         for(int i=0;i<50;i++){
00033             emgavg = emgavg + y[i];
00034         }
00035         emgavg = emgavg/(n+1);
00036     }
00037     else{
00038         for(int i=0;i<50;i++){
00039             emgavg = emgavg + y[i];
00040         }
00041         emgavg = emgavg/50;    
00042     }
00043     n++;        
00044     if(n == 50){
00045         n = 0;
00046         full = 1;
00047     }
00048     scope.set(2,emgavg);
00049     scope.send();
00050     // do stuff with filtered point emgavg
00051 }
00052 
00053 
00054 int main()
00055 {   
00056     bqc.add( &bqlow ).add( &bqhigh ).add( &bqnotch );
00057     emgSampleTicker.attach( &emgSample, 0.01 );
00058     
00059     /*empty loop, sample() is executed periodically*/
00060     while(1) {}
00061 }