Open Ephys GUI
 All Classes Functions Variables
Layout.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_LAYOUT_H
37 #define DSPFILTERS_LAYOUT_H
38 
39 #include "Common.h"
40 #include "MathSupplement.h"
41 
42 namespace Dsp {
43 
44 //
45 // Describes a filter as a collection of poles and zeros along with
46 // normalization information to achieve a specified gain at a specified
47 // frequency. The poles and zeros may lie either in the s or the z plane.
48 //
49 
50 // Base uses pointers to reduce template instantiations
52 {
53 public:
54  LayoutBase ()
55  : m_numPoles (0)
56  , m_maxPoles (0)
57  {
58  }
59 
60  LayoutBase (int maxPoles, PoleZeroPair* pairs)
61  : m_numPoles (0)
62  , m_maxPoles (maxPoles)
63  , m_pair (pairs)
64  {
65  }
66 
67  void setStorage (const LayoutBase& other)
68  {
69  m_numPoles = 0;
70  m_maxPoles = other.m_maxPoles;
71  m_pair = other.m_pair;
72  }
73 
74  void reset ()
75  {
76  m_numPoles = 0;
77  }
78 
79  int getNumPoles () const
80  {
81  return m_numPoles;
82  }
83 
84  int getMaxPoles () const
85  {
86  return m_maxPoles;
87  }
88 
89  void add (const complex_t& pole, const complex_t& zero)
90  {
91  assert (!(m_numPoles&1)); // single comes last
92  assert (!Dsp::is_nan (pole));
93  m_pair[m_numPoles/2] = PoleZeroPair (pole, zero);
94  ++m_numPoles;
95  }
96 
97  void addPoleZeroConjugatePairs (const complex_t pole,
98  const complex_t zero)
99  {
100  assert (!(m_numPoles&1)); // single comes last
101  assert (!Dsp::is_nan (pole));
102  m_pair[m_numPoles/2] = PoleZeroPair (
103  pole, zero, std::conj (pole), std::conj (zero));
104  m_numPoles += 2;
105  }
106 
107  void add (const ComplexPair& poles, const ComplexPair& zeros)
108  {
109  assert (!(m_numPoles&1)); // single comes last
110  assert (poles.isMatchedPair ());
111  assert (zeros.isMatchedPair ());
112  m_pair[m_numPoles/2] = PoleZeroPair (poles.first, zeros.first,
113  poles.second, zeros.second);
114  m_numPoles += 2;
115  }
116 
117  const PoleZeroPair& getPair (int pairIndex) const
118  {
119  assert (pairIndex >= 0 && pairIndex < (m_numPoles+1)/2);
120  return m_pair[pairIndex];
121  }
122 
123  const PoleZeroPair& operator[] (int pairIndex) const
124  {
125  return getPair (pairIndex);
126  }
127 
128  double getNormalW () const
129  {
130  return m_normalW;
131  }
132 
133  double getNormalGain () const
134  {
135  return m_normalGain;
136  }
137 
138  void setNormal (double w, double g)
139  {
140  m_normalW = w;
141  m_normalGain = g;
142  }
143 
144 private:
145  int m_numPoles;
146  int m_maxPoles;
147  PoleZeroPair* m_pair;
148  double m_normalW;
149  double m_normalGain;
150 };
151 
152 //------------------------------------------------------------------------------
153 
154 // Storage for Layout
155 template <int MaxPoles>
156 class Layout
157 {
158 public:
159  operator LayoutBase ()
160  {
161  return LayoutBase (MaxPoles, m_pairs);
162  }
163 
164 private:
165  PoleZeroPair m_pairs[(MaxPoles+1)/2];
166 };
167 
168 }
169 
170 #endif