Open Ephys GUI
 All Classes Functions Variables
Elliptic.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_ELLIPTIC_H
37 #define DSPFILTERS_ELLIPTIC_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 Elliptic response characteristics
49  *
50  */
51 
52 namespace Elliptic {
53 
54 // Solves for Jacobi elliptics
55 class Solver
56 {
57 public:
58  static double ellipticK (double k);
59 };
60 
61 // Half-band analog prototype (s-plane)
62 
63 class AnalogLowPass : public LayoutBase
64 {
65 public:
66  AnalogLowPass ();
67 
68  void design (const int numPoles,
69  double rippleDb,
70  double rolloff);
71 
72 private:
73  void prodpoly (int sn);
74  void calcfz2 (int i);
75  void calcfz ();
76  void calcqz ();
77  double findfact (int t);
78  double calcsn (double u);
79 
80 #if 0
81  template<int n>
82  struct CalcArray
83  {
84  double& operator[](size_t index)
85  {
86  assert( index<n );
87  return m_a[index];
88  }
89  private:
90  double m_a[n];
91  };
92 #else
93 #endif
94 
95  double m_p0;
96  double m_q;
97  double m_K;
98  double m_Kprime;
99  double m_e;
100  int m_nin;
101  int m_m;
102  int m_n2;
103  int m_em;
104  double m_zeros[100];
105  double m_c1[100];
106  double m_b1[100];
107  double m_a1[100];
108  double m_d1[100];
109  double m_q1[100];
110  double m_z1[100];
111  double m_f1[100];
112  double m_s1[100];
113  double m_p [100];
114  double m_zw1[100];
115  double m_zf1[100];
116  double m_zq1[100];
117  double m_rootR[100];
118  double m_rootI[100];
119 
120  int m_numPoles;
121  double m_rippleDb;
122  double m_rolloff;
123 };
124 
125 //------------------------------------------------------------------------------
126 
127 // Factored implementations to reduce template instantiations
128 
129 struct LowPassBase : PoleFilterBase <AnalogLowPass>
130 {
131  void setup (int order,
132  double sampleRate,
133  double cutoffFrequency,
134  double rippleDb,
135  double rolloff);
136 };
137 
138 struct HighPassBase : PoleFilterBase <AnalogLowPass>
139 {
140  void setup (int order,
141  double sampleRate,
142  double cutoffFrequency,
143  double rippleDb,
144  double rolloff);
145 };
146 
147 struct BandPassBase : PoleFilterBase <AnalogLowPass>
148 {
149  void setup (int order,
150  double sampleRate,
151  double centerFrequency,
152  double widthFrequency,
153  double rippleDb,
154  double rolloff);
155 };
156 
157 struct BandStopBase : PoleFilterBase <AnalogLowPass>
158 {
159  void setup (int order,
160  double sampleRate,
161  double centerFrequency,
162  double widthFrequency,
163  double rippleDb,
164  double rolloff);
165 };
166 
167 //------------------------------------------------------------------------------
168 
169 //
170 // Raw filters
171 //
172 
173 template <int MaxOrder>
174 struct LowPass : PoleFilter <LowPassBase, MaxOrder>
175 {
176 };
177 
178 template <int MaxOrder>
179 struct HighPass : PoleFilter <HighPassBase, MaxOrder>
180 {
181 };
182 
183 template <int MaxOrder>
184 struct BandPass : PoleFilter <BandPassBase, MaxOrder, MaxOrder*2>
185 {
186 };
187 
188 template <int MaxOrder>
189 struct BandStop : PoleFilter <BandStopBase, MaxOrder, MaxOrder*2>
190 {
191 };
192 
193 //------------------------------------------------------------------------------
194 
195 //
196 // Gui-friendly Design layer
197 //
198 
199 namespace Design {
200 
202 {
203  enum
204  {
205  NumParams = 5
206  };
207 
208  static int getNumParams ()
209  {
210  return 5;
211  }
212 
213  static const ParamInfo getParamInfo_2 ()
214  {
215  return ParamInfo::defaultCutoffFrequencyParam ();
216  }
217 
218  static const ParamInfo getParamInfo_3 ()
219  {
220  return ParamInfo::defaultRippleDbParam ();
221  }
222 
223  static const ParamInfo getParamInfo_4 ()
224  {
225  return ParamInfo::defaultRolloffParam ();
226  }
227 };
228 
229 template <class FilterClass>
230 struct TypeI : TypeIBase, FilterClass
231 {
232  void setParams (const Params& params)
233  {
234  FilterClass::setup (int(params[1]), params[0], params[2], params[3], params[4]);
235  }
236 };
237 
239 {
240  enum
241  {
242  NumParams = 6
243  };
244 
245  static int getNumParams ()
246  {
247  return 6;
248  }
249 
250  static const ParamInfo getParamInfo_2 ()
251  {
252  return ParamInfo::defaultCenterFrequencyParam ();
253  }
254 
255  static const ParamInfo getParamInfo_3 ()
256  {
257  return ParamInfo::defaultBandwidthHzParam ();
258  }
259 
260  static const ParamInfo getParamInfo_4 ()
261  {
262  return ParamInfo::defaultRippleDbParam ();
263  }
264 
265  static const ParamInfo getParamInfo_5 ()
266  {
267  return ParamInfo::defaultRolloffParam ();
268  }
269 };
270 
271 template <class FilterClass>
272 struct TypeII : TypeIIBase, FilterClass
273 {
274  void setParams (const Params& params)
275  {
276  FilterClass::setup (int(params[1]), params[0], params[2], params[3], params[4], params[5]);
277  }
278 };
279 
280 // Factored kind and name
281 
283 {
284  static Kind getKind () { return kindLowPass; }
285  static const char* getName() { return "Elliptic Low Pass"; }
286 };
287 
289 {
290  static Kind getKind () { return kindHighPass; }
291  static const char* getName() { return "Elliptic High Pass"; }
292 };
293 
295 {
296  static Kind getKind () { return kindHighPass; }
297  static const char* getName() { return "Elliptic Band Pass"; }
298 };
299 
301 {
302  static Kind getKind () { return kindHighPass; }
303  static const char* getName() { return "Elliptic Band Stop"; }
304 };
305 
306 // This glues on the Order parameter
307 template <int MaxOrder,
308  template <class> class TypeClass,
309  template <int> class FilterClass>
310 struct OrderBase : TypeClass <FilterClass <MaxOrder> >
311 {
312  const ParamInfo getParamInfo_1 () const
313  {
314  return ParamInfo (idOrder, "Order", "Order",
315  1, MaxOrder, 2,
316  &ParamInfo::Int_toControlValue,
317  &ParamInfo::Int_toNativeValue,
318  &ParamInfo::Int_toString);
319 
320  }
321 };
322 //------------------------------------------------------------------------------
323 
324 //
325 // Design filters
326 //
327 
328 template <int MaxOrder>
329 struct LowPass : OrderBase <MaxOrder, TypeI, Elliptic::LowPass>,
331 {
332 };
333 
334 template <int MaxOrder>
335 struct HighPass : OrderBase <MaxOrder, TypeI, Elliptic::HighPass>,
337 {
338 };
339 
340 template <int MaxOrder>
341 struct BandPass : OrderBase <MaxOrder, TypeII, Elliptic::BandPass>,
343 {
344 };
345 
346 template <int MaxOrder>
347 struct BandStop : OrderBase <MaxOrder, TypeII, Elliptic::BandStop>,
349 {
350 };
351 
352 }
353 
354 }
355 
356 }
357 
358 #endif
359