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

some lpf changes

This commit is contained in:
2025-03-27 18:53:49 +01:00
parent a9b53484b3
commit 3b1941038e
3 changed files with 21 additions and 21 deletions

View File

@@ -15,13 +15,13 @@ float hard_clip(float sample, float threshold) {
return fmaxf(-threshold, fminf(threshold, sample)); return fmaxf(-threshold, fminf(threshold, sample));
} }
void init_lpf(FIR *filter, float cutoff, int sample_rate) { void init_lpf(LPFFilter *filter, float cutoff, int sample_rate) {
float a = tanf(M_PI*cutoff/sample_rate); float a = tanf(M_PI*cutoff/sample_rate);
float a2 = a*a; float a2 = a*a;
float r, e; float r, e;
for(int i = 0; i < FIR_ORDER; i++) { for(int i = 0; i < LPF_ORDER; i++) {
r = sinf(M_PI*(2.0f*i+1.0f)/(4.0f*FIR_ORDER)); r = sinf(M_PI*(2.0f*i+1.0f)/(4.0f*LPF_ORDER));
e = a2+2.0f*a*r+1.0f; e = a2+2.0f*a*r+1.0f;
filter->A[i] = a2 / e; filter->A[i] = a2 / e;
filter->d1[i] = 2.0f*(1.0f-a2)/e; filter->d1[i] = 2.0f*(1.0f-a2)/e;
@@ -29,13 +29,13 @@ void init_lpf(FIR *filter, float cutoff, int sample_rate) {
} }
} }
float process_lpf(FIR *filter, float x) { float process_lpf(LPFFilter *filter, float x) {
float output; float y = x;
for(int i = 0; i < FIR_ORDER; i++) { for(int i = 0; i < LPF_ORDER; i++) {
filter->w0[i] = filter->d1[i]*filter->w1[i] + filter->d2[i]*filter->w2[i] + x; filter->w0[i] = filter->d1[i] * filter->w1[i] + filter->d2[i] * filter->w2[i] + y;
output = filter->A[i] * (filter->w0[i] + 2.0f * filter->w1[i] + filter->w2[i]); y = filter->A[i] * (filter->w0[i] + 2.0f * filter->w1[i] + filter->w2[i]);
filter->w2[i] = filter->w1[i]; filter->w2[i] = filter->w1[i];
filter->w1[i] = filter->w0[i]; filter->w1[i] = filter->w0[i];
} }
return output; return y;
} }

View File

@@ -5,7 +5,7 @@
#include "constants.h" #include "constants.h"
#include "oscillator.h" #include "oscillator.h"
#define FIR_ORDER 5 #define LPF_ORDER 5
typedef struct typedef struct
{ {
@@ -21,12 +21,12 @@ float hard_clip(float sample, float threshold);
typedef struct typedef struct
{ {
float A[FIR_ORDER]; float A[LPF_ORDER];
float d1[FIR_ORDER]; float d1[LPF_ORDER];
float d2[FIR_ORDER]; float d2[LPF_ORDER];
float w0[FIR_ORDER]; float w0[LPF_ORDER];
float w1[FIR_ORDER]; float w1[LPF_ORDER];
float w2[FIR_ORDER]; float w2[LPF_ORDER];
} FIR; } LPFFilter;
void init_lpf(FIR *filter, float cutoff, int sample_rate); void init_lpf(LPFFilter *filter, float cutoff, int sample_rate);
float process_lpf(FIR *filter, float x); float process_lpf(LPFFilter *filter, float x);

View File

@@ -414,7 +414,7 @@ int main(int argc, char **argv) {
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);
FIR lpf_l, lpf_r; LPFFilter lpf_l, lpf_r;
init_lpf(&lpf_l, 15000, sample_rate); init_lpf(&lpf_l, 15000, sample_rate);
init_lpf(&lpf_r, 15000, sample_rate); init_lpf(&lpf_r, 15000, sample_rate);