mirror of
https://github.com/radio95-rnt/fm95.git
synced 2026-02-27 03:23:54 +01:00
add new preemp
This commit is contained in:
2
.vscode/.server-controller-port.log
vendored
2
.vscode/.server-controller-port.log
vendored
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"port": 13452,
|
"port": 13452,
|
||||||
"time": 1738326357935,
|
"time": 1738336716329,
|
||||||
"version": "0.0.3"
|
"version": "0.0.3"
|
||||||
}
|
}
|
||||||
@@ -1,19 +1,51 @@
|
|||||||
#include "filters.h"
|
#include "filters.h"
|
||||||
|
|
||||||
void init_rc(ResistorCapacitor *rc, float alpha) {
|
void init_preemphasis(BiquadFilter *filter, float tau, float sample_rate) {
|
||||||
rc->prev_sample = 0.0f;
|
/*
|
||||||
rc->alpha = alpha;
|
premphasis should go like this:
|
||||||
}
|
cutoff: +0
|
||||||
|
2cutoff: +6
|
||||||
|
3cutoff: +12
|
||||||
|
4cutoff: +18
|
||||||
|
...
|
||||||
|
*/
|
||||||
|
|
||||||
void init_rc_tau(ResistorCapacitor *rc, float tau, float sample_rate) {
|
// Calculate the cutoff frequency from tau (f_c = 1/(2πτ))
|
||||||
rc->prev_sample = 0.0f;
|
float cutoff_freq = 1.0f / (2.0f * M_PI * tau);
|
||||||
rc->alpha = exp(-1 / (tau * sample_rate));
|
|
||||||
}
|
|
||||||
|
|
||||||
float apply_pre_emphasis(ResistorCapacitor *rc, float sample) {
|
// Pre-warp the cutoff frequency for the bilinear transform
|
||||||
float audio = sample-rc->alpha*rc->prev_sample;
|
float omega = 2.0f * M_PI * cutoff_freq / sample_rate;
|
||||||
rc->prev_sample = audio;
|
float K = tanf(omega / 2.0f);
|
||||||
return audio;
|
|
||||||
|
// Calculate filter coefficients for 1st-order high-pass (converted to biquad)
|
||||||
|
float alpha = 1.0f + K;
|
||||||
|
filter->b0 = 1.0f / alpha;
|
||||||
|
filter->b1 = -filter->b0;
|
||||||
|
filter->b2 = 0.0f;
|
||||||
|
filter->a1 = (K - 1.0f) / alpha;
|
||||||
|
filter->a2 = 0.0f;
|
||||||
|
|
||||||
|
// Reset state variables
|
||||||
|
filter->x1 = 0.0f;
|
||||||
|
filter->x2 = 0.0f;
|
||||||
|
filter->y1 = 0.0f;
|
||||||
|
filter->y2 = 0.0f;
|
||||||
|
}
|
||||||
|
float apply_preemphasis(BiquadFilter *filter, float input) {
|
||||||
|
// Direct Form I implementation
|
||||||
|
float output = filter->b0 * input
|
||||||
|
+ filter->b1 * filter->x1
|
||||||
|
+ filter->b2 * filter->x2
|
||||||
|
- filter->a1 * filter->y1
|
||||||
|
- filter->a2 * filter->y2;
|
||||||
|
|
||||||
|
// Update state variables
|
||||||
|
filter->x2 = filter->x1;
|
||||||
|
filter->x1 = input;
|
||||||
|
filter->y2 = filter->y1;
|
||||||
|
filter->y1 = output;
|
||||||
|
|
||||||
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
void init_lpf(FrequencyFilter* filter, float cutoffFreq, float sampleRate) {
|
void init_lpf(FrequencyFilter* filter, float cutoffFreq, float sampleRate) {
|
||||||
|
|||||||
@@ -6,13 +6,14 @@
|
|||||||
#include "constants.h"
|
#include "constants.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
float alpha;
|
float b0, b1, b2;
|
||||||
float prev_sample;
|
float a1, a2;
|
||||||
} ResistorCapacitor;
|
float x1, x2;
|
||||||
|
float y1, y2;
|
||||||
|
} BiquadFilter;
|
||||||
|
|
||||||
void init_rc(ResistorCapacitor *pe, float alpha);
|
void init_preemphasis(BiquadFilter *filter, float tau, float sample_rate);
|
||||||
void init_rc_tau(ResistorCapacitor *pe, float tau, float sample_rate);
|
float apply_preemphasis(BiquadFilter *filter, float input);
|
||||||
float apply_pre_emphasis(ResistorCapacitor *pe, float sample);
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
float coeffs[FILTER_TAPS];
|
float coeffs[FILTER_TAPS];
|
||||||
|
|||||||
10
src/fm95.c
10
src/fm95.c
@@ -456,9 +456,9 @@ int main(int argc, char **argv) {
|
|||||||
DelayLine monoDelay; // Hilbert introduces a delay, this should be here to sync the mono with stereo to a sample
|
DelayLine monoDelay; // Hilbert introduces a delay, this should be here to sync the mono with stereo to a sample
|
||||||
init_delay_line(&monoDelay, (HILBERT_TAPS-1)/2);
|
init_delay_line(&monoDelay, (HILBERT_TAPS-1)/2);
|
||||||
|
|
||||||
ResistorCapacitor preemp_l, preemp_r;
|
BiquadFilter preemp_l, preemp_r;
|
||||||
init_rc_tau(&preemp_l, preemphasis_tau, SAMPLE_RATE);
|
init_preemphasis(&preemp_l, preemphasis_tau, SAMPLE_RATE);
|
||||||
init_rc_tau(&preemp_r, preemphasis_tau, SAMPLE_RATE);
|
init_preemphasis(&preemp_r, preemphasis_tau, SAMPLE_RATE);
|
||||||
|
|
||||||
FrequencyFilter lpf_l, lpf_r;
|
FrequencyFilter lpf_l, lpf_r;
|
||||||
init_lpf(&lpf_l, LPF_CUTOFF, SAMPLE_RATE);
|
init_lpf(&lpf_l, LPF_CUTOFF, SAMPLE_RATE);
|
||||||
@@ -514,8 +514,8 @@ int main(int argc, char **argv) {
|
|||||||
float ready_r = apply_frequency_filter(&lpf_r, l_in);
|
float ready_r = apply_frequency_filter(&lpf_r, l_in);
|
||||||
ready_l = apply_frequency_filter(&hpf_l, ready_l);
|
ready_l = apply_frequency_filter(&hpf_l, ready_l);
|
||||||
ready_r = apply_frequency_filter(&hpf_r, ready_r);
|
ready_r = apply_frequency_filter(&hpf_r, ready_r);
|
||||||
ready_l = apply_pre_emphasis(&preemp_l, ready_l)*2;
|
ready_l = apply_preemphasis(&preemp_l, ready_l);
|
||||||
ready_r = apply_pre_emphasis(&preemp_r, ready_r)*2;
|
ready_r = apply_preemphasis(&preemp_r, ready_r);
|
||||||
ready_l = soft_clip(ready_l, soft_clipper_threshold);
|
ready_l = soft_clip(ready_l, soft_clipper_threshold);
|
||||||
ready_r = soft_clip(ready_r, soft_clipper_threshold);
|
ready_r = soft_clip(ready_r, soft_clipper_threshold);
|
||||||
ready_l = hard_clip(ready_l, clipper_threshold);
|
ready_l = hard_clip(ready_l, clipper_threshold);
|
||||||
|
|||||||
Reference in New Issue
Block a user