diff --git a/middleware/include/high_pass.h b/middleware/include/high_pass.h index 8abc59b..1a397c8 100644 --- a/middleware/include/high_pass.h +++ b/middleware/include/high_pass.h @@ -3,6 +3,23 @@ #include -int high_pass(int alpha, int scale); +typedef struct { + //Function Parameters + float alpha; + float scale; + + float prev_output; + +} high_pass_st; + +void high_pass_init(float alpha, float scale, high_pass_st filter); +/** +@brief Initialization for high pass values + */ + +float high_pass(high_pass_st filter, float input); +/** +@brief Function for high pass filter + */ #endif diff --git a/middleware/src/high_pass.c b/middleware/src/high_pass.c index b5e6b5d..90cae67 100644 --- a/middleware/src/high_pass.c +++ b/middleware/src/high_pass.c @@ -17,36 +17,27 @@ output = alpha * last_output + (1-alpha) * input output = alpha * last_output + alpha * (avg - prev_avg) */ -#include "high_pass.h" -//Samples can be modified -#define SAMPLES 12 +#include "high_pass.h" -//Scale Var allows adjustment of output strength -//Alpha Var controls weight of prev input & sum difference -float high_pass(int alpha, int scale) +void high_pass_init(float alpha, float scale, high_pass_st filter) { - static float buffer[SAMPLES] = { 0 }; //initialize changing buffer - static int index = 0; - static float prev_sum = 0.0; - - //buffer[index] = freq; //-> adding the freq input into the - - index = (index + 1) % SAMPLES; + filter->alpha = alpha; + filter->scale = scale; - float sum = 0.0; - - //Iterates through all indices - for (int i = 0; i < SAMPLES; i++) { - sum += buffer[i]; - } + filter->prev_output = 0.0; +} - //Algorithm - // +//y[n]=x[n]−SMA[n] +//The output is equal to the input - the moving average +float high_pass(high_pass_st filter, float input) +{ float output = - scale * (alpha * buffer[index - 1] + alpha * (sum - prev_sum)); + filter->scale(input - (filter->alpha * input + + (1 - filter->alpha) * prev_output)); - prev_sum = sum; //updates the prev_sum + filter->prev_output = + filter->alpha * input + (1 - filter->alpha) * prev_output; return output; }