iir1: iir/Cascade.h Source File

iir1

Loading...
Searching...
No Matches
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
44namespace Iir {
45
49 class IIR_EXPORT Cascade
50 {
51 public:
52
53 Cascade () = default;
54
59 struct IIR_EXPORT Storage
60 {
61 Storage() = delete;
62 Storage(const int maxNumBiquads, Biquad* const biquadArray) : maxStages(maxNumBiquads), stageArray(biquadArray) {}
63 const int maxStages = 0;
64 Biquad* const stageArray = nullptr;
65 };
66
70 int getNumStages () const
71 {
72 return m_numStages;
73 }
74
78 const Biquad& operator[] (int index)
79 {
80 if ((index < 0) || (index >= m_numStages))
81 throw_invalid_argument("Index out of bounds.");
82 return m_stageArray[index];
83 }
84
89 complex_t response (double normalizedFrequency) const;
90
94 std::vector<PoleZeroPair> getPoleZeros () const;
95
96 void setCascadeStorage (const Storage& storage);
97
98 void applyScale (double scale);
99
100 void setLayout (const LayoutBase& proto);
101
102 private:
103 int m_numStages = 0;
104 int m_maxStages = 0;
105 Biquad* m_stageArray = nullptr;
106 };
107
108
109//------------------------------------------------------------------------------
110
115 template <int MaxStages,class StateType>
116 class CascadeStages {
117
118 public:
119 CascadeStages() = default;
120
121
122 public:
126 void reset ()
127 {
128 for (auto &state: m_states)
129 state.reset();
130 }
131
132 public:
138 void setup (const double (&sosCoefficients)[MaxStages][6]) {
139 for (int i = 0; i < MaxStages; i++) {
140 m_stages[i].setCoefficients(
141 sosCoefficients[i][3],
142 sosCoefficients[i][4],
143 sosCoefficients[i][5],
144 sosCoefficients[i][0],
145 sosCoefficients[i][1],
146 sosCoefficients[i][2]);
147 }
148 }
149
150 public:
156 template <typename Sample>
157 inline Sample filter(const Sample in)
158 {
159 double out = in;
160 StateType* state = m_states;
161 for (const auto &stage: m_stages)
162 out = (state++)->filter(out, stage);
163 return static_cast<Sample> (out);
164 }
165
169 const Cascade::Storage getCascadeStorage()
170 {
171 const Cascade::Storage s(MaxStages, 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:116
Iir::CascadeStages::getCascadeStorage
const Cascade::Storage getCascadeStorage()
Definition Cascade.h:169
Iir::CascadeStages::setup
void setup(const double(&sosCoefficients)[MaxStages][6])
Definition Cascade.h:138
Iir::CascadeStages::filter
Sample filter(const Sample in)
Definition Cascade.h:157
Iir::CascadeStages::reset
void reset()
Definition Cascade.h:126
Iir::Cascade
Definition Cascade.h:50
Iir::Cascade::getNumStages
int getNumStages() const
Definition Cascade.h:70
Iir::LayoutBase
Definition Layout.h:63
Iir
Definition Biquad.cpp:40
Iir::Cascade::Storage
Definition Cascade.h:60