![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Emg filter function script for a uni project. Made by Teun van der Molen
Dependencies: HIDScope MODSERIAL mbed
Fork of frdm_EMG by
main.cpp@3:499c71ca30a0, 2015-09-21 (annotated)
- Committer:
- teunman
- Date:
- Mon Sep 21 09:37:43 2015 +0000
- Revision:
- 3:499c71ca30a0
- Parent:
- 2:ce5ead27b7cc
- Child:
- 4:1baefd1397d6
Working low-pass filter. only one filter in place. Takes A0 as input and sends a filterd signal to HIDscope; ;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
teunman | 0:674026fdd982 | 1 | #include "mbed.h" |
teunman | 0:674026fdd982 | 2 | #include "HIDScope.h" |
teunman | 0:674026fdd982 | 3 | #include "math.h" |
teunman | 0:674026fdd982 | 4 | // Define the HIDScope and Ticker object |
teunman | 3:499c71ca30a0 | 5 | HIDScope scope(2); |
teunman | 0:674026fdd982 | 6 | Ticker scopeTimer; |
teunman | 1:75f61e111ed0 | 7 | Ticker biquadTicker; |
teunman | 1:75f61e111ed0 | 8 | |
teunman | 1:75f61e111ed0 | 9 | double v1=0, v2=0, u=0, y=0; |
teunman | 1:75f61e111ed0 | 10 | const double |
teunman | 3:499c71ca30a0 | 11 | //b0 = 0.9999999999999999,b1 = 1.9999999999999998,b2 = 0.9999999999999999, a1 = 1.9999999999999998 ,a2 = 0.9999999999999998; //low-pass Fc = 50hz |
teunman | 2:ce5ead27b7cc | 12 | //b0 = 0.02008333102602092 ,b1 = 0.04016666205204184 ,b2 = 0.02008333102602092, a1 = -1.5610153912536877 ,a2 = 0.6413487153577715; //low-pass Fc = 5hz |
teunman | 2:ce5ead27b7cc | 13 | //b0 = 0.8005910266528649,b1 = -1.6011820533057297,b2 = 0.8005910266528649,a1 = -1.5610153912536877,a2 = 0.6413487153577715; //high-pass Fc = 5hz |
teunman | 3:499c71ca30a0 | 14 | b0 = 0.007820199259120319,b1 = 0.015640398518240638,b2 = 0.007820199259120319,a1 = -1.7347238224240125,a2 = 0.7660046194604936; //low-pass Fc = 3hz |
teunman | 3:499c71ca30a0 | 15 | //b0 = 0.0009446914586925257,b1 = 0.0018893829173850514,b2 = 0.0009446914586925257,a1 = -1.911196288237583,a2 = 0.914975054072353; //low-pass Fc = 1hz |
teunman | 1:75f61e111ed0 | 16 | |
teunman | 3:499c71ca30a0 | 17 | |
teunman | 3:499c71ca30a0 | 18 | |
teunman | 3:499c71ca30a0 | 19 | |
teunman | 3:499c71ca30a0 | 20 | |
teunman | 3:499c71ca30a0 | 21 | |
teunman | 3:499c71ca30a0 | 22 | // Read the analog input |
teunman | 3:499c71ca30a0 | 23 | AnalogIn an_in(A0); |
teunman | 3:499c71ca30a0 | 24 | |
teunman | 3:499c71ca30a0 | 25 | AnalogOut an_out(DAC0_OUT); |
teunman | 3:499c71ca30a0 | 26 | // The data read and send function |
teunman | 3:499c71ca30a0 | 27 | void scopeSend() |
teunman | 3:499c71ca30a0 | 28 | { |
teunman | 3:499c71ca30a0 | 29 | scope.set(0,y); |
teunman | 3:499c71ca30a0 | 30 | scope.set(1,an_in); |
teunman | 3:499c71ca30a0 | 31 | |
teunman | 3:499c71ca30a0 | 32 | scope.send(); |
teunman | 3:499c71ca30a0 | 33 | } |
teunman | 3:499c71ca30a0 | 34 | |
teunman | 3:499c71ca30a0 | 35 | void computeBiquad(){ |
teunman | 3:499c71ca30a0 | 36 | double v = an_in - a1*v1 - a2*v2; |
teunman | 1:75f61e111ed0 | 37 | y= b0*v + b1*v1 +b2*v2; |
teunman | 1:75f61e111ed0 | 38 | v2=v1; |
teunman | 1:75f61e111ed0 | 39 | v1=v; |
teunman | 1:75f61e111ed0 | 40 | } |
teunman | 3:499c71ca30a0 | 41 | |
teunman | 0:674026fdd982 | 42 | |
teunman | 0:674026fdd982 | 43 | int main() |
teunman | 0:674026fdd982 | 44 | { |
teunman | 0:674026fdd982 | 45 | // Attach the data read and send function at 100 Hz |
teunman | 0:674026fdd982 | 46 | scopeTimer.attach_us(&scopeSend, 1e4); |
teunman | 3:499c71ca30a0 | 47 | biquadTicker.attach(&computeBiquad,0.01f); |
teunman | 3:499c71ca30a0 | 48 | //float i = 1; |
teunman | 0:674026fdd982 | 49 | while(1) { |
teunman | 0:674026fdd982 | 50 | // sinewave output |
teunman | 3:499c71ca30a0 | 51 | // float u = (0.25 + 0.1*sin(i) + (0.25 + 0.1*sin(100*i))); |
teunman | 1:75f61e111ed0 | 52 | |
teunman | 3:499c71ca30a0 | 53 | // i = i+0.01; |
teunman | 3:499c71ca30a0 | 54 | // an_out = y; |
teunman | 3:499c71ca30a0 | 55 | // wait(0.01); |
teunman | 3:499c71ca30a0 | 56 | // double an_in = y; |
teunman | 0:674026fdd982 | 57 | } |
teunman | 0:674026fdd982 | 58 | } |