diff --git a/firmware/include/dc_cancelation.h b/firmware/include/dc_cancelation.h index 6ddeefe..36efdef 100644 --- a/firmware/include/dc_cancelation.h +++ b/firmware/include/dc_cancelation.h @@ -1,11 +1,18 @@ #pragma once -struct dc_cancelation_state { - float x_n1; - float y_n1; - float R; -}; +template +struct DcCancelation { + T x_n1; + T y_n1; + T R; -void dc_cancelation_init(struct dc_cancelation_state *state, float R); -void dc_cancelation_set_R(struct dc_cancelation_state *state, float R); -float dc_cancelation_update(struct dc_cancelation_state *state, float x); \ No newline at end of file + DcCancelation(T R) : R(R) {}; + + T update(T x) { + T y = x-this->x_n1 + this->R * this->y_n1; + this->x_n1 = x; + this->y_n1 = y; + + return y; + }; +}; \ No newline at end of file diff --git a/firmware/src/dc_cancelation/dc_canelcation.cpp b/firmware/src/dc_cancelation/dc_canelcation.cpp deleted file mode 100644 index e0faa79..0000000 --- a/firmware/src/dc_cancelation/dc_canelcation.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "dc_cancelation.h" - -void dc_cancelation_init(struct dc_cancelation_state *state, float R){ - state->R = R; - state->x_n1 = 0.0f; - state->y_n1 = 0.0f; -} - -void dc_cancelation_set_R(struct dc_cancelation_state *state, float R) { - state->R = R; -} - -float dc_cancelation_update(struct dc_cancelation_state *state, float x) { - float y = x-state->x_n1 + state->R * state->y_n1; - state->x_n1 = x; - state->y_n1 = y; - return y; -} diff --git a/firmware/src/zauberstab.cpp b/firmware/src/zauberstab.cpp index 85b8389..c4b2936 100644 --- a/firmware/src/zauberstab.cpp +++ b/firmware/src/zauberstab.cpp @@ -1,7 +1,7 @@ #include "zauberstab.h" #include "dc_cancelation.h" -struct dc_cancelation_state dc_blocker; +DcCancelation dc_blocker{0.95}; CRGB leds[NUM_LEDS]; static int16_t mic_offset = 0; @@ -12,12 +12,11 @@ static uint16_t read_mic() { int zauberstab_init() { FastLED.addLeds(leds, NUM_LEDS); //FastLED.setMaxPowerInVoltsAndMilliamps(5, 300); - dc_cancelation_init(&dc_blocker, 0.95); return 0; } float get_sample() { float sample = read_mic(); - sample = dc_cancelation_update(&dc_blocker, sample); + sample = dc_blocker.update(sample); return sample; } \ No newline at end of file