From 531e5a6a3893230358ccb27748f9d5265775ce24 Mon Sep 17 00:00:00 2001 From: KubaPro010 Date: Thu, 27 Mar 2025 18:22:40 +0100 Subject: [PATCH] idk --- lib/filters.c | 40 +++++++++++++++++++--------------------- lib/filters.h | 8 +++++++- src/fm95.c | 10 +++++----- 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/lib/filters.c b/lib/filters.c index 98b6971..c521e37 100644 --- a/lib/filters.c +++ b/lib/filters.c @@ -15,34 +15,32 @@ float hard_clip(float sample, float threshold) { return fmaxf(-threshold, fminf(threshold, sample)); } -void init_lpf(Biquad* filter, float sample_rate, float cutoff_freq) { +void init_lpf(Biquad* filter, float sample_rate, float cutoff_freq, float Q) { float omega = 2.0f * M_PI * cutoff_freq / sample_rate; - float alpha = sinf(omega) / (2.0f * 0.707f); + float alpha = sinf(omega) / (2.0f * Q); float cos_omega = cosf(omega); - float norm = 1.0f / (1.0f + alpha); - filter->b0 = (1.0f - cos_omega) * 0.5f * norm; - filter->b1 = (1.0f - cos_omega) * norm; - filter->b2 = filter->b0; - filter->a1 = -2.0f * cos_omega * norm; - filter->a2 = (1.0f - alpha) * norm; + float norm = 1.0f / (1.0f + alpha); + filter->b0 = (1.0f - cos_omega) * 0.5f * norm; + filter->b1 = (1.0f - cos_omega) * norm; + filter->b2 = filter->b0; + filter->a1 = -2.0f * cos_omega * norm; + filter->a2 = (1.0f - alpha) * norm; filter->x1 = filter->x2 = 0.0f; filter->y1 = filter->y2 = 0.0f; } -float biquad(Biquad *filter, float input) { - float output = filter->b0 * input - + filter->b1 * filter->x1 - + filter->b2 * filter->x2 - - filter->a1 * filter->y1 - - filter->a2 * filter->y2; - - filter->x2 = filter->x1; - filter->x1 = input; - - filter->y2 = filter->y1; - filter->y1 = output; - +void init_lpf4(LPF4* filter, float sample_rate, float cutoff_freq) { + float Q1 = 1.0f / (2.0f * cosf(M_PI / 8.0f)); + init_lpf(&filter->section1, sample_rate, cutoff_freq, Q1); + + float Q2 = 1.0f / (2.0f * cosf(3.0f * M_PI / 8.0f)); + init_lpf(&filter->section2, sample_rate, cutoff_freq, Q2); +} + +float apply_lpf4(LPF4* filter, float input) { + float output = biquad(&filter->section1, input); + output = biquad(&filter->section2, output); return output; } \ No newline at end of file diff --git a/lib/filters.h b/lib/filters.h index 57946da..7da6657 100644 --- a/lib/filters.h +++ b/lib/filters.h @@ -24,5 +24,11 @@ typedef struct float x1, x2; float y1, y2; } Biquad; -void init_lpf(Biquad* filter, float sample_rate, float cutoff_freq); +typedef struct { + Biquad section1; + Biquad section2; +} LPF4; +void init_lpf(Biquad* filter, float sample_rate, float cutoff_freq, float Q); +void init_lpf4(LPF4* filter, float sample_rate, float cutoff_freq); +float apply_lpf4(LPF4* filter, float input); float biquad(Biquad *filter, float input); \ No newline at end of file diff --git a/src/fm95.c b/src/fm95.c index d5c721a..b885021 100644 --- a/src/fm95.c +++ b/src/fm95.c @@ -414,9 +414,9 @@ int main(int argc, char **argv) { init_preemphasis(&preemp_l, preemphasis_tau, sample_rate); init_preemphasis(&preemp_r, preemphasis_tau, sample_rate); - Biquad lpf_l, lpf_r; - init_lpf(&lpf_l, sample_rate, 15000); - init_lpf(&lpf_r, sample_rate, 15000); + LPF4 lpf_l, lpf_r; + init_lpf4(&lpf_l, sample_rate, 15000); + init_lpf4(&lpf_r, sample_rate, 15000); signal(SIGINT, stop); signal(SIGTERM, stop); @@ -473,8 +473,8 @@ int main(int argc, char **argv) { float ready_l = apply_preemphasis(&preemp_l, l_in); float ready_r = apply_preemphasis(&preemp_r, r_in); - ready_l = biquad(&lpf_l, ready_l); - ready_r = biquad(&lpf_r, ready_r); + ready_l = apply_lpf4(&lpf_l, ready_l); + ready_r = apply_lpf4(&lpf_r, ready_r); ready_l = hard_clip(ready_l*audio_volume, clipper_threshold); ready_r = hard_clip(ready_r*audio_volume, clipper_threshold);