Highpass en Notch filter voor EMG verwerking

Dependencies:   HIDScope mbed-dsp mbed

Fork of EMG_HIDScope by First Last

main.cpp

Committer:
vsluiter
Date:
2014-09-24
Revision:
17:1388f1a2d7b2
Parent:
16:24e992616cf6
Child:
18:fed07cc1f8f6

File content as of revision 17:1388f1a2d7b2:

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

//Define objects
AnalogIn    emg0(PTB1); //Analog input
PwmOut      red(LED_RED); //PWM output
Ticker log_timer;
MODSERIAL pc(USBTX,USBRX);
HIDScope scope(2);

typedef struct second_order_constants
{
    float b[3];
    float a[3];
} second_order_constants_t;

typedef struct second_order_values
{
    float x_1,x_2;
    float y_1,y_2;
} second_order_values_t;

//constants
second_order_constants_t highpass= {{0.97803048, -1.95606096,  0.97803048},{1, -1.95557824,  0.95654368}};
second_order_constants_t lowpass={{ 0.02008337 , 0.04016673 , 0.02008337},{1.0,         -1.56101808,  0.64135154}};
//type for values
second_order_values_t highpass_values, lowpass_values;

float second_order(float x, second_order_constants_t constants, second_order_values_t &values);


/** Looper function
* functions used for Ticker and Timeout should be of type void <name>(void)
* i.e. no input arguments, no output arguments.
* if you want to change a variable that you use in other places (for example in main)
* you will have to make that variable global in order to be able to reach it both from
* the function called at interrupt time, and in the main function.
* To make a variable global, define it under the includes.
* variables that are changed in the interrupt routine (written to) should be made
* 'volatile' to let the compiler know that those values may change outside the current context.
* i.e.: "volatile uint16_t emg_value;" instead of "uint16_t emg_value"
* in the example below, the variable is not re-used in the main function, and is thus declared
* local in the looper function only.
**/
void looper()
{
    /*variable to store value in*/    
    uint16_t emg_value;
    float filtered_emg;
    /*put raw emg value both in red and in emg_value*/
    red.write(emg0.read());      // read float value (0..1 = 0..3.3V)
    emg_value = emg0.read_u16(); // read direct ADC result, converted to 16 bit integer (0..2^16 = 0..65536 = 0..3.3V)
    filtered_emg = second_order((float)emg_value,highpass, highpass_values);
    /*send value to PC. Line below is used to prevent buffer overrun */

    scope.set(0,emg_value);
    scope.set(1,second_order(fabs(filtered_emg), lowpass, lowpass_values));
    scope.send();

    /**When not using the LED, the above could also have been done this way:
    * pc.printf("%u\n", emg0.read_u16());
    */
}

int main()
{
    /*setup baudrate. Choose the same in your program on PC side*/
    //pc.baud(115200);
    /*set the period for the PWM to the red LED*/
    red.period_ms(2);
    /**Here you attach the 'void looper(void)' function to the Ticker object
    * The looper() function will be called every 0.01 seconds.
    * Please mind that the parentheses after looper are omitted when using attach.
    */
    log_timer.attach(looper, 0.005);
    while(1) //Loop
    {
      /*Empty!*/
      /*Everything is handled by the interrupt routine now!*/
    }
}

float second_order(float x, second_order_constants_t constants, second_order_values_t &values)
{
    float y = 0;
    float b_terms, a_terms;
    b_terms = (constants.b[0]*x) + (constants.b[1]*values.x_1) + (constants.b[2]*values.x_2);
    a_terms = (constants.a[1]*values.y_1) + (constants.a[2]*values.y_2);
    y=(1./constants.a[0])* (b_terms-a_terms);
    values.x_2 = values.x_1;
    values.x_1 = x;
    values.y_2 = values.y_1;
    values.y_1 = y;
    return y;
}