Open Ephys GUI
 All Classes Functions Variables
Butterworth.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_BUTTERWORTH_H
37 #define DSPFILTERS_BUTTERWORTH_H
38 
39 #include "Common.h"
40 #include "Cascade.h"
41 #include "Design.h"
42 #include "Filter.h"
43 #include "PoleFilter.h"
44 
45 namespace Dsp {
46 
47 /*
48  * Filters with Butterworth response characteristics
49  *
50  */
51 
52 namespace Butterworth {
53 
54 // Half-band analog prototypes (s-plane)
55 
56 class AnalogLowPass : public LayoutBase
57 {
58 public:
59  AnalogLowPass ();
60 
61  void design (const int numPoles);
62 
63 private:
64  int m_numPoles;
65 };
66 
67 //------------------------------------------------------------------------------
68 
69 class AnalogLowShelf : public LayoutBase
70 {
71 public:
72  AnalogLowShelf ();
73 
74  void design (int numPoles, double gainDb);
75 
76 private:
77  int m_numPoles;
78  double m_gainDb;
79 };
80 
81 //------------------------------------------------------------------------------
82 
83 // Factored implementations to reduce template instantiations
84 
85 struct LowPassBase : PoleFilterBase <AnalogLowPass>
86 {
87  void setup (int order,
88  double sampleRate,
89  double cutoffFrequency);
90 };
91 
92 struct HighPassBase : PoleFilterBase <AnalogLowPass>
93 {
94  void setup (int order,
95  double sampleRate,
96  double cutoffFrequency);
97 };
98 
99 struct BandPassBase : PoleFilterBase <AnalogLowPass>
100 {
101  void setup (int order,
102  double sampleRate,
103  double centerFrequency,
104  double widthFrequency);
105 };
106 
107 struct BandStopBase : PoleFilterBase <AnalogLowPass>
108 {
109  void setup (int order,
110  double sampleRate,
111  double centerFrequency,
112  double widthFrequency);
113 };
114 
115 struct LowShelfBase : PoleFilterBase <AnalogLowShelf>
116 {
117  void setup (int order,
118  double sampleRate,
119  double cutoffFrequency,
120  double gainDb);
121 };
122 
123 struct HighShelfBase : PoleFilterBase <AnalogLowShelf>
124 {
125  void setup (int order,
126  double sampleRate,
127  double cutoffFrequency,
128  double gainDb);
129 };
130 
131 struct BandShelfBase : PoleFilterBase <AnalogLowShelf>
132 {
133  void setup (int order,
134  double sampleRate,
135  double centerFrequency,
136  double widthFrequency,
137  double gainDb);
138 };
139 
140 //------------------------------------------------------------------------------
141 
142 //
143 // Raw filters
144 //
145 
146 template <int MaxOrder>
147 struct LowPass : PoleFilter <LowPassBase, MaxOrder>
148 {
149 };
150 
151 template <int MaxOrder>
152 struct HighPass : PoleFilter <HighPassBase, MaxOrder>
153 {
154 };
155 
156 template <int MaxOrder>
157 struct BandPass : PoleFilter <BandPassBase, MaxOrder, MaxOrder*2>
158 {
159 };
160 
161 template <int MaxOrder>
162 struct BandStop : PoleFilter <BandStopBase, MaxOrder, MaxOrder*2>
163 {
164 };
165 
166 template <int MaxOrder>
167 struct LowShelf : PoleFilter <LowShelfBase, MaxOrder>
168 {
169 };
170 
171 template <int MaxOrder>
172 struct HighShelf : PoleFilter <HighShelfBase, MaxOrder>
173 {
174 };
175 
176 template <int MaxOrder>
177 struct BandShelf : PoleFilter <BandShelfBase, MaxOrder, MaxOrder*2>
178 {
179 };
180 
181 //------------------------------------------------------------------------------
182 
183 //
184 // Gui-friendly Design layer
185 //
186 
187 namespace Design {
188 
190 {
191  enum
192  {
193  NumParams = 3
194  };
195 
196  static int getNumParams ()
197  {
198  return 3;
199  }
200 
201  static const ParamInfo getParamInfo_2 ()
202  {
203  return ParamInfo::defaultCutoffFrequencyParam ();
204  }
205 };
206 
207 template <class FilterClass>
208 struct TypeI : TypeIBase, FilterClass
209 {
210  void setParams (const Params& params)
211  {
212  FilterClass::setup (int(params[1]), params[0], params[2]);
213  }
214 };
215 
217 {
218  enum
219  {
220  NumParams = 4
221  };
222 
223  static int getNumParams ()
224  {
225  return 4;
226  }
227 
228  static const ParamInfo getParamInfo_2 ()
229  {
230  return ParamInfo::defaultCenterFrequencyParam ();
231  }
232 
233  static const ParamInfo getParamInfo_3 ()
234  {
235  return ParamInfo::defaultBandwidthHzParam ();
236  }
237 };
238 
239 template <class FilterClass>
240 struct TypeII : TypeIIBase, FilterClass
241 {
242  void setParams (const Params& params)
243  {
244  FilterClass::setup (int(params[1]), params[0], params[2], params[3]);
245  }
246 };
247 
249 {
250  enum
251  {
252  NumParams = 4
253  };
254 
255  static int getNumParams ()
256  {
257  return 4;
258  }
259 
260  static const ParamInfo getParamInfo_2 ()
261  {
262  return ParamInfo::defaultCutoffFrequencyParam ();
263  }
264 
265  static const ParamInfo getParamInfo_3 ()
266  {
267  return ParamInfo::defaultGainParam ();
268  }
269 };
270 
271 template <class FilterClass>
272 struct TypeIII : TypeIIIBase, FilterClass
273 {
274  void setParams (const Params& params)
275  {
276  FilterClass::setup (int(params[1]),
277  params[0],
278  params[2],
279  params[3]);
280  }
281 };
282 
284 {
285  enum
286  {
287  NumParams = 5
288  };
289 
290  static int getNumParams ()
291  {
292  return 5;
293  }
294 
295  static const ParamInfo getParamInfo_2 ()
296  {
297  return ParamInfo::defaultCenterFrequencyParam ();
298  }
299 
300  static const ParamInfo getParamInfo_3 ()
301  {
302  return ParamInfo::defaultBandwidthHzParam ();
303  }
304 
305  static const ParamInfo getParamInfo_4 ()
306  {
307  return ParamInfo::defaultGainParam ();
308  }
309 };
310 
311 template <class FilterClass>
312 struct TypeIV : TypeIVBase, FilterClass
313 {
314  void setParams (const Params& params)
315  {
316  FilterClass::setup (int(params[1]), params[0], params[2], params[3], params[4]);
317  }
318 };
319 
320 // Factored kind and name
321 
323 {
324  static Kind getKind () { return kindLowPass; }
325  static const char* getName() { return "Butterworth Low Pass"; }
326 };
327 
329 {
330  static Kind getKind () { return kindHighPass; }
331  static const char* getName() { return "Butterworth High Pass"; }
332 };
333 
335 {
336  static Kind getKind () { return kindHighPass; }
337  static const char* getName() { return "Butterworth Band Pass"; }
338 };
339 
341 {
342  static Kind getKind () { return kindHighPass; }
343  static const char* getName() { return "Butterworth Band Stop"; }
344 };
345 
347 {
348  static Kind getKind () { return kindLowShelf; }
349  static const char* getName() { return "Butterworth Low Shelf"; }
350 };
351 
353 {
354  static Kind getKind () { return kindHighShelf; }
355  static const char* getName() { return "Butterworth High Shelf"; }
356 };
357 
359 {
360  static Kind getKind () { return kindBandShelf; }
361  static const char* getName() { return "Butterworth Band Shelf"; }
362 };
363 
364 // This glues on the Order parameter
365 template <int MaxOrder,
366  template <class> class TypeClass,
367  template <int> class FilterClass>
368 struct OrderBase : TypeClass <FilterClass <MaxOrder> >
369 {
370  const ParamInfo getParamInfo_1 () const
371  {
372  return ParamInfo (idOrder, "Order", "Order",
373  1, MaxOrder, 2,
374  &ParamInfo::Int_toControlValue,
375  &ParamInfo::Int_toNativeValue,
376  &ParamInfo::Int_toString);
377 
378  }
379 };
380 
381 //------------------------------------------------------------------------------
382 
383 //
384 // Design filters
385 //
386 
387 template <int MaxOrder>
388 struct LowPass : OrderBase <MaxOrder, TypeI, Butterworth::LowPass>,
390 {
391 };
392 
393 template <int MaxOrder>
394 struct HighPass : OrderBase <MaxOrder, TypeI, Butterworth::HighPass>,
396 {
397 };
398 
399 template <int MaxOrder>
400 struct BandPass : OrderBase <MaxOrder, TypeII, Butterworth::BandPass>,
402 {
403 };
404 
405 template <int MaxOrder>
406 struct BandStop : OrderBase <MaxOrder, TypeII, Butterworth::BandStop>,
408 {
409 };
410 
411 template <int MaxOrder>
412 struct LowShelf : OrderBase <MaxOrder, TypeIII, Butterworth::LowShelf>,
414 {
415 };
416 
417 template <int MaxOrder>
418 struct HighShelf : OrderBase <MaxOrder, TypeIII, Butterworth::HighShelf>,
420 {
421 };
422 
423 template <int MaxOrder>
424 struct BandShelf : OrderBase <MaxOrder, TypeIV, Butterworth::BandShelf>,
426 {
427 };
428 
429 }
430 
431 }
432 
433 }
434 
435 #endif
436