iir1
Loading...
Searching...
No Matches
PoleFilter.h
1
35
36#pragma once
37
38#include "Common.h"
39#include "MathSupplement.h"
40#include "Cascade.h"
41
42namespace Iir {
43
44/***
45 * Base for filters designed via algorithmic placement of poles and zeros.
46 *
47 * Typically, the filter is first designed as a half-band low pass or
48 * low shelf analog filter (s-plane). Then, using a transformation such
49 * as the ones from Constantinides, the poles and zeros of the analog filter
50 * are calculated in the z-plane.
51 *
52 ***/
53
57 class IIR_EXPORT PoleFilterBase2 : public Cascade
58 {
59 public:
60 // This gets the poles/zeros directly from the digital
61 // prototype. It is used to double check the correctness
62 // of the recovery of pole/zeros from biquad coefficients.
63 //
64 // It can also be used to accelerate the interpolation
65 // of pole/zeros for parameter modulation, since a pole
66 // filter already has them calculated
67
68 PoleFilterBase2() = default;
69
70 std::vector<PoleZeroPair> getPoleZeros () const
71 {
72 std::vector<PoleZeroPair> vpz;
73 const int pairs = (m_digitalProto.getNumPoles () + 1) / 2;
74 for (int i = 0; i < pairs; ++i)
75 vpz.push_back (m_digitalProto[i]);
76 return vpz;
77 }
78
79 protected:
80 LayoutBase m_digitalProto = {};
81 };
82
83
88 template <class AnalogPrototype>
89 class PoleFilterBase : public PoleFilterBase2
90 {
91 protected:
92 void setPrototypeStorage (const LayoutBase& analogStorage,
93 const LayoutBase& digitalStorage)
94 {
95 m_analogProto.setStorage (analogStorage);
96 m_digitalProto = digitalStorage;
97 }
98
99 protected:
100 AnalogPrototype m_analogProto = {};
101 };
102
103//------------------------------------------------------------------------------
104
108 template <class BaseClass,
109 class StateType,
110 int MaxAnalogPoles,
111 int MaxDigitalPoles = MaxAnalogPoles>
112 struct PoleFilter : BaseClass
113 , CascadeStages <(MaxDigitalPoles + 1) / 2 , StateType>
114 {
115 public:
116 PoleFilter ()
117 {
118 // This glues together the factored base classes
119 // with the templatized storage classes.
120 BaseClass::setCascadeStorage (this->getCascadeStorage());
121 BaseClass::setPrototypeStorage (m_analogStorage, m_digitalStorage);
122 CascadeStages<(MaxDigitalPoles + 1) / 2 , StateType>::reset();
123 }
124
125 PoleFilter(const PoleFilter&) = default;
126
127 PoleFilter& operator=(const PoleFilter&)
128 {
129 // Reset the filter state when copied for now
130 CascadeStages<(MaxDigitalPoles + 1) / 2 , StateType>::reset();
131 return *this;
132 }
133
134 private:
135 Layout <MaxAnalogPoles> m_analogStorage = {};
136 Layout <MaxDigitalPoles> m_digitalStorage = {};
137 };
138
139//------------------------------------------------------------------------------
140
151
155 class IIR_EXPORT LowPassTransform
156 {
157 public:
158 LowPassTransform (double fc,
159 LayoutBase& digital,
160 LayoutBase const& analog);
161
162 private:
163 complex_t transform (complex_t c);
164
165 double f = 0.0;
166 };
167
168//------------------------------------------------------------------------------
169
173 class IIR_EXPORT HighPassTransform
174 {
175 public:
176 HighPassTransform (double fc,
177 LayoutBase& digital,
178 LayoutBase const& analog);
179
180 private:
181 complex_t transform (complex_t c);
182
183 double f = 0.0;
184 };
185
186//------------------------------------------------------------------------------
187
191 class IIR_EXPORT BandPassTransform
192 {
193
194 public:
195 BandPassTransform (double fc,
196 double fw,
197 LayoutBase& digital,
198 LayoutBase const& analog);
199
200 private:
201 ComplexPair transform (complex_t c);
202
203 double wc = 0.0;
204 double wc2 = 0.0;
205 double a = 0.0;
206 double b = 0.0;
207 double a2 = 0.0;
208 double b2 = 0.0;
209 double ab = 0.0;
210 double ab_2 = 0.0;
211 };
212
213//------------------------------------------------------------------------------
214
218 class IIR_EXPORT BandStopTransform
219 {
220 public:
221 BandStopTransform (double fc,
222 double fw,
223 LayoutBase& digital,
224 LayoutBase const& analog);
225
226 private:
227 ComplexPair transform (complex_t c);
228
229 double wc = 0.0;
230 double wc2 = 0.0;
231 double a = 0.0;
232 double b = 0.0;
233 double a2 = 0.0;
234 double b2 = 0.0;
235 };
236
237}
const Cascade::Storage getCascadeStorage()
Definition Cascade.h:180
Definition Layout.h:62
Definition PoleFilter.h:90
Definition Biquad.cpp:40
Definition Types.h:47