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
main.cpp@21:3ac3c2e9d9ae, 2018-10-12 (annotated)
- Committer:
- MaikOvermars
- Date:
- Fri Oct 12 13:12:31 2018 +0000
- Revision:
- 21:3ac3c2e9d9ae
- Parent:
- 20:97059009a491
- Child:
- 22:d2393c670afd
working moving average filter for an emg signal. still have to look at biquadfilter coefficients
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsluiter | 0:32bb76391d89 | 1 | #include "mbed.h" |
MaikOvermars | 21:3ac3c2e9d9ae | 2 | #include "BiQuad.h" |
vsluiter | 11:ce72ec658a95 | 3 | #include "HIDScope.h" |
vsluiter | 0:32bb76391d89 | 4 | |
vsluiter | 4:8b298dfada81 | 5 | //Define objects |
MaikOvermars | 21:3ac3c2e9d9ae | 6 | HIDScope scope(2); // We’re going to send 2 channels of data |
tomlankhorst | 19:2bf824669684 | 7 | AnalogIn emg0( A0 ); |
tomlankhorst | 19:2bf824669684 | 8 | AnalogIn emg1( A1 ); |
tomlankhorst | 19:2bf824669684 | 9 | |
MaikOvermars | 21:3ac3c2e9d9ae | 10 | Ticker emgSampleTicker; |
MaikOvermars | 21:3ac3c2e9d9ae | 11 | |
MaikOvermars | 21:3ac3c2e9d9ae | 12 | // filter chains for high pass, low pass and notch filters |
MaikOvermars | 21:3ac3c2e9d9ae | 13 | BiQuadChain bqc; |
MaikOvermars | 21:3ac3c2e9d9ae | 14 | BiQuad bq1( 4.1660e04,8.3320e04,4.1660e04,1.4797e+00,.5582e01); |
MaikOvermars | 21:3ac3c2e9d9ae | 15 | BiQuad bq2( 1.0000e+00, 2.0000e+00, 1.0000e+00, 1.7010e+00,7.8850e01); |
MaikOvermars | 21:3ac3c2e9d9ae | 16 | volatile double y[50]= { }; |
MaikOvermars | 21:3ac3c2e9d9ae | 17 | volatile int n = 0; |
MaikOvermars | 21:3ac3c2e9d9ae | 18 | volatile bool full = 0; |
vsluiter | 2:e314bb3b2d99 | 19 | |
MaikOvermars | 21:3ac3c2e9d9ae | 20 | void emgSample() { |
MaikOvermars | 21:3ac3c2e9d9ae | 21 | //double emgFiltered = bqc.step( emg0.read() ); |
MaikOvermars | 21:3ac3c2e9d9ae | 22 | double emgFiltered = emg0.read(); |
MaikOvermars | 21:3ac3c2e9d9ae | 23 | scope.set(0,emgFiltered); |
MaikOvermars | 21:3ac3c2e9d9ae | 24 | |
MaikOvermars | 21:3ac3c2e9d9ae | 25 | // moving average |
MaikOvermars | 21:3ac3c2e9d9ae | 26 | y[n] = abs(emgFiltered - 0.45); |
MaikOvermars | 21:3ac3c2e9d9ae | 27 | double emgavg = 0; |
MaikOvermars | 21:3ac3c2e9d9ae | 28 | if(full == 0){ |
MaikOvermars | 21:3ac3c2e9d9ae | 29 | for(int i=0;i<50;i++){ |
MaikOvermars | 21:3ac3c2e9d9ae | 30 | emgavg = emgavg + y[i]; |
MaikOvermars | 21:3ac3c2e9d9ae | 31 | } |
MaikOvermars | 21:3ac3c2e9d9ae | 32 | emgavg = emgavg/(n+1); |
MaikOvermars | 21:3ac3c2e9d9ae | 33 | } |
MaikOvermars | 21:3ac3c2e9d9ae | 34 | else{ |
MaikOvermars | 21:3ac3c2e9d9ae | 35 | for(int i=0;i<50;i++){ |
MaikOvermars | 21:3ac3c2e9d9ae | 36 | emgavg = emgavg + y[i]; |
MaikOvermars | 21:3ac3c2e9d9ae | 37 | } |
MaikOvermars | 21:3ac3c2e9d9ae | 38 | emgavg = emgavg/50; |
MaikOvermars | 21:3ac3c2e9d9ae | 39 | } |
MaikOvermars | 21:3ac3c2e9d9ae | 40 | n++; |
MaikOvermars | 21:3ac3c2e9d9ae | 41 | if(n == 50){ |
MaikOvermars | 21:3ac3c2e9d9ae | 42 | n = 0; |
MaikOvermars | 21:3ac3c2e9d9ae | 43 | full = 1; |
MaikOvermars | 21:3ac3c2e9d9ae | 44 | } |
MaikOvermars | 21:3ac3c2e9d9ae | 45 | scope.set(1,emgavg); |
vsluiter | 11:ce72ec658a95 | 46 | scope.send(); |
MaikOvermars | 21:3ac3c2e9d9ae | 47 | // do stuff with filtered point emgavg |
vsluiter | 2:e314bb3b2d99 | 48 | } |
vsluiter | 0:32bb76391d89 | 49 | |
MaikOvermars | 21:3ac3c2e9d9ae | 50 | |
vsluiter | 0:32bb76391d89 | 51 | int main() |
tomlankhorst | 19:2bf824669684 | 52 | { |
MaikOvermars | 21:3ac3c2e9d9ae | 53 | bqc.add( &bq1 ).add( &bq2 ); |
MaikOvermars | 21:3ac3c2e9d9ae | 54 | emgSampleTicker.attach( &emgSample, 0.01 ); |
MaikOvermars | 21:3ac3c2e9d9ae | 55 | |
tomlankhorst | 14:f83354387756 | 56 | /*empty loop, sample() is executed periodically*/ |
tomlankhorst | 15:0da764eea774 | 57 | while(1) {} |
vsluiter | 0:32bb76391d89 | 58 | } |