Home Information Classes Download Usage Mail List Requirements Links FAQ Tutorial

# Filter.h

    00001 #ifndef STK_FILTER_H
    00002 #define STK_FILTER_H
    00003
    00004 #include "Stk.h"
    00005 #include <vector>
    00006 #include <cmath>
    00007
    00008 namespace stk {
    00009
    00010 /***************************************************/
    00020 /***************************************************/
    00021
    00022 class Filter : public Stk
    00023 {
    00024 public:
    00026   Filter( void ) { gain_ = 1.0; channelsIn_ = 1; lastFrame_.resize( 1, 1, 0.0 ); };
    00027
    00029   unsigned int channelsIn( void ) const { return channelsIn_; };
    00030
    00032   unsigned int channelsOut( void ) const { return lastFrame_.channels(); };
    00033
    00035   virtual void clear( void );
    00036
    00038
    00042   void setGain( StkFloat gain ) { gain_ = gain; };
    00043
    00045   StkFloat getGain( void ) const { return gain_; };
    00046
    00048
    00053   StkFloat phaseDelay( StkFloat frequency );
    00054
    00056   const StkFrames& lastFrame( void ) const { return lastFrame_; };
    00057
    00059
    00067   virtual StkFrames& tick( StkFrames& frames, unsigned int channel = 0 ) = 0;
    00068
    00069 protected:
    00070
    00071   StkFloat gain_;
    00072   unsigned int channelsIn_;
    00073   StkFrames lastFrame_;
    00074
    00075   std::vector<StkFloat> b_;
    00076   std::vector<StkFloat> a_;
    00077   StkFrames outputs_;
    00078   StkFrames inputs_;
    00079
    00080 };
    00081
    00082 inline void Filter :: clear( void )
    00083 {
    00084   unsigned int i;
    00085   for ( i=0; i<inputs_.size(); i++ )
    00086     inputs_[i] = 0.0;
    00087   for ( i=0; i<outputs_.size(); i++ )
    00088     outputs_[i] = 0.0;
    00089   for ( i=0; i<lastFrame_.size(); i++ )
    00090     lastFrame_[i] = 0.0;
    00091 }
    00092
    00093 inline StkFloat Filter :: phaseDelay( StkFloat frequency )
    00094 {
    00095   if ( frequency <= 0.0 || frequency > 0.5 * Stk::sampleRate() ) {
    00096     oStream_ << "Filter::phaseDelay: argument (" << frequency << ") is out of range!";
    00097     handleError( StkError::WARNING ); return 0.0;
    00098   }
    00099
    00100   StkFloat omegaT = 2 * PI * frequency / Stk::sampleRate();
    00101   StkFloat real = 0.0, imag = 0.0;
    00102   for ( unsigned int i=0; i<b_.size(); i++ ) {
    00103     real += b_[i] * std::cos( i * omegaT );
    00104     imag -= b_[i] * std::sin( i * omegaT );
    00105   }
    00106   real *= gain_;
    00107   imag *= gain_;
    00108
    00109   StkFloat phase = atan2( imag, real );
    00110
    00111   real = 0.0, imag = 0.0;
    00112   for ( unsigned int i=0; i<a_.size(); i++ ) {
    00113     real += a_[i] * std::cos( i * omegaT );
    00114     imag -= a_[i] * std::sin( i * omegaT );
    00115   }
    00116
    00117   phase -= std::atan2( imag, real );
    00118   phase = std::fmod( -phase, 2 * PI );
    00119   return phase / omegaT;
    00120 }
    00121
    00122 } // stk namespace
    00123
    00124 #endif

The Synthesis ToolKit in C++ (STK)
©1995-2012 Perry R. Cook and Gary P. Scavone. All Rights Reserved.