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 #include <stdexcept>
44 
45 namespace Iir {
46 
50  class DllExport Cascade
51  {
52  public:
53 
57  struct DllExport Storage
58  {
64  Storage (int maxStages_, Biquad* const stageArray_)
65  : maxStages (maxStages_)
66  , stageArray (stageArray_)
67  {
68  }
69 
70  const int maxStages;
71  Biquad* const stageArray;
72  };
73 
77  int getNumStages () const
78  {
79  return m_numStages;
80  }
81 
85  const Biquad& operator[] (int index)
86  {
87  if ((index < 0) || (index >= m_numStages))
88  throw std::invalid_argument("Index out of bounds.");
89  return m_stageArray[index];
90  }
91 
96  complex_t response (double normalizedFrequency) const;
97 
101  std::vector<PoleZeroPair> getPoleZeros () const;
102 
103  protected:
104  Cascade ();
105 
106  void setCascadeStorage (const Storage& storage);
107 
108  void applyScale (double scale);
109 
110  void setLayout (const LayoutBase& proto);
111 
112  private:
113  int m_numStages;
114  int m_maxStages;
115  Biquad* m_stageArray;
116  };
117 
118 
119 
120 //------------------------------------------------------------------------------
121 
126  template <int MaxStages,class StateType>
127  class DllExport CascadeStages {
128  public:
132  void reset ()
133  {
134  for (auto &state: m_states)
135  state.reset();
136  }
137 
138  public:
144  void setup (const double (&sosCoefficients)[MaxStages][6]) {
145  for (int i = 0; i < MaxStages; i++) {
146  m_stages[i].setCoefficients(
147  sosCoefficients[i][3],
148  sosCoefficients[i][4],
149  sosCoefficients[i][5],
150  sosCoefficients[i][0],
151  sosCoefficients[i][1],
152  sosCoefficients[i][2]);
153  }
154  }
155 
156  public:
162  template <typename Sample>
163  inline Sample filter(const Sample in)
164  {
165  double out = in;
166  StateType* state = m_states;
167  for (const auto &stage: m_stages)
168  out = (state++)->filter(out, stage);
169  return static_cast<Sample> (out);
170  }
171 
176  {
177  return Cascade::Storage (MaxStages, m_stages);
178  }
179 
180  private:
181  Biquad m_stages[MaxStages];
182  StateType m_states[MaxStages];
183  };
184 
185 }
186 
187 #endif
Iir::Biquad
Definition: Biquad.h:52
Iir::Cascade::Storage
Definition: Cascade.h:57
Iir::CascadeStages
Definition: Cascade.h:127
Iir::CascadeStages::setup
void setup(const double(&sosCoefficients)[MaxStages][6])
Definition: Cascade.h:144
Iir::CascadeStages::reset
void reset()
Definition: Cascade.h:132
Iir::CascadeStages::filter
Sample filter(const Sample in)
Definition: Cascade.h:163
Iir::CascadeStages::getCascadeStorage
const Cascade::Storage getCascadeStorage()
Definition: Cascade.h:175
Iir::Cascade
Definition: Cascade.h:50
Iir
Definition: Biquad.cpp:41
Iir::LayoutBase
Definition: Layout.h:63
Iir::Cascade::Storage::Storage
Storage(int maxStages_, Biquad *const stageArray_)
Definition: Cascade.h:64
Iir::Cascade::getNumStages
int getNumStages() const
Definition: Cascade.h:77