Open Ephys GUI
 All Classes Functions Variables
Biquad.h
1 /*******************************************************************************
2 
3 "A Collection of Useful C++ Classes for Digital Signal Processing"
4  By Vincent Falco
5 
6 Official project location:
7 http://code.google.com/p/dspfilterscpp/
8 
9 See Documentation.cpp for contact information, notes, and bibliography.
10 
11 --------------------------------------------------------------------------------
12 
13 License: MIT License (http://www.opensource.org/licenses/mit-license.php)
14 Copyright (c) 2009 by Vincent Falco
15 
16 Permission is hereby granted, free of charge, to any person obtaining a copy
17 of this software and associated documentation files (the "Software"), to deal
18 in the Software without restriction, including without limitation the rights
19 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20 copies of the Software, and to permit persons to whom the Software is
21 furnished to do so, subject to the following conditions:
22 
23 The above copyright notice and this permission notice shall be included in
24 all copies or substantial portions of the Software.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 THE SOFTWARE.
33 
34 *******************************************************************************/
35 
36 #ifndef DSPFILTERS_BIQUAD_H
37 #define DSPFILTERS_BIQUAD_H
38 
39 #include "Common.h"
40 #include "MathSupplement.h"
41 #include "Types.h"
42 
43 namespace Dsp {
44 
45 struct BiquadPoleState;
46 
47 /*
48  * Holds coefficients for a second order Infinite Impulse Response
49  * digital filter. This is the building block for all IIR filters.
50  *
51  */
52 
53 // Factored interface to prevent outsiders from fiddling
55 {
56 public:
57  template <class StateType>
58  struct State : StateType, private DenormalPrevention
59  {
60  template <typename Sample>
61  inline Sample process (const Sample in, const BiquadBase& b)
62  {
63  return static_cast<Sample> (StateType::process1 (in, b, ac()));
64  }
65  };
66 
67 public:
68  // Calculate filter response at the given normalized frequency.
69  complex_t response (double normalizedFrequency) const;
70 
71  std::vector<PoleZeroPair> getPoleZeros () const;
72 
73  double getA0 () const { return m_a0; }
74  double getA1 () const { return m_a1*m_a0; }
75  double getA2 () const { return m_a2*m_a0; }
76  double getB0 () const { return m_b0*m_a0; }
77  double getB1 () const { return m_b1*m_a0; }
78  double getB2 () const { return m_b2*m_a0; }
79 
80  // Process a block of samples in the given form
81  template <class StateType, typename Sample>
82  void process (int numSamples, Sample* dest, StateType& state) const
83  {
84  while (--numSamples >= 0)
85  *dest++ = state.process (*dest, *this);
86  }
87 
88 protected:
89  //
90  // These are protected so you can't mess with RBJ biquads
91  //
92 
93  void setCoefficients (double a0, double a1, double a2,
94  double b0, double b1, double b2);
95 
96  void setOnePole (complex_t pole, complex_t zero);
97 
98  void setTwoPole (complex_t pole1, complex_t zero1,
99  complex_t pole2, complex_t zero2);
100 
101  void setPoleZeroPair (const PoleZeroPair& pair)
102  {
103  if (pair.isSinglePole ())
104  setOnePole (pair.poles.first, pair.zeros.first);
105  else
106  setTwoPole (pair.poles.first, pair.zeros.first,
107  pair.poles.second, pair.zeros.second);
108  }
109 
110  void setPoleZeroForm (const BiquadPoleState& bps);
111 
112  void setIdentity ();
113 
114  void applyScale (double scale);
115 
116 public:
117  double m_a0;
118  double m_a1;
119  double m_a2;
120  double m_b1;
121  double m_b2;
122  double m_b0;
123 };
124 
125 //------------------------------------------------------------------------------
126 
127 // Expresses a biquad as a pair of pole/zeros, with gain
128 // values so that the coefficients can be reconstructed precisely.
130 {
131  BiquadPoleState () { }
132 
133  explicit BiquadPoleState (const BiquadBase& s);
134 
135  double gain;
136 };
137 
138 // More permissive interface for fooling around
139 class Biquad : public BiquadBase
140 {
141 public:
142  Biquad ();
143 
144  explicit Biquad (const BiquadPoleState& bps);
145 
146 public:
147  // Process a block of samples, interpolating from the old section's coefficients
148  // to this section's coefficients, over numSamples. This implements smooth
149  // parameter changes.
150 
151  template <class StateType, typename Sample>
152  void smoothProcess1 (int numSamples,
153  Sample* dest,
154  StateType& state,
155  Biquad sectionPrev) const
156  {
157  double t = 1. / numSamples;
158  double da1 = (m_a1 - sectionPrev.m_a1) * t;
159  double da2 = (m_a2 - sectionPrev.m_a2) * t;
160  double db0 = (m_b0 - sectionPrev.m_b0) * t;
161  double db1 = (m_b1 - sectionPrev.m_b1) * t;
162  double db2 = (m_b2 - sectionPrev.m_b2) * t;
163 
164  while (--numSamples >= 0)
165  {
166  sectionPrev.m_a1 += da1;
167  sectionPrev.m_a2 += da2;
168  sectionPrev.m_b0 += db0;
169  sectionPrev.m_b1 += db1;
170  sectionPrev.m_b2 += db2;
171 
172  *dest++ = state.process (*dest, sectionPrev);
173  }
174  }
175 
176  // Process a block of samples, interpolating from the old section's pole/zeros
177  // to this section's pole/zeros, over numSamples. The interpolation is done
178  // in the z-plane using polar coordinates.
179  template <class StateType, typename Sample>
180  void smoothProcess2 (int numSamples,
181  Sample* dest,
182  StateType& state,
183  BiquadPoleState zPrev) const
184  {
185  BiquadPoleState z (*this);
186  double t = 1. / numSamples;
187  complex_t dp0 = (z.poles.first - zPrev.poles.first) * t;
188  complex_t dp1 = (z.poles.second - zPrev.poles.second) * t;
189  complex_t dz0 = (z.zeros.first - zPrev.zeros.first) * t;
190  complex_t dz1 = (z.zeros.second - zPrev.zeros.second) * t;
191  double dg = (z.gain - zPrev.gain) * t;
192 
193  while (--numSamples >= 0)
194  {
195  zPrev.poles.first += dp0;
196  zPrev.poles.second += dp1;
197  zPrev.zeros.first += dz0;
198  zPrev.zeros.second += dz1;
199  zPrev.gain += dg;
200 
201  *dest++ = state.process (*dest, Biquad (zPrev));
202  }
203  }
204 
205 public:
206  // Export these as public
207 
208  void setOnePole (complex_t pole, complex_t zero)
209  {
210  BiquadBase::setOnePole (pole, zero);
211  }
212 
213  void setTwoPole (complex_t pole1, complex_t zero1,
214  complex_t pole2, complex_t zero2)
215  {
216  BiquadBase::setTwoPole (pole1, zero1, pole2, zero2);
217  }
218 
219  void setPoleZeroPair (const PoleZeroPair& pair)
220  {
221  BiquadBase::setPoleZeroPair (pair);
222  }
223 
224  void applyScale (double scale)
225  {
226  BiquadBase::applyScale (scale);
227  }
228 };
229 
230 }
231 
232 #endif