Update iir.c

This commit is contained in:
Kuba
2025-08-08 22:49:40 +02:00
committed by GitHub
parent 3d9b7cdf6c
commit 2da62b42fd
+17 -8
View File
@@ -16,17 +16,26 @@ inline float apply_preemphasis(ResistorCapacitor *filter, float sample) {
} }
void tilt_init(TiltCorrectionFilter* filter, float alpha) { void tilt_init(TiltCorrectionFilter* filter, float alpha) {
filter->alpha = alpha; // 0.95 to 0.99 typically // Leaky integrator for DC estimation: dc[n] = alpha*dc[n-1] + (1-alpha)*x[n]
filter->x_prev = 0.0f; // Tilt correction: y[n] = x[n] - dc[n]
filter->y_prev = 0.0f;
if (correction_strength >= 1.0f) {
correction_strength = 0.99999f;
}
if (correction_strength < 0.0f) {
correction_strength = 0.0f;
}
filter->alpha = alpha; // Leaky integrator coefficient
filter->dc_estimate = 0.0f; // Running DC estimate
} }
float tilt(TiltCorrectionFilter* filter, float input) { float tilt_correct(TiltCorrectionFilter* filter, float input) {
// High-pass: y[n] = α*y[n-1] + (x[n] - x[n-1]) // Update DC estimate using leaky integrator
float output = filter->alpha * filter->y_prev + (input - filter->x_prev); filter->dc_estimate = filter->alpha * filter->dc_estimate + (1.0f - filter->alpha) * input;
filter->x_prev = input; // Remove estimated DC component
filter->y_prev = output; float output = input - filter->dc_estimate;
return output; return output;
} }