Bela
Real-time, ultra-low-latency audio and sensor processing system for BeagleBone Black
 All Classes Files Functions Variables Typedefs Macros Groups
Biquad.h
1 //
2 // Biquad.h
3 //
4 // Created by Nigel Redmon on 11/24/12
5 // EarLevel Engineering: earlevel.com
6 // Copyright 2012 Nigel Redmon
7 //
8 // For a complete explanation of the Biquad code:
9 // http://www.earlevel.com/main/2012/11/25/biquad-c-source-code/
10 //
11 // License:
12 //
13 // This source code is provided as is, without warranty.
14 // You may copy and distribute verbatim copies of this document.
15 // You may modify and use this source code to create binary code
16 // for your own purposes, free or commercial.
17 //
18 
19 #ifndef Biquad_h
20 #define Biquad_h
21 
22 
23 class Biquad {
24  public:
25  Biquad();
26  Biquad(double Fc, float Fs, int type, double Q = 0.707, double peakGainDB = 0.0);
27  ~Biquad();
28  void setType(int type);
29  void setQ(double Q);
30  void setFc(double Fc);
31  void setPeakGain(double peakGainDB);
32  int setup(double Fc, float Fs, int type, double Q = 0.707, double peakGainDB = 0.0);
33  float process(float in);
34 
35  double getQ();
36  double getFc();
37  double getPeakGain();
38 
39  double getStartingQ();
40  double getStartingFc();
41  double getStartingPeakGain();
42 
43  enum filter_type
44  {
45  lowpass = 0,
46  highpass,
47  bandpass,
48  notch,
49  peak,
50  lowshelf,
51  highshelf
52  };
53 
54  protected:
55  void calcBiquad(void);
56 
57  int type;
58  double a0, a1, a2, b1, b2;
59  double Fc, Q, peakGain;
60  float Fs;
61  double startFc, startQ, startPeakGain;
62  double z1, z2;
63 };
64 
65 inline double Biquad::getQ()
66 {
67  return Q;
68 }
69 
70 inline double Biquad::getFc()
71 {
72  return Fc;
73 }
74 
75 inline double Biquad::getPeakGain()
76 {
77  return peakGain;
78 }
79 
80 inline double Biquad::getStartingQ()
81 {
82  return startQ;
83 }
84 
85 inline double Biquad::getStartingFc()
86 {
87  return startFc;
88 }
89 
90 inline double Biquad::getStartingPeakGain()
91 {
92  return startPeakGain;
93 }
94 
95 inline float Biquad::process(float in) {
96  double out = in * a0 + z1;
97  z1 = in * a1 + z2 - b1 * out;
98  z2 = in * a2 - b2 * out;
99  return out;
100 }
101 
102 #endif // Biquad_h
Definition: Biquad.h:32