mirror of
https://github.com/radio95-rnt/fm95.git
synced 2026-02-27 03:23:54 +01:00
i hate this with a passion
This commit is contained in:
@@ -1,51 +1,13 @@
|
|||||||
#include "filters.h"
|
#include "filters.h"
|
||||||
|
|
||||||
void init_preemphasis(BiquadFilter *filter, float tau, float sample_rate) {
|
void init_preemphasis(ResistorCapacitor *filter, float tau, float sample_rate) {
|
||||||
/*
|
filter->prev_sample = 0.0f;
|
||||||
premphasis should go like this:
|
filter->alpha = exp(-1 / (tau*sample_rate));
|
||||||
cutoff: +0
|
|
||||||
2cutoff: +6
|
|
||||||
3cutoff: +12
|
|
||||||
4cutoff: +18
|
|
||||||
...
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Calculate the cutoff frequency from tau (f_c = 1/(2πτ))
|
|
||||||
float cutoff_freq = 1.0f / (2.0f * M_PI * tau);
|
|
||||||
|
|
||||||
// Pre-warp the cutoff frequency for the bilinear transform
|
|
||||||
float omega = 2.0f * M_PI * cutoff_freq / sample_rate;
|
|
||||||
float K = tanf(omega / 2.0f);
|
|
||||||
|
|
||||||
// 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) {
|
float apply_preemphasis(ResistorCapacitor *filter, float sample) {
|
||||||
// Direct Form I implementation
|
float out = sample-filter->alpha*filter->prev_sample;
|
||||||
float output = filter->b0 * input
|
filter->prev_sample = sample;
|
||||||
+ filter->b1 * filter->x1
|
return out;
|
||||||
+ 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(BiquadFilter* filter, float cutoffFreq, float qFactor, float sampleRate) {
|
void init_lpf(BiquadFilter* filter, float cutoffFreq, float qFactor, float sampleRate) {
|
||||||
|
|||||||
@@ -5,16 +5,21 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "constants.h"
|
#include "constants.h"
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
float alpha;
|
||||||
|
float prev_sample;
|
||||||
|
} ResistorCapacitor;
|
||||||
|
|
||||||
|
void init_preemphasis(ResistorCapacitor *filter, float tau, float sample_rate);
|
||||||
|
float apply_preemphasis(ResistorCapacitor *filter, float sample);
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
float b0, b1, b2;
|
float b0, b1, b2;
|
||||||
float a1, a2;
|
float a1, a2;
|
||||||
float x1, x2;
|
float x1, x2;
|
||||||
float y1, y2;
|
float y1, y2;
|
||||||
} BiquadFilter;
|
} BiquadFilter;
|
||||||
|
|
||||||
void init_preemphasis(BiquadFilter *filter, float tau, float sample_rate);
|
|
||||||
float apply_preemphasis(BiquadFilter *filter, float input);
|
|
||||||
|
|
||||||
void init_lpf(BiquadFilter* filter, float cutoffFreq, float qFactor, float sampleRate);
|
void init_lpf(BiquadFilter* filter, float cutoffFreq, float qFactor, float sampleRate);
|
||||||
void init_hpf(BiquadFilter* filter, float cutoffFreq, float qFactor, float sampleRate);
|
void init_hpf(BiquadFilter* filter, float cutoffFreq, float qFactor, float sampleRate);
|
||||||
float apply_frequency_filter(BiquadFilter* filter, float input);
|
float apply_frequency_filter(BiquadFilter* filter, float input);
|
||||||
|
|||||||
@@ -396,7 +396,7 @@ 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);
|
||||||
|
|
||||||
BiquadFilter 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);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user