diff options
Diffstat (limited to 'src/lowpass.c')
-rw-r--r-- | src/lowpass.c | 278 |
1 files changed, 139 insertions, 139 deletions
diff --git a/src/lowpass.c b/src/lowpass.c index 304ad9f..ccb41dd 100644 --- a/src/lowpass.c +++ b/src/lowpass.c @@ -1,8 +1,8 @@ /* -Resonant low pass filter source code. -By baltrax@hotmail.com (Zxform) + Resonant low pass filter source code. + By baltrax@hotmail.com (Zxform) -- little changes and optimizations by Brett Paterson for FMOD example. + - little changes and optimizations by Brett Paterson for FMOD example. */ @@ -46,79 +46,79 @@ FILTER iir; * Allocation errors cause an error message and a call to exit. * -------------------------------------------------------------------- */ -float LowPass_Filter(float input) +float lpf_filter(float input) { - unsigned int i; - float *hist1_ptr,*hist2_ptr,*coef_ptr; - float output,new_hist,history1,history2; - static float dc = (float)1E-25; - input += dc; - dc = -dc; + unsigned int i; + float *hist1_ptr,*hist2_ptr,*coef_ptr; + float output,new_hist,history1,history2; + static float dc = (float)1E-25; + input += dc; + dc = -dc; - /* allocate history array if different size than last call */ + /* allocate history array if different size than last call */ - coef_ptr = iir.coef; /* coefficient pointer */ + coef_ptr = iir.coef; /* coefficient pointer */ - hist1_ptr = iir.history; /* first history */ - hist2_ptr = hist1_ptr + 1; /* next history */ + hist1_ptr = iir.history; /* first history */ + hist2_ptr = hist1_ptr + 1; /* next history */ - /* 1st number of coefficients array is overall input scale factor, - * or filter gain */ - output = input * (*coef_ptr++); + /* 1st number of coefficients array is overall input scale factor, + * or filter gain */ + output = input * (*coef_ptr++); - for (i = 0 ; i < iir.length; i++) + for (i = 0 ; i < iir.length; i++) { - history1 = *hist1_ptr; /* history values */ - history2 = *hist2_ptr; + history1 = *hist1_ptr; /* history values */ + history2 = *hist2_ptr; - output = output - history1 * coef_ptr[0]; - new_hist = output - history2 * coef_ptr[1]; /* poles */ + output = output - history1 * coef_ptr[0]; + new_hist = output - history2 * coef_ptr[1]; /* poles */ - output = new_hist + history1 * coef_ptr[2]; - output = output + history2 * coef_ptr[3]; /* zeros */ + output = new_hist + history1 * coef_ptr[2]; + output = output + history2 * coef_ptr[3]; /* zeros */ - coef_ptr += 4; - *hist2_ptr++ = *hist1_ptr; - *hist1_ptr++ = new_hist; - hist1_ptr++; - hist2_ptr++; + coef_ptr += 4; + *hist2_ptr++ = *hist1_ptr; + *hist1_ptr++ = new_hist; + hist1_ptr++; + hist2_ptr++; } - return(output); + return(output); } -void LowPass_Update(float resonance, float cutoff, int samplerate) +void lpf_update(float resonance, float cutoff, int samplerate) { - unsigned nInd; - double a0, a1, a2, b0, b1, b2; - double fs; /* Sampling frequency, cutoff frequency */ - double k; /* overall gain factor */ - float *coef; - - k = 1.0; /* Set overall filter gain */ - coef = iir.coef + 1; /* Skip k, or gain */ - fs = (double)samplerate; /* Sampling frequency (Hz) */ - -/* - * Compute z-domain coefficients for each biquad section - * for new Cutoff Frequency and Resonance - */ - for (nInd = 0; nInd < iir.length; nInd++) + unsigned nInd; + double a0, a1, a2, b0, b1, b2; + double fs; /* Sampling frequency, cutoff frequency */ + double k; /* overall gain factor */ + float *coef; + + k = 1.0; /* Set overall filter gain */ + coef = iir.coef + 1; /* Skip k, or gain */ + fs = (double)samplerate; /* Sampling frequency (Hz) */ + + /* + * Compute z-domain coefficients for each biquad section + * for new Cutoff Frequency and Resonance + */ + for (nInd = 0; nInd < iir.length; nInd++) { - a0 = ProtoCoef[nInd].a0; - a1 = ProtoCoef[nInd].a1; - a2 = ProtoCoef[nInd].a2; - - b0 = ProtoCoef[nInd].b0; - b1 = ProtoCoef[nInd].b1 / resonance; /* Divide by resonance or Q */ - b2 = ProtoCoef[nInd].b2; - szxform(&a0, &a1, &a2, &b0, &b1, &b2, cutoff, fs, &k, coef); - coef += 4; /* Point to next filter section */ + a0 = ProtoCoef[nInd].a0; + a1 = ProtoCoef[nInd].a1; + a2 = ProtoCoef[nInd].a2; + + b0 = ProtoCoef[nInd].b0; + b1 = ProtoCoef[nInd].b1 / resonance; /* Divide by resonance or Q */ + b2 = ProtoCoef[nInd].b2; + szxform(&a0, &a1, &a2, &b0, &b1, &b2, cutoff, fs, &k, coef); + coef += 4; /* Point to next filter section */ } - /* Update overall filter gain in coef array */ - iir.coef[0] = (float)k; + /* Update overall filter gain in coef array */ + iir.coef[0] = (float)k; } @@ -132,54 +132,54 @@ void LowPass_Update(float resonance, float cutoff, int samplerate) * of two second order sections. * -------------------------------------------------------------------- */ -signed char LowPass_Init() +signed char lpf_init() { -/* - * Setup filter s-domain coefficients - */ - /* Section 1 */ - ProtoCoef[0].a0 = 1.0; - ProtoCoef[0].a1 = 0; - ProtoCoef[0].a2 = 0; - ProtoCoef[0].b0 = 1.0; - ProtoCoef[0].b1 = 0.765367; - ProtoCoef[0].b2 = 1.0; + /* + * Setup filter s-domain coefficients + */ + /* Section 1 */ + ProtoCoef[0].a0 = 1.0; + ProtoCoef[0].a1 = 0; + ProtoCoef[0].a2 = 0; + ProtoCoef[0].b0 = 1.0; + ProtoCoef[0].b1 = 0.765367; + ProtoCoef[0].b2 = 1.0; - /* Section 2 */ - ProtoCoef[1].a0 = 1.0; - ProtoCoef[1].a1 = 0; - ProtoCoef[1].a2 = 0; - ProtoCoef[1].b0 = 1.0; - ProtoCoef[1].b1 = 1.847759; - ProtoCoef[1].b2 = 1.0; + /* Section 2 */ + ProtoCoef[1].a0 = 1.0; + ProtoCoef[1].a1 = 0; + ProtoCoef[1].a2 = 0; + ProtoCoef[1].b0 = 1.0; + ProtoCoef[1].b1 = 1.847759; + ProtoCoef[1].b2 = 1.0; + + iir.length = FILTER_SECTIONS; /* Number of filter sections */ + + /* + * Allocate array of z-domain coefficients for each filter section + * plus filter gain variable + */ + iir.coef = (float *) calloc(4 * iir.length + 1, sizeof(float)); + if (!iir.coef) + { + // printf("Unable to allocate coef array, exiting\n"); + return 0; + } - iir.length = FILTER_SECTIONS; /* Number of filter sections */ + lpf_update(1.0, 5000.0, 44100); -/* - * Allocate array of z-domain coefficients for each filter section - * plus filter gain variable - */ - iir.coef = (float *) calloc(4 * iir.length + 1, sizeof(float)); - if (!iir.coef) - { -// printf("Unable to allocate coef array, exiting\n"); - return 0; - } - - LowPass_Update(1.0, 5000.0, 44100); - - /* Display filter coefficients */ -// for (nInd = 0; nInd < (iir.length * 4 + 1); nInd++) -// printf("C[%d] = %15.10f\n", nInd, iir.coef[nInd]); -/* - * To process audio samples, call function iir_filter() - * for each audio sample - */ - return 1; + /* Display filter coefficients */ + // for (nInd = 0; nInd < (iir.length * 4 + 1); nInd++) + // printf("C[%d] = %15.10f\n", nInd, iir.coef[nInd]); + /* + * To process audio samples, call function iir_filter() + * for each audio sample + */ + return 1; } -void LowPass_Close() +void lpf_close() { } @@ -254,11 +254,11 @@ void LowPass_Close() void prewarp(double *a0, double *a1, double *a2, double fc, double fs); void bilinear( - double a0, double a1, double a2, /* numerator coefficients */ - double b0, double b1, double b2, /* denominator coefficients */ - double *k, /* overall gain factor */ - double fs, /* sampling rate */ - float *coef); /* pointer to 4 iir coefficients */ + double a0, double a1, double a2, /* numerator coefficients */ + double b0, double b1, double b2, /* denominator coefficients */ + double *k, /* overall gain factor */ + double fs, /* sampling rate */ + float *coef); /* pointer to 4 iir coefficients */ /* @@ -269,16 +269,16 @@ void bilinear( * ---------------------------------------------------------- */ void prewarp( - double *a0, double *a1, double *a2, - double fc, double fs) + double *a0, double *a1, double *a2, + double fc, double fs) { - double wp, pi; + double wp, pi; - pi = 4.0 * atan(1.0); - wp = 2.0 * fs * tan(pi * fc / fs); + pi = 4.0 * atan(1.0); + wp = 2.0 * fs * tan(pi * fc / fs); - *a2 = (*a2) / (wp * wp); - *a1 = (*a1) / wp; + *a2 = (*a2) / (wp * wp); + *a1 = (*a1) / wp; } @@ -312,30 +312,30 @@ void prewarp( * ---------------------------------------------------------- */ void bilinear( - double a0, double a1, double a2, /* numerator coefficients */ - double b0, double b1, double b2, /* denominator coefficients */ - double *k, /* overall gain factor */ - double fs, /* sampling rate */ - float *coef /* pointer to 4 iir coefficients */ -) + double a0, double a1, double a2, /* numerator coefficients */ + double b0, double b1, double b2, /* denominator coefficients */ + double *k, /* overall gain factor */ + double fs, /* sampling rate */ + float *coef /* pointer to 4 iir coefficients */ + ) { - double ad, bd; + double ad, bd; - /* alpha (Numerator in s-domain) */ - ad = 4. * a2 * fs * fs + 2. * a1 * fs + a0; - /* beta (Denominator in s-domain) */ - bd = 4. * b2 * fs * fs + 2. * b1* fs + b0; + /* alpha (Numerator in s-domain) */ + ad = 4. * a2 * fs * fs + 2. * a1 * fs + a0; + /* beta (Denominator in s-domain) */ + bd = 4. * b2 * fs * fs + 2. * b1* fs + b0; - /* update gain constant for this section */ - *k *= ad/bd; + /* update gain constant for this section */ + *k *= ad/bd; - /* Denominator */ - *coef++ = (float)((2. * b0 - 8. * b2 * fs * fs) / bd); /* beta1 */ - *coef++ = (float)((4. * b2 * fs * fs - 2. * b1 * fs + b0) / bd); /* beta2 */ + /* Denominator */ + *coef++ = (float)((2. * b0 - 8. * b2 * fs * fs) / bd); /* beta1 */ + *coef++ = (float)((4. * b2 * fs * fs - 2. * b1 * fs + b0) / bd); /* beta2 */ - /* Nominator */ - *coef++ = (float)((2. * a0 - 8. * a2 * fs * fs) / ad); /* alpha1 */ - *coef = (float)((4. * a2 * fs * fs - 2. * a1 * fs + a0) / ad); /* alpha2 */ + /* Nominator */ + *coef++ = (float)((2. * a0 - 8. * a2 * fs * fs) / ad); /* alpha1 */ + *coef = (float)((4. * a2 * fs * fs - 2. * a1 * fs + a0) / ad); /* alpha2 */ } @@ -355,17 +355,17 @@ void bilinear( * ---------------------------------------------------------- */ void szxform( - double *a0, double *a1, double *a2, /* numerator coefficients */ - double *b0, double *b1, double *b2, /* denominator coefficients */ - double fc, /* Filter cutoff frequency */ - double fs, /* sampling rate */ - double *k, /* overall gain factor */ - float *coef) /* pointer to 4 iir coefficients */ + double *a0, double *a1, double *a2, /* numerator coefficients */ + double *b0, double *b1, double *b2, /* denominator coefficients */ + double fc, /* Filter cutoff frequency */ + double fs, /* sampling rate */ + double *k, /* overall gain factor */ + float *coef) /* pointer to 4 iir coefficients */ { - /* Calculate a1 and a2 and overwrite the original values */ - prewarp(a0, a1, a2, fc, fs); - prewarp(b0, b1, b2, fc, fs); - bilinear(*a0, *a1, *a2, *b0, *b1, *b2, k, fs, coef); + /* Calculate a1 and a2 and overwrite the original values */ + prewarp(a0, a1, a2, fc, fs); + prewarp(b0, b1, b2, fc, fs); + bilinear(*a0, *a1, *a2, *b0, *b1, *b2, k, fs, coef); } |