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@0:f8ed62dbacbc, 2012-02-14 (annotated)
- Committer:
- mhowellaz
- Date:
- Tue Feb 14 19:12:33 2012 +0000
- Revision:
- 0:f8ed62dbacbc
- Child:
- 1:9c0546cd518e
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mhowellaz | 0:f8ed62dbacbc | 1 | /* |
mhowellaz | 0:f8ed62dbacbc | 2 | * biquad.cpp |
mhowellaz | 0:f8ed62dbacbc | 3 | * |
mhowellaz | 0:f8ed62dbacbc | 4 | * Original source material from Robert Bristow Johnson at |
mhowellaz | 0:f8ed62dbacbc | 5 | * http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt |
mhowellaz | 0:f8ed62dbacbc | 6 | * |
mhowellaz | 0:f8ed62dbacbc | 7 | * Derived from public domain C implementation by Tom St. Denis |
mhowellaz | 0:f8ed62dbacbc | 8 | * |
mhowellaz | 0:f8ed62dbacbc | 9 | * Thanks gents! |
mhowellaz | 0:f8ed62dbacbc | 10 | * |
mhowellaz | 0:f8ed62dbacbc | 11 | * C++-ification by Mark J. Howell |
mhowellaz | 0:f8ed62dbacbc | 12 | * |
mhowellaz | 0:f8ed62dbacbc | 13 | */ |
mhowellaz | 0:f8ed62dbacbc | 14 | |
mhowellaz | 0:f8ed62dbacbc | 15 | #include <math.h> |
mhowellaz | 0:f8ed62dbacbc | 16 | |
mhowellaz | 0:f8ed62dbacbc | 17 | #ifndef M_LN2 |
mhowellaz | 0:f8ed62dbacbc | 18 | #define M_LN2 0.69314718055994530942 |
mhowellaz | 0:f8ed62dbacbc | 19 | #endif |
mhowellaz | 0:f8ed62dbacbc | 20 | |
mhowellaz | 0:f8ed62dbacbc | 21 | #ifndef M_PI |
mhowellaz | 0:f8ed62dbacbc | 22 | #define M_PI 3.14159265358979323846 |
mhowellaz | 0:f8ed62dbacbc | 23 | #endif |
mhowellaz | 0:f8ed62dbacbc | 24 | |
mhowellaz | 0:f8ed62dbacbc | 25 | /* filter types */ |
mhowellaz | 0:f8ed62dbacbc | 26 | typedef enum { |
mhowellaz | 0:f8ed62dbacbc | 27 | LPF, /* low pass filter */ |
mhowellaz | 0:f8ed62dbacbc | 28 | HPF, /* High pass filter */ |
mhowellaz | 0:f8ed62dbacbc | 29 | BPF, /* band pass filter */ |
mhowellaz | 0:f8ed62dbacbc | 30 | NOTCH, /* Notch Filter */ |
mhowellaz | 0:f8ed62dbacbc | 31 | PEQ, /* Peaking band EQ filter */ |
mhowellaz | 0:f8ed62dbacbc | 32 | LSH, /* Low shelf filter */ |
mhowellaz | 0:f8ed62dbacbc | 33 | HSH /* High shelf filter */ |
mhowellaz | 0:f8ed62dbacbc | 34 | } BIQUAD_FILTER_TYPE; |
mhowellaz | 0:f8ed62dbacbc | 35 | |
mhowellaz | 0:f8ed62dbacbc | 36 | template <typename T> |
mhowellaz | 0:f8ed62dbacbc | 37 | class Biquad { |
mhowellaz | 0:f8ed62dbacbc | 38 | |
mhowellaz | 0:f8ed62dbacbc | 39 | public: |
mhowellaz | 0:f8ed62dbacbc | 40 | Biquad(BIQUAD_FILTER_TYPE type, |
mhowellaz | 0:f8ed62dbacbc | 41 | T dbGain, /* gain of filter */ |
mhowellaz | 0:f8ed62dbacbc | 42 | T freq, /* center frequency */ |
mhowellaz | 0:f8ed62dbacbc | 43 | T srate, /* sampling rate */ |
mhowellaz | 0:f8ed62dbacbc | 44 | T bandwidth); /* bandwidth in octaves */ |
mhowellaz | 0:f8ed62dbacbc | 45 | |
mhowellaz | 0:f8ed62dbacbc | 46 | void Reset(BIQUAD_FILTER_TYPE type, T dbGain, T freq, T srate, T bandwidth); |
mhowellaz | 0:f8ed62dbacbc | 47 | |
mhowellaz | 0:f8ed62dbacbc | 48 | T Calculate(T sample); |
mhowellaz | 0:f8ed62dbacbc | 49 | |
mhowellaz | 0:f8ed62dbacbc | 50 | T a0, a1, a2, a3, a4; |
mhowellaz | 0:f8ed62dbacbc | 51 | T x1, x2, y1, y2; |
mhowellaz | 0:f8ed62dbacbc | 52 | |
mhowellaz | 0:f8ed62dbacbc | 53 | }; |