From 81570e47339cca190b9eb365367cfa3eacd9ac37 Mon Sep 17 00:00:00 2001 From: SaucySoliton Date: Wed, 11 Jan 2017 03:43:37 +0000 Subject: [PATCH] FIR filter speedup, reduced memory usage --- src/fm_mpx.c | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/fm_mpx.c b/src/fm_mpx.c index 8783dca..dfa0b52 100644 --- a/src/fm_mpx.c +++ b/src/fm_mpx.c @@ -36,13 +36,12 @@ #define FIR_PHASES (32) -#define FIR_SIZE (1024) // MUST be a power of 2 for the circular buffer -#define FIR_TAPS (FIR_SIZE/FIR_PHASES) +#define FIR_TAPS (32) // MUST be a power of 2 for the circular buffer size_t length; // coefficients of the low-pass FIR filter -float low_pass_fir[FIR_SIZE]; +float low_pass_fir[FIR_PHASES][FIR_TAPS]; float carrier_38[] = {0.0, 0.8660254037844386, 0.8660254037844388, 1.2246467991473532e-16, -0.8660254037844384, -0.8660254037844386}; @@ -61,8 +60,8 @@ int audio_index = 0; int audio_len = 0; float audio_pos; -float fir_buffer_left[FIR_SIZE] = {0}; -float fir_buffer_right[FIR_SIZE] = {0}; +float fir_buffer_left[FIR_TAPS] = {0}; +float fir_buffer_right[FIR_TAPS] = {0}; int fir_index = 0; int channels; float left_max=1, right_max=1; // start compressor with low gain @@ -140,9 +139,11 @@ int fm_mpx_open(char *filename, size_t len) { b1=( 2.0*bp + 1/(in_samplerate*FIR_PHASES) )/(2.0*bp + 1/(in_samplerate*FIR_PHASES) ); double x=0,y=0; - for(int i=1; i<=FIR_SIZE; i++) { // match indexing of Matlab script - sincpos = i-((FIR_SIZE+1.0)/2.0); // offset by 0.5 so sincpos!=0 (causes NaN x/0 ) - //printf("%d=%f ", i,sincpos); + for(int i=0; i= FIR_SIZE) fir_index = 0; + if(fir_index >= FIR_TAPS) fir_index = 0; // Store the current sample(s) into the FIR filter's ring buffer fir_buffer_left[fir_index] = audio_buffer[audio_index]; if(channels > 1) { @@ -232,23 +234,21 @@ int fm_mpx_get_samples(float *mpx_buffer) { //int iphase=FIR_PHASES-1; // test override //printf("%d %d \n",fir_index,iphase); // diagnostics // Sanity checks - if ( iphase < 0 ) {iphase=0; printf("low\n"); } + if ( iphase < 0 ) {iphase=0; printf("low\n"); }// Seems to run faster with these checks in place if ( iphase >= FIR_PHASES ) {iphase=FIR_PHASES-2; printf("high\n"); } - int fir_start = (fir_index - FIR_TAPS); - if( fir_start < 0 ) fir_start+=FIR_SIZE; if( channels > 1 ) { for(int fi=0; fi