0
1
mirror of https://github.com/radio95-rnt/fm95.git synced 2026-02-26 11:22:00 +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));
}
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 a2 = a*a;
float r, e;
for(int i = 0; i < FIR_ORDER; i++) {
r = sinf(M_PI*(2.0f*i+1.0f)/(4.0f*FIR_ORDER));
for(int i = 0; i < LPF_ORDER; i++) {
r = sinf(M_PI*(2.0f*i+1.0f)/(4.0f*LPF_ORDER));
e = a2+2.0f*a*r+1.0f;
filter->A[i] = 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 output;
for(int i = 0; i < FIR_ORDER; i++) {
filter->w0[i] = filter->d1[i]*filter->w1[i] + filter->d2[i]*filter->w2[i] + x;
output = filter->A[i] * (filter->w0[i] + 2.0f * filter->w1[i] + filter->w2[i]);
float process_lpf(LPFFilter *filter, float x) {
float y = x;
for(int i = 0; i < LPF_ORDER; i++) {
filter->w0[i] = filter->d1[i] * filter->w1[i] + filter->d2[i] * filter->w2[i] + y;
y = filter->A[i] * (filter->w0[i] + 2.0f * filter->w1[i] + filter->w2[i]);
filter->w2[i] = filter->w1[i];
filter->w1[i] = filter->w0[i];
}
return output;
}
return y;
}

View File

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