add dc_cancelation filter

Signed-off-by: Thomas Schmid <tom@lfence.de>
This commit is contained in:
Thomas 2022-06-18 11:16:07 +02:00
parent 0103e8c9c0
commit 32f3f3c273
4 changed files with 36 additions and 17 deletions

View File

@ -0,0 +1,11 @@
#pragma once
struct dc_cancelation_state {
float x_n1;
float y_n1;
float 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);

View File

@ -9,4 +9,4 @@
extern CRGB leds[NUM_LEDS];
int zauberstab_init();
int32_t get_sample();
float get_sample();

View File

@ -0,0 +1,18 @@
#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,5 +1,7 @@
#include "zauberstab.h"
#include "dc_cancelation.h"
struct dc_cancelation_state dc_blocker;
CRGB leds[NUM_LEDS];
static int16_t mic_offset = 0;
@ -7,25 +9,13 @@ static uint16_t read_mic() {
return analogRead(MIC_PIN);
}
static uint16_t get_mic_offset() {
float average = 0.f;
uint32_t num_samples = 0;
for(num_samples = 0; num_samples < 10000; num_samples++) {
uint16_t sample = read_mic();
average += ((float) sample - average) / (num_samples + 1);
}
return (uint16_t)average;
}
int zauberstab_init() {
FastLED.addLeds<WS2812, LED_PIN, RGB>(leds, NUM_LEDS);
//FastLED.setMaxPowerInVoltsAndMilliamps(5, 300);
mic_offset = get_mic_offset();
dc_cancelation_init(&dc_blocker, 0.95);
return 0;
}
int32_t get_sample() {
return read_mic() - mic_offset;
float get_sample() {
int32_t raw_sample = read_mic();
return dc_cancelation_update(&dc_blocker, (float) raw_sample);
}