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.

Dependents:   EMG_Wiesje

Committer:
mhowellaz
Date:
Wed Feb 15 18:12:50 2012 +0000
Revision:
2:9a9b643fcc66
Parent:
1:9c0546cd518e
Child:
3:abd20f34fd68

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mhowellaz 0:f8ed62dbacbc 1 /*
mhowellaz 1:9c0546cd518e 2 * biquad.h
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 2:9a9b643fcc66 15 #ifndef __BIQUAD_H_
mhowellaz 2:9a9b643fcc66 16 #define __BIQUAD_H_
mhowellaz 2:9a9b643fcc66 17
mhowellaz 0:f8ed62dbacbc 18 #include <math.h>
mhowellaz 0:f8ed62dbacbc 19
mhowellaz 0:f8ed62dbacbc 20 #ifndef M_LN2
mhowellaz 0:f8ed62dbacbc 21 #define M_LN2 0.69314718055994530942
mhowellaz 0:f8ed62dbacbc 22 #endif
mhowellaz 0:f8ed62dbacbc 23
mhowellaz 0:f8ed62dbacbc 24 #ifndef M_PI
mhowellaz 0:f8ed62dbacbc 25 #define M_PI 3.14159265358979323846
mhowellaz 0:f8ed62dbacbc 26 #endif
mhowellaz 0:f8ed62dbacbc 27
mhowellaz 0:f8ed62dbacbc 28 /* filter types */
mhowellaz 0:f8ed62dbacbc 29 typedef enum {
mhowellaz 0:f8ed62dbacbc 30 LPF, /* low pass filter */
mhowellaz 0:f8ed62dbacbc 31 HPF, /* High pass filter */
mhowellaz 0:f8ed62dbacbc 32 BPF, /* band pass filter */
mhowellaz 0:f8ed62dbacbc 33 NOTCH, /* Notch Filter */
mhowellaz 0:f8ed62dbacbc 34 PEQ, /* Peaking band EQ filter */
mhowellaz 0:f8ed62dbacbc 35 LSH, /* Low shelf filter */
mhowellaz 0:f8ed62dbacbc 36 HSH /* High shelf filter */
mhowellaz 0:f8ed62dbacbc 37 } BIQUAD_FILTER_TYPE;
mhowellaz 0:f8ed62dbacbc 38
mhowellaz 0:f8ed62dbacbc 39 template <typename T>
mhowellaz 0:f8ed62dbacbc 40 class Biquad {
mhowellaz 0:f8ed62dbacbc 41
mhowellaz 0:f8ed62dbacbc 42 public:
mhowellaz 0:f8ed62dbacbc 43 Biquad(BIQUAD_FILTER_TYPE type,
mhowellaz 0:f8ed62dbacbc 44 T dbGain, /* gain of filter */
mhowellaz 0:f8ed62dbacbc 45 T freq, /* center frequency */
mhowellaz 0:f8ed62dbacbc 46 T srate, /* sampling rate */
mhowellaz 0:f8ed62dbacbc 47 T bandwidth); /* bandwidth in octaves */
mhowellaz 0:f8ed62dbacbc 48
mhowellaz 2:9a9b643fcc66 49 void Reset(BIQUAD_FILTER_TYPE type, T dbGain, T freq, T srate, T bandwidth, bool clearHistory = false);
mhowellaz 0:f8ed62dbacbc 50
mhowellaz 0:f8ed62dbacbc 51 T Calculate(T sample);
mhowellaz 0:f8ed62dbacbc 52
mhowellaz 0:f8ed62dbacbc 53 T a0, a1, a2, a3, a4;
mhowellaz 0:f8ed62dbacbc 54 T x1, x2, y1, y2;
mhowellaz 0:f8ed62dbacbc 55
mhowellaz 0:f8ed62dbacbc 56 };
mhowellaz 2:9a9b643fcc66 57
mhowellaz 2:9a9b643fcc66 58 #endif
mhowellaz 2:9a9b643fcc66 59