From bab8efec1b363114247320461455f2c2010c92f5 Mon Sep 17 00:00:00 2001 From: Ruben Noroian Date: Sun, 24 Nov 2024 14:42:40 -0500 Subject: [PATCH] high pass filter implementation --- middleware/include/high_pass.h | 8 ++++++ middleware/src/high_pass.c | 52 ++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 middleware/include/high_pass.h create mode 100644 middleware/src/high_pass.c diff --git a/middleware/include/high_pass.h b/middleware/include/high_pass.h new file mode 100644 index 0000000..8abc59b --- /dev/null +++ b/middleware/include/high_pass.h @@ -0,0 +1,8 @@ +#ifndef HIGH_PASS_H +#define HIGH_PASS_H + +#include + +int high_pass(int alpha, int scale); + +#endif diff --git a/middleware/src/high_pass.c b/middleware/src/high_pass.c new file mode 100644 index 0000000..b5e6b5d --- /dev/null +++ b/middleware/src/high_pass.c @@ -0,0 +1,52 @@ +/* +Understanding High-Pass Filter Implementation + +Goal is to customize the cutoff frequency/strength + +One style: +-alpha factor -> smoothing factor used to make the changes smoother +-scale factor -> scales the output given by algo +these two params correlate to strength + +Or user could pass desired cutoff frequency and that would be used + +Algorithm: +1. first possibility +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 + +//Scale Var allows adjustment of output strength +//Alpha Var controls weight of prev input & sum difference +float high_pass(int alpha, int scale) +{ + 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; + + float sum = 0.0; + + //Iterates through all indices + for (int i = 0; i < SAMPLES; i++) { + sum += buffer[i]; + } + + //Algorithm + // + float output = + scale * (alpha * buffer[index - 1] + alpha * (sum - prev_sum)); + + prev_sum = sum; //updates the prev_sum + + return output; +}