iir1: iir/Cascade.h Source File

iir1

Cascade.h
1
36 #ifndef IIR1_CASCADE_H
37 #define IIR1_CASCADE_H
38
39 #include "Common.h"
40 #include "Biquad.h"
41 #include "Layout.h"
42 #include "MathSupplement.h"
43
44 namespace Iir {
45
49 class IIR_EXPORT Cascade
50 {
51 public:
52
53 Cascade () = default;
54
59 struct IIR_EXPORT Storage
60 {
61 int maxStages = 0;
62 Biquad* stageArray = nullptr;
63 };
64
68 int getNumStages () const
69 {
70 return m_numStages;
71 }
72
76 const Biquad& operator[] (int index)
77 {
78 if ((index < 0) || (index >= m_numStages))
79 throw_invalid_argument("Index out of bounds.");
80 return m_stageArray[index];
81 }
82
87 complex_t response (double normalizedFrequency) const;
88
92 std::vector<PoleZeroPair> getPoleZeros () const;
93
94 void setCascadeStorage (const Storage& storage);
95
96 void applyScale (double scale);
97
98 void setLayout (const LayoutBase& proto);
99
100 private:
101 int m_numStages = 0;
102 int m_maxStages = 0;
103 Biquad* m_stageArray = nullptr;
104 };
105
106
107 //------------------------------------------------------------------------------
108
113 template <int MaxStages,class StateType>
114 class CascadeStages {
115
116 public:
117 CascadeStages() = default;
118
119
120 public:
124 void reset ()
125 {
126 for (auto &state: m_states)
127 state.reset();
128 }
129
130 public:
136 void setup (const double (&sosCoefficients)[MaxStages][6]) {
137 for (int i = 0; i < MaxStages; i++) {
138 m_stages[i].setCoefficients(
139 sosCoefficients[i][3],
140 sosCoefficients[i][4],
141 sosCoefficients[i][5],
142 sosCoefficients[i][0],
143 sosCoefficients[i][1],
144 sosCoefficients[i][2]);
145 }
146 }
147
148 public:
154 template <typename Sample>
155 inline Sample filter(const Sample in)
156 {
157 double out = in;
158 StateType* state = m_states;
159 for (const auto &stage: m_stages)
160 out = (state++)->filter(out, stage);
161 return static_cast<Sample> (out);
162 }
163
167 const Cascade::Storage getCascadeStorage()
168 {
169 Cascade::Storage s;
170 s.maxStages = MaxStages;
171 s.stageArray = m_stages;
172 return s;
173 }
174
175 private:
176 Biquad m_stages[MaxStages] = {};
177 StateType m_states[MaxStages] = {};
178 };
179
180 }
181
182 #endif
Iir::Biquad
Definition: Biquad.h:52
Iir::Biquad::setCoefficients
void setCoefficients(double a0, double a1, double a2, double b0, double b1, double b2)
Definition: Biquad.cpp:123
Iir::CascadeStages
Definition: Cascade.h:114
Iir::CascadeStages::getCascadeStorage
const Cascade::Storage getCascadeStorage()
Definition: Cascade.h:167
Iir::CascadeStages::setup
void setup(const double(&sosCoefficients)[MaxStages][6])
Definition: Cascade.h:136
Iir::CascadeStages::filter
Sample filter(const Sample in)
Definition: Cascade.h:155
Iir::CascadeStages::reset
void reset()
Definition: Cascade.h:124
Iir::Cascade
Definition: Cascade.h:50
Iir::Cascade::getNumStages
int getNumStages() const
Definition: Cascade.h:68
Iir::LayoutBase
Definition: Layout.h:63
Iir
Definition: Biquad.cpp:40
Iir::Cascade::Storage
Definition: Cascade.h:60