Biquad filter library in C++ template form. On mbed LPC1768, can run about 100K operations per second with double precision float, or about 200K with single precision float.
biquad.h@3:abd20f34fd68, 2012-02-16 (annotated)
- Committer:
- mhowellaz
- Date:
- Thu Feb 16 00:07:57 2012 +0000
- Revision:
- 3:abd20f34fd68
- Parent:
- 2:9a9b643fcc66
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mhowellaz | 0:f8ed62dbacbc | 1 | /* |
mhowellaz | 1:9c0546cd518e | 2 | * biquad.h |
mhowellaz | 0:f8ed62dbacbc | 3 | * |
mhowellaz | 3:abd20f34fd68 | 4 | * Original source material from Robert Bristow-Johnson at |
mhowellaz | 0:f8ed62dbacbc | 5 | * http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt |
mhowellaz | 3:abd20f34fd68 | 6 | * and Nigel Redmon at http://www.earlevel.com/ |
mhowellaz | 0:f8ed62dbacbc | 7 | * |
mhowellaz | 0:f8ed62dbacbc | 8 | * Derived from public domain C implementation by Tom St. Denis |
mhowellaz | 0:f8ed62dbacbc | 9 | * |
mhowellaz | 0:f8ed62dbacbc | 10 | * Thanks gents! |
mhowellaz | 0:f8ed62dbacbc | 11 | * |
mhowellaz | 0:f8ed62dbacbc | 12 | * C++-ification by Mark J. Howell |
mhowellaz | 0:f8ed62dbacbc | 13 | * |
mhowellaz | 0:f8ed62dbacbc | 14 | */ |
mhowellaz | 0:f8ed62dbacbc | 15 | |
mhowellaz | 2:9a9b643fcc66 | 16 | #ifndef __BIQUAD_H_ |
mhowellaz | 2:9a9b643fcc66 | 17 | #define __BIQUAD_H_ |
mhowellaz | 2:9a9b643fcc66 | 18 | |
mhowellaz | 0:f8ed62dbacbc | 19 | #include <math.h> |
mhowellaz | 0:f8ed62dbacbc | 20 | |
mhowellaz | 0:f8ed62dbacbc | 21 | #ifndef M_LN2 |
mhowellaz | 0:f8ed62dbacbc | 22 | #define M_LN2 0.69314718055994530942 |
mhowellaz | 0:f8ed62dbacbc | 23 | #endif |
mhowellaz | 0:f8ed62dbacbc | 24 | |
mhowellaz | 0:f8ed62dbacbc | 25 | #ifndef M_PI |
mhowellaz | 0:f8ed62dbacbc | 26 | #define M_PI 3.14159265358979323846 |
mhowellaz | 0:f8ed62dbacbc | 27 | #endif |
mhowellaz | 0:f8ed62dbacbc | 28 | |
mhowellaz | 3:abd20f34fd68 | 29 | /* formulation types */ |
mhowellaz | 3:abd20f34fd68 | 30 | typedef enum { |
mhowellaz | 3:abd20f34fd68 | 31 | DIRECT_FORM_I = 0, |
mhowellaz | 3:abd20f34fd68 | 32 | TRANSPOSED_DIRECT_FORM_II |
mhowellaz | 3:abd20f34fd68 | 33 | } BIQUAD_FORMULATION_TYPE; |
mhowellaz | 3:abd20f34fd68 | 34 | |
mhowellaz | 0:f8ed62dbacbc | 35 | /* filter types */ |
mhowellaz | 0:f8ed62dbacbc | 36 | typedef enum { |
mhowellaz | 0:f8ed62dbacbc | 37 | LPF, /* low pass filter */ |
mhowellaz | 0:f8ed62dbacbc | 38 | HPF, /* High pass filter */ |
mhowellaz | 0:f8ed62dbacbc | 39 | BPF, /* band pass filter */ |
mhowellaz | 0:f8ed62dbacbc | 40 | NOTCH, /* Notch Filter */ |
mhowellaz | 0:f8ed62dbacbc | 41 | PEQ, /* Peaking band EQ filter */ |
mhowellaz | 0:f8ed62dbacbc | 42 | LSH, /* Low shelf filter */ |
mhowellaz | 0:f8ed62dbacbc | 43 | HSH /* High shelf filter */ |
mhowellaz | 0:f8ed62dbacbc | 44 | } BIQUAD_FILTER_TYPE; |
mhowellaz | 0:f8ed62dbacbc | 45 | |
mhowellaz | 0:f8ed62dbacbc | 46 | template <typename T> |
mhowellaz | 0:f8ed62dbacbc | 47 | class Biquad { |
mhowellaz | 0:f8ed62dbacbc | 48 | |
mhowellaz | 0:f8ed62dbacbc | 49 | public: |
mhowellaz | 0:f8ed62dbacbc | 50 | Biquad(BIQUAD_FILTER_TYPE type, |
mhowellaz | 3:abd20f34fd68 | 51 | T dbGain, /* peak gain of filter */ |
mhowellaz | 0:f8ed62dbacbc | 52 | T freq, /* center frequency */ |
mhowellaz | 0:f8ed62dbacbc | 53 | T srate, /* sampling rate */ |
mhowellaz | 3:abd20f34fd68 | 54 | T Q, /* Q */ |
mhowellaz | 3:abd20f34fd68 | 55 | BIQUAD_FORMULATION_TYPE form = DIRECT_FORM_I); |
mhowellaz | 0:f8ed62dbacbc | 56 | |
mhowellaz | 3:abd20f34fd68 | 57 | void Reset(BIQUAD_FILTER_TYPE type, T dbGain, T freq, T srate, T Q, bool clearHistory = false, BIQUAD_FORMULATION_TYPE form = DIRECT_FORM_I); |
mhowellaz | 3:abd20f34fd68 | 58 | |
mhowellaz | 3:abd20f34fd68 | 59 | void CalcCoefficientsDirectI(BIQUAD_FILTER_TYPE type, T dbGain, T freq, T srate, T Q); |
mhowellaz | 3:abd20f34fd68 | 60 | |
mhowellaz | 3:abd20f34fd68 | 61 | void CalcCoefficientsTransposedDirectII(BIQUAD_FILTER_TYPE type, T dbGain, T freq, T srate, T Q); |
mhowellaz | 0:f8ed62dbacbc | 62 | |
mhowellaz | 0:f8ed62dbacbc | 63 | T Calculate(T sample); |
mhowellaz | 0:f8ed62dbacbc | 64 | |
mhowellaz | 3:abd20f34fd68 | 65 | BIQUAD_FORMULATION_TYPE form; |
mhowellaz | 3:abd20f34fd68 | 66 | |
mhowellaz | 3:abd20f34fd68 | 67 | // used for DIRECT_FORM_1 |
mhowellaz | 3:abd20f34fd68 | 68 | T a0_I, a1_I, a2_I, a3_I, a4_I; |
mhowellaz | 0:f8ed62dbacbc | 69 | T x1, x2, y1, y2; |
mhowellaz | 3:abd20f34fd68 | 70 | |
mhowellaz | 3:abd20f34fd68 | 71 | // used for TRANSPOSED_DIRECT_FORM_II |
mhowellaz | 3:abd20f34fd68 | 72 | T a0_II, a1_II, a2_II, b1_II, b2_II; |
mhowellaz | 3:abd20f34fd68 | 73 | T z1, z2; |
mhowellaz | 0:f8ed62dbacbc | 74 | |
mhowellaz | 0:f8ed62dbacbc | 75 | }; |
mhowellaz | 2:9a9b643fcc66 | 76 | |
mhowellaz | 2:9a9b643fcc66 | 77 | #endif |
mhowellaz | 2:9a9b643fcc66 | 78 |