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

try to add a lpf

This commit is contained in:
2025-03-27 18:00:05 +01:00
parent 66ca7317d5
commit 7ff33aa5a7
4 changed files with 56 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
{ {
"port": 13452, "port": 13452,
"time": 1743019078692, "time": 1743094558477,
"version": "0.0.3" "version": "0.0.3"
} }

View File

@@ -14,3 +14,41 @@ float apply_preemphasis(ResistorCapacitor *filter, float sample) {
float hard_clip(float sample, float threshold) { float hard_clip(float sample, float threshold) {
return fmaxf(-threshold, fminf(threshold, sample)); return fmaxf(-threshold, fminf(threshold, sample));
} }
void init_butterworth_lpf(Biquad *filter, float cutoff_freq, float sample_rate) {
float omega = M_2PI * cutoff_freq / sample_rate;
float Q = 1.0f / sqrtf(2.0f);
float alpha = sinf(omega) / (2.0f * Q);
float b0 = (1.0f - cosf(omega)) / 2.0f;
float b1 = 1.0f - cosf(omega);
float b2 = (1.0f - cosf(omega)) / 2.0f;
float a0 = 1.0f + alpha;
float a1 = -2.0f * cosf(omega);
float a2 = 1.0f - alpha;
filter->b0 = b0 / a0;
filter->b1 = b1 / a0;
filter->b2 = b2 / a0;
filter->a1 = a1 / a0;
filter->a2 = a2 / a0;
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;
return output;
}

View File

@@ -16,3 +16,13 @@ void init_preemphasis(ResistorCapacitor *filter, float tau, float sample_rate);
float apply_preemphasis(ResistorCapacitor *filter, float sample); float apply_preemphasis(ResistorCapacitor *filter, float sample);
float hard_clip(float sample, float threshold); float hard_clip(float sample, float threshold);
typedef struct
{
float b0, b1, b2;
float a1, a2;
float x1, x2;
float y1, y2;
} Biquad;
void init_butterworth_lpf(Biquad *filter, float cutoff_freq, float sample_rate);
float biquad(Biquad *filter, float input);

View File

@@ -404,7 +404,6 @@ int main(int argc, char **argv) {
return 0; return 0;
} }
// #region Setup Filters/Modulaltors/Oscillators
Oscillator osc; Oscillator osc;
init_oscillator(&osc, polar_stereo ? 31250.0 : 4750, sample_rate); init_oscillator(&osc, polar_stereo ? 31250.0 : 4750, sample_rate);
@@ -414,7 +413,10 @@ int main(int argc, char **argv) {
ResistorCapacitor preemp_l, preemp_r; ResistorCapacitor preemp_l, preemp_r;
init_preemphasis(&preemp_l, preemphasis_tau, sample_rate); init_preemphasis(&preemp_l, preemphasis_tau, sample_rate);
init_preemphasis(&preemp_r, preemphasis_tau, sample_rate); init_preemphasis(&preemp_r, preemphasis_tau, sample_rate);
// #endregion
Biquad lpf_l, lpf_r;
init_butterworth_lpf(&lpf_l, 15000, 192000);
init_butterworth_lpf(&lpf_r, 15000, 192000);
signal(SIGINT, stop); signal(SIGINT, stop);
signal(SIGTERM, stop); signal(SIGTERM, stop);
@@ -471,6 +473,8 @@ int main(int argc, char **argv) {
float ready_l = apply_preemphasis(&preemp_l, l_in); float ready_l = apply_preemphasis(&preemp_l, l_in);
float ready_r = apply_preemphasis(&preemp_r, r_in); float ready_r = apply_preemphasis(&preemp_r, r_in);
ready_l = biquad(&lpf_l, ready_l);
ready_l = biquad(&lpf_r, ready_r);
ready_l = hard_clip(ready_l*audio_volume, clipper_threshold); ready_l = hard_clip(ready_l*audio_volume, clipper_threshold);
ready_r = hard_clip(ready_r*audio_volume, clipper_threshold); ready_r = hard_clip(ready_r*audio_volume, clipper_threshold);