Arduino STK  4.6.2
Fir.h
1 #ifndef STK_FIR_H
2 #define STK_FIR_H
3 
4 #include "Filter.h"
5 
6 namespace stk {
7 
8 /***************************************************/
28 /***************************************************/
29 
30 class Fir : public Filter
31 {
32 public:
34  Fir( void );
35 
37 
41  Fir( std::vector<StkFloat> &coefficients );
42 
44  ~Fir( void );
45 
47 
52  void setCoefficients( std::vector<StkFloat> &coefficients, bool clearState = false );
53 
55  StkFloat lastOut( void ) const { return lastFrame_[0]; };
56 
58  StkFloat tick( StkFloat input );
59 
61 
69  StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
70 
72 
80  StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
81 
82 protected:
83 
84 };
85 
86 inline StkFloat Fir :: tick( StkFloat input )
87 {
88  lastFrame_[0] = 0.0;
89  inputs_[0] = gain_ * input;
90 
91  for ( unsigned int i=(unsigned int)(b_.size())-1; i>0; i-- ) {
92  lastFrame_[0] += b_[i] * inputs_[i];
93  inputs_[i] = inputs_[i-1];
94  }
95  lastFrame_[0] += b_[0] * inputs_[0];
96 
97  return lastFrame_[0];
98 }
99 
100 inline StkFrames& Fir :: tick( StkFrames& frames, unsigned int channel )
101 {
102 #if defined(_STK_DEBUG_)
103  if ( channel >= frames.channels() ) {
104  oStream_ << "Fir::tick(): channel and StkFrames arguments are incompatible!";
105  handleError( StkError::FUNCTION_ARGUMENT );
106  }
107 #endif
108 
109  StkFloat *samples = &frames[channel];
110  unsigned int i, hop = frames.channels();
111  for ( unsigned int j=0; j<frames.frames(); j++, samples += hop ) {
112  inputs_[0] = gain_ * *samples;
113  *samples = 0.0;
114 
115  for ( i=(unsigned int)b_.size()-1; i>0; i-- ) {
116  *samples += b_[i] * inputs_[i];
117  inputs_[i] = inputs_[i-1];
118  }
119  *samples += b_[0] * inputs_[0];
120  }
121 
122  lastFrame_[0] = *(samples-hop);
123  return frames;
124 }
125 
126 inline StkFrames& Fir :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
127 {
128 #if defined(_STK_DEBUG_)
129  if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
130  oStream_ << "Fir::tick(): channel and StkFrames arguments are incompatible!";
131  handleError( StkError::FUNCTION_ARGUMENT );
132  }
133 #endif
134 
135  StkFloat *iSamples = &iFrames[iChannel];
136  StkFloat *oSamples = &oFrames[oChannel];
137  unsigned int i, iHop = iFrames.channels(), oHop = oFrames.channels();
138  for ( unsigned int j=0; j<iFrames.frames(); j++, iSamples += iHop, oSamples += oHop ) {
139  inputs_[0] = gain_ * *iSamples;
140  *oSamples = 0.0;
141 
142  for ( i=(unsigned int)b_.size()-1; i>0; i-- ) {
143  *oSamples += b_[i] * inputs_[i];
144  inputs_[i] = inputs_[i-1];
145  }
146  *oSamples += b_[0] * inputs_[0];
147  }
148 
149  lastFrame_[0] = *(oSamples-oHop);
150  return iFrames;
151 }
152 
153 } // stk namespace
154 
155 #endif
STK abstract filter class.
Definition: Filter.h:23
STK general finite impulse response filter class.
Definition: Fir.h:31
StkFloat lastOut(void) const
Return the last computed output value.
Definition: Fir.h:55
~Fir(void)
Class destructor.
Fir(void)
Default constructor creates a zero-order pass-through "filter".
Fir(std::vector< StkFloat > &coefficients)
Overloaded constructor which takes filter coefficients.
void setCoefficients(std::vector< StkFloat > &coefficients, bool clearState=false)
Set filter coefficients.
StkFloat tick(StkFloat input)
Input one sample to the filter and return one output.
Definition: Fir.h:86
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