andrewm@0: // andrewm@0: // Biquad.h andrewm@0: // andrewm@0: // Created by Nigel Redmon on 11/24/12 andrewm@0: // EarLevel Engineering: earlevel.com andrewm@0: // Copyright 2012 Nigel Redmon andrewm@0: // andrewm@0: // For a complete explanation of the Biquad code: andrewm@0: // http://www.earlevel.com/main/2012/11/25/biquad-c-source-code/ andrewm@0: // andrewm@0: // License: andrewm@0: // andrewm@0: // This source code is provided as is, without warranty. andrewm@0: // You may copy and distribute verbatim copies of this document. andrewm@0: // You may modify and use this source code to create binary code andrewm@0: // for your own purposes, free or commercial. andrewm@0: // andrewm@0: andrewm@0: #ifndef Biquad_h andrewm@0: #define Biquad_h andrewm@0: andrewm@0: enum { andrewm@0: bq_type_lowpass = 0, andrewm@0: bq_type_highpass, andrewm@0: bq_type_bandpass, andrewm@0: bq_type_notch, andrewm@0: bq_type_peak, andrewm@0: bq_type_lowshelf, andrewm@0: bq_type_highshelf andrewm@0: }; andrewm@0: andrewm@0: class Biquad { andrewm@0: public: andrewm@0: Biquad(); andrewm@0: Biquad(int type, double Fc, double Q, double peakGainDB); andrewm@0: ~Biquad(); andrewm@0: void setType(int type); andrewm@0: void setQ(double Q); andrewm@0: void setFc(double Fc); andrewm@0: void setPeakGain(double peakGainDB); andrewm@0: void setBiquad(int type, double Fc, double Q, double peakGain); andrewm@0: float process(float in); andrewm@0: andrewm@0: double getQ(); andrewm@0: double getFc(); andrewm@0: double getPeakGain(); andrewm@0: andrewm@0: double getStartingQ(); andrewm@0: double getStartingFc(); andrewm@0: double getStartingPeakGain(); andrewm@0: andrewm@0: protected: andrewm@0: void calcBiquad(void); andrewm@0: andrewm@0: int type; andrewm@0: double a0, a1, a2, b1, b2; andrewm@0: double Fc, Q, peakGain; andrewm@0: double startFc, startQ, startPeakGain; andrewm@0: double z1, z2; andrewm@0: }; andrewm@0: andrewm@0: inline double Biquad::getQ() andrewm@0: { andrewm@0: return Q; andrewm@0: } andrewm@0: andrewm@0: inline double Biquad::getFc() andrewm@0: { andrewm@0: return Fc; andrewm@0: } andrewm@0: andrewm@0: inline double Biquad::getPeakGain() andrewm@0: { andrewm@0: return peakGain; andrewm@0: } andrewm@0: andrewm@0: inline double Biquad::getStartingQ() andrewm@0: { andrewm@0: return startQ; andrewm@0: } andrewm@0: andrewm@0: inline double Biquad::getStartingFc() andrewm@0: { andrewm@0: return startFc; andrewm@0: } andrewm@0: andrewm@0: inline double Biquad::getStartingPeakGain() andrewm@0: { andrewm@0: return startPeakGain; andrewm@0: } andrewm@0: andrewm@0: inline float Biquad::process(float in) { andrewm@0: double out = in * a0 + z1; andrewm@0: z1 = in * a1 + z2 - b1 * out; andrewm@0: z2 = in * a2 - b2 * out; andrewm@0: return out; andrewm@0: } andrewm@0: andrewm@0: #endif // Biquad_h