dc_cancelation: implement as template class

Signed-off-by: Thomas Schmid <tom@lfence.de>
This commit is contained in:
Thomas 2022-06-19 12:17:21 +02:00
parent cf750201a2
commit b3ce3e0169
3 changed files with 17 additions and 29 deletions

View File

@ -1,11 +1,18 @@
#pragma once
struct dc_cancelation_state {
float x_n1;
float y_n1;
float R;
};
template<class T>
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);
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;
};
};

View File

@ -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;
}

View File

@ -1,7 +1,7 @@
#include "zauberstab.h"
#include "dc_cancelation.h"
struct dc_cancelation_state dc_blocker;
DcCancelation<float> 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<WS2812, LED_PIN, GRB>(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;
}