forked from buddhabrot/fusion-zauberstab
add dc_cancelation filter
Signed-off-by: Thomas Schmid <tom@lfence.de>
This commit is contained in:
parent
0103e8c9c0
commit
32f3f3c273
11
firmware/include/dc_cancelation.h
Normal file
11
firmware/include/dc_cancelation.h
Normal 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);
|
@ -9,4 +9,4 @@
|
|||||||
extern CRGB leds[NUM_LEDS];
|
extern CRGB leds[NUM_LEDS];
|
||||||
|
|
||||||
int zauberstab_init();
|
int zauberstab_init();
|
||||||
int32_t get_sample();
|
float get_sample();
|
18
firmware/src/dc_cancelation/dc_canelcation.cpp
Normal file
18
firmware/src/dc_cancelation/dc_canelcation.cpp
Normal 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;
|
||||||
|
}
|
@ -1,5 +1,7 @@
|
|||||||
#include "zauberstab.h"
|
#include "zauberstab.h"
|
||||||
|
#include "dc_cancelation.h"
|
||||||
|
|
||||||
|
struct dc_cancelation_state dc_blocker;
|
||||||
CRGB leds[NUM_LEDS];
|
CRGB leds[NUM_LEDS];
|
||||||
static int16_t mic_offset = 0;
|
static int16_t mic_offset = 0;
|
||||||
|
|
||||||
@ -7,25 +9,13 @@ static uint16_t read_mic() {
|
|||||||
return analogRead(MIC_PIN);
|
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() {
|
int zauberstab_init() {
|
||||||
FastLED.addLeds<WS2812, LED_PIN, RGB>(leds, NUM_LEDS);
|
FastLED.addLeds<WS2812, LED_PIN, RGB>(leds, NUM_LEDS);
|
||||||
//FastLED.setMaxPowerInVoltsAndMilliamps(5, 300);
|
dc_cancelation_init(&dc_blocker, 0.95);
|
||||||
mic_offset = get_mic_offset();
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t get_sample() {
|
float get_sample() {
|
||||||
return read_mic() - mic_offset;
|
int32_t raw_sample = read_mic();
|
||||||
|
return dc_cancelation_update(&dc_blocker, (float) raw_sample);
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user