mirror of
https://github.com/radio95-rnt/fm95.git
synced 2026-02-26 19:23:51 +01:00
turn lpf and emphasis into a common rc
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": 1735989625302,
|
"time": 1737462229390,
|
||||||
"version": "0.0.3"
|
"version": "0.0.3"
|
||||||
}
|
}
|
||||||
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
@@ -9,6 +9,8 @@
|
|||||||
"stdlib.h": "c",
|
"stdlib.h": "c",
|
||||||
"error.h": "c",
|
"error.h": "c",
|
||||||
"math.h": "c",
|
"math.h": "c",
|
||||||
"options.h": "c"
|
"options.h": "c",
|
||||||
|
"random": "c",
|
||||||
|
"__locale": "c"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,25 +1,23 @@
|
|||||||
#include "filters.h"
|
#include "filters.h"
|
||||||
|
|
||||||
void init_emphasis(Emphasis *pe, float tau, float sample_rate) {
|
void init_rc(ResistorCapacitor *rc, float tau, float sample_rate) {
|
||||||
pe->prev_sample = 0.0f;
|
rc->prev_sample = 0.0f;
|
||||||
pe->alpha = exp(-1 / (tau * sample_rate));
|
rc->alpha = exp(-1 / (tau * sample_rate));
|
||||||
}
|
}
|
||||||
|
|
||||||
float apply_pre_emphasis(Emphasis *pe, float sample) {
|
float apply_pre_emphasis(ResistorCapacitor *rc, float sample) {
|
||||||
float audio = sample-pe->alpha*pe->prev_sample;
|
float audio = sample-rc->alpha*rc->prev_sample;
|
||||||
pe->prev_sample = audio;
|
rc->prev_sample = audio;
|
||||||
return audio*2;
|
return audio*2;
|
||||||
}
|
}
|
||||||
|
|
||||||
void init_low_pass_filter(LowPassFilter *lp, float cutoff_frequency, float sample_rate) {
|
void init_low_pass_filter(ResistorCapacitor *rc, float cutoff_frequency, float sample_rate) {
|
||||||
float rc = 1/(M_2PI*cutoff_frequency);
|
init_rc(&rc, (-1 / (sample_rate * log(sample_rate/(sample_rate+(1/(M_2PI*cutoff_frequency)))))), sample_rate);
|
||||||
lp->alpha = sample_rate/(sample_rate+rc);
|
|
||||||
lp->prev_sample = 0.0f;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float apply_low_pass_filter(LowPassFilter *lp, float sample) {
|
float apply_low_pass_filter(ResistorCapacitor *rc, float sample) {
|
||||||
float output = lp->alpha*sample+(1-lp->alpha)*lp->prev_sample;
|
float output = rc->alpha*sample+(1-rc->alpha)*rc->prev_sample;
|
||||||
lp->prev_sample = output;
|
rc->prev_sample = output;
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,18 +11,13 @@
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
float alpha;
|
float alpha;
|
||||||
float prev_sample;
|
float prev_sample;
|
||||||
} Emphasis;
|
} ResistorCapacitor;
|
||||||
|
|
||||||
void init_emphasis(Emphasis *pe, float tau, float sample_rate);
|
void init_rc(ResistorCapacitor *pe, float tau, float sample_rate);
|
||||||
float apply_pre_emphasis(Emphasis *pe, float sample);
|
float apply_pre_emphasis(ResistorCapacitor *pe, float sample);
|
||||||
|
|
||||||
typedef struct {
|
void init_low_pass_filter(ResistorCapacitor *lp, float cutoff_frequency, float sample_rate);
|
||||||
float alpha;
|
float apply_low_pass_filter(ResistorCapacitor *lp, float sample);
|
||||||
float prev_sample;
|
|
||||||
} LowPassFilter;
|
|
||||||
|
|
||||||
void init_low_pass_filter(LowPassFilter *lp, float cutoff_frequency, float sample_rate);
|
|
||||||
float apply_low_pass_filter(LowPassFilter *lp, float sample);
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|||||||
@@ -122,9 +122,9 @@ int main() {
|
|||||||
Oscillator osc;
|
Oscillator osc;
|
||||||
init_oscillator(&osc, 50000.0, SAMPLE_RATE);
|
init_oscillator(&osc, 50000.0, SAMPLE_RATE);
|
||||||
#ifdef PREEMPHASIS
|
#ifdef PREEMPHASIS
|
||||||
Emphasis preemp_l, preemp_r;
|
ResistorCapacitor preemp_l, preemp_r;
|
||||||
init_emphasis(&preemp_l, PREEMPHASIS_TAU, SAMPLE_RATE);
|
init_rc(&preemp_l, PREEMPHASIS_TAU, SAMPLE_RATE);
|
||||||
init_emphasis(&preemp_r, PREEMPHASIS_TAU, SAMPLE_RATE);
|
init_rc(&preemp_r, PREEMPHASIS_TAU, SAMPLE_RATE);
|
||||||
#endif
|
#endif
|
||||||
#ifdef LPF
|
#ifdef LPF
|
||||||
LowPassFilter lpf_l, lpf_r;
|
LowPassFilter lpf_l, lpf_r;
|
||||||
|
|||||||
@@ -104,8 +104,8 @@ int main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef PREEMPHASIS
|
#ifdef PREEMPHASIS
|
||||||
Emphasis preemp;
|
ResistorCapacitor preemp;
|
||||||
init_emphasis(&preemp, PREEMPHASIS_TAU, SAMPLE_RATE);
|
init_rc(&preemp, PREEMPHASIS_TAU, SAMPLE_RATE);
|
||||||
#endif
|
#endif
|
||||||
#ifdef LPF
|
#ifdef LPF
|
||||||
LowPassFilter lpf;
|
LowPassFilter lpf;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
// #define PREEMPHASIS
|
#define PREEMPHASIS
|
||||||
// #define LPF
|
// #define LPF
|
||||||
|
|
||||||
#define buffer_maxlength 12288
|
#define buffer_maxlength 12288
|
||||||
|
|||||||
@@ -121,9 +121,9 @@ int main() {
|
|||||||
Oscillator stereo_osc;
|
Oscillator stereo_osc;
|
||||||
init_oscillator(&stereo_osc, 31250.0, SAMPLE_RATE);
|
init_oscillator(&stereo_osc, 31250.0, SAMPLE_RATE);
|
||||||
#ifdef PREEMPHASIS
|
#ifdef PREEMPHASIS
|
||||||
Emphasis preemp_l, preemp_r;
|
ResistorCapacitor preemp_l, preemp_r;
|
||||||
init_emphasis(&preemp_l, PREEMPHASIS_TAU, SAMPLE_RATE);
|
init_rc(&preemp_l, PREEMPHASIS_TAU, SAMPLE_RATE);
|
||||||
init_emphasis(&preemp_r, PREEMPHASIS_TAU, SAMPLE_RATE);
|
init_rc(&preemp_r, PREEMPHASIS_TAU, SAMPLE_RATE);
|
||||||
#endif
|
#endif
|
||||||
#ifdef LPF
|
#ifdef LPF
|
||||||
LowPassFilter lpf_l, lpf_r;
|
LowPassFilter lpf_l, lpf_r;
|
||||||
|
|||||||
@@ -112,8 +112,8 @@ int main() {
|
|||||||
FMModulator mod;
|
FMModulator mod;
|
||||||
init_fm_modulator(&mod, FREQUENCY, DEVIATION, SAMPLE_RATE);
|
init_fm_modulator(&mod, FREQUENCY, DEVIATION, SAMPLE_RATE);
|
||||||
#ifdef PREEMPHASIS
|
#ifdef PREEMPHASIS
|
||||||
Emphasis preemp;
|
ResistorCapacitor preemp;
|
||||||
init_emphasis(&preemp, PREEMPHASIS_TAU, SAMPLE_RATE);
|
init_rc(&preemp, PREEMPHASIS_TAU, SAMPLE_RATE);
|
||||||
#endif
|
#endif
|
||||||
#ifdef LPF
|
#ifdef LPF
|
||||||
LowPassFilter lpf;
|
LowPassFilter lpf;
|
||||||
|
|||||||
@@ -128,9 +128,9 @@ int main() {
|
|||||||
DelayLine monoDelay;
|
DelayLine monoDelay;
|
||||||
init_delay_line(&monoDelay, 99);
|
init_delay_line(&monoDelay, 99);
|
||||||
#ifdef PREEMPHASIS
|
#ifdef PREEMPHASIS
|
||||||
Emphasis preemp_l, preemp_r;
|
ResistorCapacitor preemp_l, preemp_r;
|
||||||
init_emphasis(&preemp_l, PREEMPHASIS_TAU, SAMPLE_RATE);
|
init_rc(&preemp_l, PREEMPHASIS_TAU, SAMPLE_RATE);
|
||||||
init_emphasis(&preemp_r, PREEMPHASIS_TAU, SAMPLE_RATE);
|
init_rc(&preemp_r, PREEMPHASIS_TAU, SAMPLE_RATE);
|
||||||
#endif
|
#endif
|
||||||
#ifdef LPF
|
#ifdef LPF
|
||||||
LowPassFilter lpf_l, lpf_r;
|
LowPassFilter lpf_l, lpf_r;
|
||||||
|
|||||||
@@ -122,9 +122,9 @@ int main() {
|
|||||||
Oscillator pilot_osc;
|
Oscillator pilot_osc;
|
||||||
init_oscillator(&pilot_osc, 19000.0, SAMPLE_RATE); // Pilot, it's there to indicate stereo and as a refrence signal with the stereo carrier
|
init_oscillator(&pilot_osc, 19000.0, SAMPLE_RATE); // Pilot, it's there to indicate stereo and as a refrence signal with the stereo carrier
|
||||||
#ifdef PREEMPHASIS
|
#ifdef PREEMPHASIS
|
||||||
Emphasis preemp_l, preemp_r;
|
ResistorCapacitor preemp_l, preemp_r;
|
||||||
init_emphasis(&preemp_l, PREEMPHASIS_TAU, SAMPLE_RATE);
|
init_rc(&preemp_l, PREEMPHASIS_TAU, SAMPLE_RATE);
|
||||||
init_emphasis(&preemp_r, PREEMPHASIS_TAU, SAMPLE_RATE);
|
init_rc(&preemp_r, PREEMPHASIS_TAU, SAMPLE_RATE);
|
||||||
#endif
|
#endif
|
||||||
#ifdef LPF
|
#ifdef LPF
|
||||||
LowPassFilter lpf_l, lpf_r;
|
LowPassFilter lpf_l, lpf_r;
|
||||||
|
|||||||
@@ -124,9 +124,9 @@ int main() {
|
|||||||
init_fm_modulator(&mod_mono, 67000, 6000, SAMPLE_RATE);
|
init_fm_modulator(&mod_mono, 67000, 6000, SAMPLE_RATE);
|
||||||
init_fm_modulator(&mod_stereo, 80000, 6000, SAMPLE_RATE);
|
init_fm_modulator(&mod_stereo, 80000, 6000, SAMPLE_RATE);
|
||||||
#ifdef PREEMPHASIS
|
#ifdef PREEMPHASIS
|
||||||
Emphasis preemp_l, preemp_r;
|
ResistorCapacitor preemp_l, preemp_r;
|
||||||
init_emphasis(&preemp_l, PREEMPHASIS_TAU, SAMPLE_RATE);
|
init_rc(&preemp_l, PREEMPHASIS_TAU, SAMPLE_RATE);
|
||||||
init_emphasis(&preemp_r, PREEMPHASIS_TAU, SAMPLE_RATE);
|
init_rc(&preemp_r, PREEMPHASIS_TAU, SAMPLE_RATE);
|
||||||
#endif
|
#endif
|
||||||
#ifdef LPF
|
#ifdef LPF
|
||||||
LowPassFilter lpf_l, lpf_r;
|
LowPassFilter lpf_l, lpf_r;
|
||||||
|
|||||||
Reference in New Issue
Block a user