From e8783ef6b87c50e448be819c1a89e68ff129dbed Mon Sep 17 00:00:00 2001 From: Benjamin Date: Tue, 19 Jul 2016 00:44:38 +0200 Subject: [PATCH] Add shelving filter support for biquad --- filter_biquad.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/filter_biquad.h b/filter_biquad.h index 9b7963dc..3775a48f 100644 --- a/filter_biquad.h +++ b/filter_biquad.h @@ -109,6 +109,44 @@ class AudioFilterBiquad : public AudioStream /* a2 */ coef[4] = (1.0 - alpha) * scale; setCoefficients(stage, coef); } + void setLowShelf(uint32_t stage, float frequency, float gain, float slope = 1.0f) { + int coef[5]; + double a = pow(10.0, gain/40.0); + double w0 = frequency * (2 * 3.141592654 / AUDIO_SAMPLE_RATE_EXACT); + double sinW0 = sin(w0); + //double alpha = (sinW0 * sqrt((a+1/a)*(1/slope-1)+2) ) / 2.0; + double cosW0 = cos(w0); + //generate three helper-values (intermediate results): + double sinsq = sinW0 * sqrt( (pow(a,2.0)+1.0)*(1.0/slope-1.0)+2.0*a ); + double aMinus = (a-1.0)*cosW0; + double aPlus = (a+1.0)*cosW0; + double scale = 1073741824.0 / ( (a+1.0) + aMinus + sinsq); + /* b0 */ coef[0] = a * ( (a+1.0) - aMinus + sinsq ) * scale; + /* b1 */ coef[1] = 2.0*a * ( (a-1.0) - aPlus ) * scale; + /* b2 */ coef[2] = a * ( (a+1.0) - aMinus - sinsq ) * scale; + /* a1 */ coef[3] = -2.0* ( (a-1.0) + aPlus ) * scale; + /* a2 */ coef[4] = ( (a+1.0) + aMinus - sinsq ) * scale; + setCoefficients(stage, coef); + } + void setHighShelf(uint32_t stage, float frequency, float gain, float slope = 1.0f) { + int coef[5]; + double a = pow(10.0, gain/40.0); + double w0 = frequency * (2 * 3.141592654 / AUDIO_SAMPLE_RATE_EXACT); + double sinW0 = sin(w0); + //double alpha = (sinW0 * sqrt((a+1/a)*(1/slope-1)+2) ) / 2.0; + double cosW0 = cos(w0); + //generate three helper-values (intermediate results): + double sinsq = sinW0 * sqrt( (pow(a,2.0)+1.0)*(1.0/slope-1.0)+2.0*a ); + double aMinus = (a-1.0)*cosW0; + double aPlus = (a+1.0)*cosW0; + double scale = 1073741824.0 / ( (a+1.0) - aMinus + sinsq); + /* b0 */ coef[0] = a * ( (a+1.0) + aMinus + sinsq ) * scale; + /* b1 */ coef[1] = -2.0*a * ( (a-1.0) + aPlus ) * scale; + /* b2 */ coef[2] = a * ( (a+1.0) + aMinus - sinsq ) * scale; + /* a1 */ coef[3] = 2.0* ( (a-1.0) - aPlus ) * scale; + /* a2 */ coef[4] = ( (a+1.0) - aMinus - sinsq ) * scale; + setCoefficients(stage, coef); + } private: int32_t definition[32]; // up to 4 cascaded biquads