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