Arduino STK  4.6.2
Iir.h
1 #ifndef STK_IIR_H
2 #define STK_IIR_H
3 
4 #include "Filter.h"
5 
6 namespace stk {
7 
8 /***************************************************/
32 /***************************************************/
33 
34 class Iir : public Filter
35 {
36 public:
38  Iir( void );
39 
41 
45  Iir( std::vector<StkFloat> &bCoefficients, std::vector<StkFloat> &aCoefficients );
46 
48  ~Iir( void );
49 
51 
58  void setCoefficients( std::vector<StkFloat> &bCoefficients, std::vector<StkFloat> &aCoefficients, bool clearState = false );
59 
61 
68  void setNumerator( std::vector<StkFloat> &bCoefficients, bool clearState = false );
69 
71 
80  void setDenominator( std::vector<StkFloat> &aCoefficients, bool clearState = false );
81 
83  StkFloat lastOut( void ) const { return lastFrame_[0]; };
84 
86  StkFloat tick( StkFloat input );
87 
89 
97  StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
98 
100 
108  StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
109 
110 protected:
111 
112 };
113 
114 inline StkFloat Iir :: tick( StkFloat input )
115 {
116  size_t i;
117 
118  outputs_[0] = 0.0;
119  inputs_[0] = gain_ * input;
120  for ( i=b_.size()-1; i>0; i-- ) {
121  outputs_[0] += b_[i] * inputs_[i];
122  inputs_[i] = inputs_[i-1];
123  }
124  outputs_[0] += b_[0] * inputs_[0];
125 
126  for ( i=a_.size()-1; i>0; i-- ) {
127  outputs_[0] += -a_[i] * outputs_[i];
128  outputs_[i] = outputs_[i-1];
129  }
130 
131  lastFrame_[0] = outputs_[0];
132  return lastFrame_[0];
133 }
134 
135 inline StkFrames& Iir :: tick( StkFrames& frames, unsigned int channel )
136 {
137 #if defined(_STK_DEBUG_)
138  if ( channel >= frames.channels() ) {
139  oStream_ << "Iir::tick(): channel and StkFrames arguments are incompatible!";
140  handleError( StkError::FUNCTION_ARGUMENT );
141  }
142 #endif
143 
144  StkFloat *samples = &frames[channel];
145  size_t i;
146  unsigned int hop = frames.channels();
147  for ( unsigned int j=0; j<frames.frames(); j++, samples += hop ) {
148  outputs_[0] = 0.0;
149  inputs_[0] = gain_ * *samples;
150  for ( i=b_.size()-1; i>0; i-- ) {
151  outputs_[0] += b_[i] * inputs_[i];
152  inputs_[i] = inputs_[i-1];
153  }
154  outputs_[0] += b_[0] * inputs_[0];
155 
156  for ( i=a_.size()-1; i>0; i-- ) {
157  outputs_[0] += -a_[i] * outputs_[i];
158  outputs_[i] = outputs_[i-1];
159  }
160 
161  *samples = outputs_[0];
162  }
163 
164  lastFrame_[0] = *(samples-hop);
165  return frames;
166 }
167 
168 inline StkFrames& Iir :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
169 {
170 #if defined(_STK_DEBUG_)
171  if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
172  oStream_ << "Iir::tick(): channel and StkFrames arguments are incompatible!";
173  handleError( StkError::FUNCTION_ARGUMENT );
174  }
175 #endif
176 
177  StkFloat *iSamples = &iFrames[iChannel];
178  StkFloat *oSamples = &oFrames[oChannel];
179  size_t i;
180  unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
181  for ( unsigned int j=0; j<iFrames.frames(); j++, iSamples += iHop, oSamples += oHop ) {
182  outputs_[0] = 0.0;
183  inputs_[0] = gain_ * *iSamples;
184  for ( i=b_.size()-1; i>0; i-- ) {
185  outputs_[0] += b_[i] * inputs_[i];
186  inputs_[i] = inputs_[i-1];
187  }
188  outputs_[0] += b_[0] * inputs_[0];
189 
190  for ( i=a_.size()-1; i>0; i-- ) {
191  outputs_[0] += -a_[i] * outputs_[i];
192  outputs_[i] = outputs_[i-1];
193  }
194 
195  *oSamples = outputs_[0];
196  }
197 
198  lastFrame_[0] = *(oSamples-oHop);
199  return iFrames;
200 }
201 
202 } // stk namespace
203 
204 #endif
STK abstract filter class.
Definition: Filter.h:23
STK general infinite impulse response filter class.
Definition: Iir.h:35
StkFloat lastOut(void) const
Return the last computed output value.
Definition: Iir.h:83
Iir(void)
Default constructor creates a zero-order pass-through "filter".
void setNumerator(std::vector< StkFloat > &bCoefficients, bool clearState=false)
Set numerator coefficients.
StkFloat tick(StkFloat input)
Input one sample to the filter and return one output.
Definition: Iir.h:114
void setCoefficients(std::vector< StkFloat > &bCoefficients, std::vector< StkFloat > &aCoefficients, bool clearState=false)
Set filter coefficients.
Iir(std::vector< StkFloat > &bCoefficients, std::vector< StkFloat > &aCoefficients)
Overloaded constructor which takes filter coefficients.
~Iir(void)
Class destructor.
void setDenominator(std::vector< StkFloat > &aCoefficients, bool clearState=false)
Set denominator coefficients.
An STK class to handle vectorized audio data.
Definition: Stk.h:287
unsigned int channels(void) const
Return the number of channels represented by the data.
Definition: Stk.h:415
unsigned int frames(void) const
Return the number of sample frames represented by the data.
Definition: Stk.h:418
static void handleError(const char *message, StkError::Type type)
Static function for error reporting and handling using c-strings.
The STK namespace.
Definition: ADSR.h:8