Cascade.h
#ifndef IIR1_CASCADE_H
#define IIR1_CASCADE_H
#include "Common.h"
#include "Biquad.h"
#include "Layout.h"
#include "MathSupplement.h"
#include <stdexcept>
namespace Iir {
class DllExport Cascade
{
public:
struct DllExport Storage
{
Storage (int maxStages_, Biquad* const stageArray_)
: maxStages (maxStages_)
, stageArray (stageArray_)
{
}
const int maxStages;
Biquad* const stageArray;
};
int getNumStages () const
{
return m_numStages;
}
const Biquad& operator[] (int index)
{
if ((index < 0) || (index >= m_numStages))
throw std::invalid_argument("Index out of bounds.");
return m_stageArray[index];
}
complex_t response (double normalizedFrequency) const;
std::vector<PoleZeroPair> getPoleZeros () const;
protected:
Cascade ();
void setCascadeStorage (const Storage& storage);
void applyScale (double scale);
void setLayout (const LayoutBase& proto);
private:
int m_numStages;
int m_maxStages;
Biquad* m_stageArray;
};
//------------------------------------------------------------------------------
template <int MaxStages,class StateType>
class DllExport CascadeStages {
public:
void reset ()
{
for (auto &state: m_states)
state.reset();
}
public:
void setup (const double (&sosCoefficients)[MaxStages][6]) {
for (int i = 0; i < MaxStages; i++) {
m_stages[i].setCoefficients(
sosCoefficients[i][3],
sosCoefficients[i][4],
sosCoefficients[i][5],
sosCoefficients[i][0],
sosCoefficients[i][1],
sosCoefficients[i][2]);
}
}
public:
template <typename Sample>
inline Sample filter(const Sample in)
{
double out = in;
StateType* state = m_states;
for (const auto &stage: m_stages)
out = (state++)->filter(out, stage);
return static_cast<Sample> (out);
}
const Cascade::Storage getCascadeStorage()
{
return Cascade::Storage (MaxStages, m_stages);
}
private:
Biquad m_stages[MaxStages];
StateType m_states[MaxStages];
};
}
#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