0
1
mirror of https://github.com/radio95-rnt/fm95.git synced 2026-02-26 19:23:51 +01:00

do the preemp gain properly, set a refrence freq (+0db) sligthly above the LPF cutoff, thus we avoid clipping

This commit is contained in:
2025-06-09 20:03:44 +02:00
parent bcb9559d59
commit cad6080c07
3 changed files with 14 additions and 8 deletions

View File

@@ -1,11 +1,17 @@
#include "filters.h"
void init_preemphasis(ResistorCapacitor *filter, float tau, float sample_rate) {
float dt = 1.0f / sample_rate;
filter->alpha = tau / (tau + dt);
filter->gain = 1.0f / sqrtf(1.0f - filter->alpha);
void init_preemphasis(ResistorCapacitor *filter, float tau, float sample_rate, float ref_freq) {
float dt = 1.0f / sample_rate;
filter->alpha = tau / (tau + dt);
filter->prev_sample = 0.0f;
float omega = 2.0f * M_PI * ref_freq / sample_rate;
float cos_omega = cosf(omega);
float numerator = sqrtf(1.0f + filter->alpha * filter->alpha - 2.0f * filter->alpha * cos_omega);
filter->gain = 1.0f / numerator;
filter->prev_sample = 0.0f;
}
inline float apply_preemphasis(ResistorCapacitor *filter, float sample) {
float out = (sample - filter->alpha * filter->prev_sample) * filter->gain;

View File

@@ -10,5 +10,5 @@ typedef struct
float gain;
} ResistorCapacitor;
void init_preemphasis(ResistorCapacitor *filter, float tau, float sample_rate);
void init_preemphasis(ResistorCapacitor *filter, float tau, float sample_rate, float ref_freq);
float apply_preemphasis(ResistorCapacitor *filter, float sample);