Biorobotics / Robot-Software

Dependencies:   HIDScope MODSERIAL QEI biquadFilter mbed Servo

help_functions/processing_chain_emg.h

Committer:
MaikOvermars
Date:
2018-10-29
Revision:
25:734a26538711
Parent:
17:1f93c83e211f
Child:
41:e9d6fdf02074

File content as of revision 25:734a26538711:

#include "mbed.h"
#include "BiQuad.h"

//Define objects

// filter chains for high pass, low pass and notch filters
BiQuadChain bqc0;
BiQuadChain bqc1;

// these are for sampling frequency 1000 Hz
BiQuad bq0low( 0.1311, 0.2622, 0.1311, -0.7478, 0.2722);
BiQuad bq0high( 0.9150, -1.8299, 0.9150, -1.8227, 0.8372);
BiQuad bq0notch( 0.9695, -1.8442, 0.9695, -1.8442, 0.9391);

BiQuad bq1low( 0.1311, 0.2622, 0.1311, -0.7478, 0.2722);
BiQuad bq1high( 0.9150, -1.8299, 0.9150, -1.8227, 0.8372);
BiQuad bq1notch( 0.9695, -1.8442, 0.9695, -1.8442, 0.9391);

// old filters
//BiQuad bqlow( 0.3767, 0.7533, 0.3767, 0.3172, 0.1894);
//BiQuad bqhigh( 0.6458, -1.2917, 0.6458, -1.1620, 0.4213);
//BiQuad bqnotch( 0.5, 0, 0.5, 0, 0);

const int k = 300;      // number of moving average samples
double movavg0[k]= { }; // array with samples emg0
double movavg1[k]= { }; // array with samples emg1
int n = 0;              // counter
bool full = 0;          // boolean to check if we have already had 300 samples

void processing_chain_emg(double &raw_emg_0, double &raw_emg_1, double &process_emg_0, double &process_emg_1) {
    
    // we first filter high pass and notch, then rectifier and after that low pass
    movavg0[n] = bq0low.step(fabs(bqc0.step( raw_emg_0 )));
    movavg1[n] = bq1low.step(fabs(bqc1.step( raw_emg_1 )));
    
    // moving average 
    double emgavg0 = 0, emgavg1 = 0;
    if(full == 0){
        for(int i=0;i<k;i++){
            emgavg0 = emgavg0 + movavg0[i];
            emgavg1 = emgavg1 + movavg1[i];
        }
        emgavg0 = emgavg0/(n+1);
        emgavg1 = emgavg1/(n+1);
    }
    else{
        for(int i=0;i<k;i++){
            emgavg0 = emgavg0 + movavg0[i];
            emgavg1 = emgavg1 + movavg1[i];
        }
        emgavg0 = emgavg0/k;
        emgavg1 = emgavg1/k;    
    }
    n++;        
    if(n == k){
        n = 0;      // reset counter when end array reached
        full = 1;   // assures we are past 300 samples
    }
    
    // outputs
    process_emg_0 = emgavg0;
    process_emg_1 = emgavg1;
}