Bandpass, notch, abs en laagdoorlaat 3H

Dependencies:   HIDScope MODSERIAL TextLCD mbed-dsp mbed

Fork of EMGmeten by Laura Heus

Committer:
lauradeheus
Date:
Wed Oct 29 16:12:34 2014 +0000
Revision:
4:7150ed6ce984
Parent:
3:c82170d8b6c8
Child:
5:f93b7068c202
Bandpass-notch-abs-laagdoorlaat-pieken tellen werkt

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lauradeheus 0:b68a6d0451b8 1 #include "mbed.h"
lauradeheus 0:b68a6d0451b8 2 #include "HIDScope.h"
lauradeheus 2:5d64e4c95f5c 3 #include "arm_math.h"
lauradeheus 2:5d64e4c95f5c 4 #include "MODSERIAL.h"
lauradeheus 4:7150ed6ce984 5 #include "TextLCD.h"
lauradeheus 4:7150ed6ce984 6
lauradeheus 4:7150ed6ce984 7 #define EMG_treshhold 0,05
lauradeheus 0:b68a6d0451b8 8
lauradeheus 2:5d64e4c95f5c 9 //MODSERIAL pc(USBTX,USBRX);
lauradeheus 0:b68a6d0451b8 10 AnalogIn emg(PTB1); //Analog input
lauradeheus 4:7150ed6ce984 11 HIDScope scope(2); // Twee kanalen op de HIDScope
lauradeheus 4:7150ed6ce984 12 DigitalIn knop(PTD4); /*Digital input pin (knop) definieren*/
lauradeheus 4:7150ed6ce984 13 TextLCD lcd(PTD2, PTA12, PTB2, PTB3, PTC2, PTA13, TextLCD::LCD16x2);
lauradeheus 0:b68a6d0451b8 14
lauradeheus 4:7150ed6ce984 15 arm_biquad_casd_df1_inst_f32 lowpass_1; //2e orde lowpass biquad butterworthfilter 99Hz
lauradeheus 4:7150ed6ce984 16 arm_biquad_casd_df1_inst_f32 lowpass_2; //2e orde lowpass biquad butterworthfilter 3Hz
lauradeheus 4:7150ed6ce984 17 arm_biquad_casd_df1_inst_f32 highpass; //2e orde highpass biquad butterworthfilter 20Hz
lauradeheus 4:7150ed6ce984 18 arm_biquad_casd_df1_inst_f32 notch; //2e orde lowpass biquad butterworthfilter 50Hz
lauradeheus 1:b7a9fcf50776 19 float lowpass_1_const[] = {0.978030479206560 , 1.956060958413119 , 0.978030479206560 , -1.955578240315036 , -0.956543676511203};
lauradeheus 1:b7a9fcf50776 20 float lowpass_1_states[4];
lauradeheus 4:7150ed6ce984 21 float lowpass_2_const[] = {0.002080567135492 , 0.004161134270985 , 0.002080567135492 , 1.866892279711715 , -0.875214548253684}; //3Hz
lauradeheus 4:7150ed6ce984 22 //{0.0009446914586925257 , 0.0018893829173850514 , 0.0009446914586925257 , 1.911196288237583 , -0.914975054072353};//2Hz
lauradeheus 4:7150ed6ce984 23 //{0.00024135899874854145 , 0.0004827179974970829 , 0.00024135899874854145 , 1.9555778328194147 , -0.9565432688144089}; 1Hz
lauradeheus 1:b7a9fcf50776 24 float lowpass_2_states[4];
lauradeheus 1:b7a9fcf50776 25 float highpass_const[] = {0.638945525159022 , -1.277891050318045 , 0.638945525159022 , 1.142980502539901 , -0.412801598096189};
lauradeheus 0:b68a6d0451b8 26 float highpass_states[4];
lauradeheus 2:5d64e4c95f5c 27 float notch_const[] = {0.978048948305681 , 0.000000000000000 , 0.978048948305681 , 0.000000000000000 , -0.956097896611362};
lauradeheus 2:5d64e4c95f5c 28 float notch_states[4];
lauradeheus 4:7150ed6ce984 29 float filtered_emg;
lauradeheus 4:7150ed6ce984 30 float EMG_max = 0.16;
lauradeheus 4:7150ed6ce984 31 float EMG_treshhold_laag = 0.3*EMG_max;
lauradeheus 4:7150ed6ce984 32 float EMG_treshhold_hoog = 0.7*EMG_max;
lauradeheus 4:7150ed6ce984 33
lauradeheus 4:7150ed6ce984 34 bool aanspan;
lauradeheus 4:7150ed6ce984 35 int aantal_pieken;
lauradeheus 4:7150ed6ce984 36
lauradeheus 4:7150ed6ce984 37 void looper();
lauradeheus 4:7150ed6ce984 38 void pieken_tellen();
lauradeheus 4:7150ed6ce984 39 //char *lcd_r1 = new char[16];
lauradeheus 4:7150ed6ce984 40 //char *lcd_r2 = new char[16];
lauradeheus 0:b68a6d0451b8 41
lauradeheus 0:b68a6d0451b8 42 void looper()
lauradeheus 0:b68a6d0451b8 43 {
lauradeheus 0:b68a6d0451b8 44 uint16_t emg_value;
lauradeheus 0:b68a6d0451b8 45 float emg_value_f32;
lauradeheus 0:b68a6d0451b8 46 emg_value = emg.read_u16(); // read direct ADC result, converted to 16 bit integer (0..2^16 = 0..65536 = 0..3.3V)
lauradeheus 0:b68a6d0451b8 47 emg_value_f32 = emg.read();
lauradeheus 0:b68a6d0451b8 48
lauradeheus 0:b68a6d0451b8 49 arm_biquad_cascade_df1_f32(&highpass, &emg_value_f32, &filtered_emg, 1 );
lauradeheus 1:b7a9fcf50776 50 arm_biquad_cascade_df1_f32(&lowpass_1, &filtered_emg, &filtered_emg, 1 );
lauradeheus 2:5d64e4c95f5c 51 arm_biquad_cascade_df1_f32(&notch, &filtered_emg, &filtered_emg, 1);
lauradeheus 1:b7a9fcf50776 52 filtered_emg = fabs(filtered_emg);
lauradeheus 2:5d64e4c95f5c 53 //emg_value_f32 = fabs(emg_value_f32);
lauradeheus 3:c82170d8b6c8 54 arm_biquad_cascade_df1_f32(&lowpass_2, &filtered_emg, &filtered_emg, 1 );
lauradeheus 4:7150ed6ce984 55
lauradeheus 4:7150ed6ce984 56 void pieken_tellen();
lauradeheus 4:7150ed6ce984 57 {
lauradeheus 4:7150ed6ce984 58 if (filtered_emg>=EMG_treshhold_hoog)
lauradeheus 4:7150ed6ce984 59 {
lauradeheus 4:7150ed6ce984 60 aanspan=true; //maak een variabele waarin je opslaat dat het signaal hoog is.
lauradeheus 4:7150ed6ce984 61 }
lauradeheus 4:7150ed6ce984 62 if (aanspan==true && filtered_emg<=EMG_treshhold_laag)//== ipv =, anders wordt aanspan true gemaakt
lauradeheus 4:7150ed6ce984 63 {
lauradeheus 4:7150ed6ce984 64 aanspan=false;
lauradeheus 4:7150ed6ce984 65 aantal_pieken++;
lauradeheus 4:7150ed6ce984 66 }
lauradeheus 4:7150ed6ce984 67 }
lauradeheus 0:b68a6d0451b8 68
lauradeheus 0:b68a6d0451b8 69 scope.set(0,emg_value); //uint value
lauradeheus 0:b68a6d0451b8 70 scope.set(1,filtered_emg); //processed float
lauradeheus 0:b68a6d0451b8 71 scope.send();
lauradeheus 0:b68a6d0451b8 72 }
lauradeheus 0:b68a6d0451b8 73
lauradeheus 0:b68a6d0451b8 74 int main()
lauradeheus 0:b68a6d0451b8 75 {
lauradeheus 0:b68a6d0451b8 76 Ticker log_timer;
lauradeheus 0:b68a6d0451b8 77
lauradeheus 1:b7a9fcf50776 78 arm_biquad_cascade_df1_init_f32(&lowpass_1,1 , lowpass_1_const, lowpass_1_states);
lauradeheus 0:b68a6d0451b8 79 arm_biquad_cascade_df1_init_f32(&highpass,1 , highpass_const, highpass_states);
lauradeheus 2:5d64e4c95f5c 80 arm_biquad_cascade_df1_init_f32(&notch,1 , notch_const, notch_states);
lauradeheus 3:c82170d8b6c8 81 arm_biquad_cascade_df1_init_f32(&lowpass_2,1 , lowpass_2_const, lowpass_2_states);
lauradeheus 0:b68a6d0451b8 82
lauradeheus 0:b68a6d0451b8 83 log_timer.attach(looper, 0.005);
lauradeheus 0:b68a6d0451b8 84
lauradeheus 0:b68a6d0451b8 85 while(1) //Loop
lauradeheus 0:b68a6d0451b8 86 {
lauradeheus 4:7150ed6ce984 87 lcd.cls();
lauradeheus 4:7150ed6ce984 88 lcd.printf("Aantal pieken =\n%d", aantal_pieken);
lauradeheus 4:7150ed6ce984 89 wait(0.02);
lauradeheus 0:b68a6d0451b8 90 /*Empty!*/
lauradeheus 0:b68a6d0451b8 91 /*Everything is handled by the interrupt routine now!*/
lauradeheus 2:5d64e4c95f5c 92 //pc.baud(9600);
lauradeheus 0:b68a6d0451b8 93 }
lauradeheus 0:b68a6d0451b8 94 }
lauradeheus 0:b68a6d0451b8 95