Bandpass, notch, abs en laagdoorlaat 3Hz

Dependencies:   HIDScope MODSERIAL mbed-dsp mbed TextLCD

EMGmeten.cpp

Committer:
lauradeheus
Date:
2014-10-29
Revision:
3:c82170d8b6c8
Parent:
2:5d64e4c95f5c
Child:
4:7150ed6ce984

File content as of revision 3:c82170d8b6c8:

#include "mbed.h"
#include "HIDScope.h"
#include "arm_math.h"
#include "MODSERIAL.h"

//MODSERIAL pc(USBTX,USBRX);
AnalogIn    emg(PTB1); //Analog input
HIDScope scope(2);

arm_biquad_casd_df1_inst_f32 lowpass_1; //99Hz
arm_biquad_casd_df1_inst_f32 lowpass_2; //3Hz
arm_biquad_casd_df1_inst_f32 highpass; //20Hz
arm_biquad_casd_df1_inst_f32 notch; //50Hz
float lowpass_1_const[] = {0.978030479206560 , 1.956060958413119 , 0.978030479206560 , -1.955578240315036 , -0.956543676511203};
float lowpass_1_states[4];
float lowpass_2_const[] = {0.002080567135492 , 0.004161134270985 , 0.002080567135492 , 1.866892279711715 , -0.875214548253684};
float lowpass_2_states[4];
float highpass_const[] = {0.638945525159022 , -1.277891050318045 ,  0.638945525159022 , 1.142980502539901 , -0.412801598096189};
float highpass_states[4];
float notch_const[] = {0.978048948305681 , 0.000000000000000 , 0.978048948305681 , 0.000000000000000 , -0.956097896611362};
float notch_states[4];

void looper()
{
    uint16_t emg_value;
    float filtered_emg;
    float emg_value_f32;
    emg_value = emg.read_u16(); // read direct ADC result, converted to 16 bit integer (0..2^16 = 0..65536 = 0..3.3V)
    emg_value_f32 = emg.read();
    
    arm_biquad_cascade_df1_f32(&highpass, &emg_value_f32, &filtered_emg, 1 );
    arm_biquad_cascade_df1_f32(&lowpass_1, &filtered_emg, &filtered_emg, 1 );
    arm_biquad_cascade_df1_f32(&notch, &filtered_emg, &filtered_emg, 1);
    filtered_emg = fabs(filtered_emg);
    //emg_value_f32 = fabs(emg_value_f32);
    arm_biquad_cascade_df1_f32(&lowpass_2, &filtered_emg, &filtered_emg, 1 );
    
    scope.set(0,emg_value);     //uint value
    scope.set(1,filtered_emg);  //processed float
    scope.send();
}

int main()
{
    Ticker log_timer;
    
    arm_biquad_cascade_df1_init_f32(&lowpass_1,1 , lowpass_1_const, lowpass_1_states);
    arm_biquad_cascade_df1_init_f32(&highpass,1 , highpass_const, highpass_states);
    arm_biquad_cascade_df1_init_f32(&notch,1 , notch_const, notch_states);
    arm_biquad_cascade_df1_init_f32(&lowpass_2,1 , lowpass_2_const, lowpass_2_states);
    
    log_timer.attach(looper, 0.005);
    
    while(1) //Loop
    {
      /*Empty!*/
      /*Everything is handled by the interrupt routine now!*/
      //pc.baud(9600);
      //pc.printf("filtered_emg=%f\n\r",filtered_emg);
    }
}