You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.8KB

  1. #ifndef STK_FIR_H
  2. #define STK_FIR_H
  3. #include "Filter.h"
  4. namespace stk {
  5. /***************************************************/
  6. /*! \class Fir
  7. \brief STK general finite impulse response filter class.
  8. This class provides a generic digital filter structure that can be
  9. used to implement FIR filters. For filters with feedback terms,
  10. the Iir class should be used.
  11. In particular, this class implements the standard difference
  12. equation:
  13. y[n] = b[0]*x[n] + ... + b[nb]*x[n-nb]
  14. The \e gain parameter is applied at the filter input and does not
  15. affect the coefficient values. The default gain value is 1.0.
  16. This structure results in one extra multiply per computed sample,
  17. but allows easy control of the overall filter gain.
  18. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  19. */
  20. /***************************************************/
  21. class Fir : public Filter
  22. {
  23. public:
  24. //! Default constructor creates a zero-order pass-through "filter".
  25. Fir( void );
  26. //! Overloaded constructor which takes filter coefficients.
  27. /*!
  28. An StkError can be thrown if the coefficient vector size is
  29. zero.
  30. */
  31. Fir( std::vector<StkFloat> &coefficients );
  32. //! Class destructor.
  33. ~Fir( void );
  34. //! Set filter coefficients.
  35. /*!
  36. An StkError can be thrown if the coefficient vector size is
  37. zero. The internal state of the filter is not cleared unless the
  38. \e clearState flag is \c true.
  39. */
  40. void setCoefficients( std::vector<StkFloat> &coefficients, bool clearState = false );
  41. //! Return the last computed output value.
  42. StkFloat lastOut( void ) const { return lastFrame_[0]; };
  43. //! Input one sample to the filter and return one output.
  44. StkFloat tick( StkFloat input );
  45. //! Take a channel of the StkFrames object as inputs to the filter and replace with corresponding outputs.
  46. /*!
  47. The StkFrames argument reference is returned. The \c channel
  48. argument must be less than the number of channels in the
  49. StkFrames argument (the first channel is specified by 0).
  50. However, range checking is only performed if _STK_DEBUG_ is
  51. defined during compilation, in which case an out-of-range value
  52. will trigger an StkError exception.
  53. */
  54. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  55. //! Take a channel of the \c iFrames object as inputs to the filter and write outputs to the \c oFrames object.
  56. /*!
  57. The \c iFrames object reference is returned. Each channel
  58. argument must be less than the number of channels in the
  59. corresponding StkFrames argument (the first channel is specified
  60. by 0). However, range checking is only performed if _STK_DEBUG_
  61. is defined during compilation, in which case an out-of-range value
  62. will trigger an StkError exception.
  63. */
  64. StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
  65. protected:
  66. };
  67. inline StkFloat Fir :: tick( StkFloat input )
  68. {
  69. lastFrame_[0] = 0.0;
  70. inputs_[0] = gain_ * input;
  71. for ( unsigned int i=(unsigned int)(b_.size())-1; i>0; i-- ) {
  72. lastFrame_[0] += b_[i] * inputs_[i];
  73. inputs_[i] = inputs_[i-1];
  74. }
  75. lastFrame_[0] += b_[0] * inputs_[0];
  76. return lastFrame_[0];
  77. }
  78. inline StkFrames& Fir :: tick( StkFrames& frames, unsigned int channel )
  79. {
  80. #if defined(_STK_DEBUG_)
  81. if ( channel >= frames.channels() ) {
  82. oStream_ << "Fir::tick(): channel and StkFrames arguments are incompatible!";
  83. handleError( StkError::FUNCTION_ARGUMENT );
  84. }
  85. #endif
  86. StkFloat *samples = &frames[channel];
  87. unsigned int i, hop = frames.channels();
  88. for ( unsigned int j=0; j<frames.frames(); j++, samples += hop ) {
  89. inputs_[0] = gain_ * *samples;
  90. *samples = 0.0;
  91. for ( i=(unsigned int)b_.size()-1; i>0; i-- ) {
  92. *samples += b_[i] * inputs_[i];
  93. inputs_[i] = inputs_[i-1];
  94. }
  95. *samples += b_[0] * inputs_[0];
  96. }
  97. lastFrame_[0] = *(samples-hop);
  98. return frames;
  99. }
  100. inline StkFrames& Fir :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
  101. {
  102. #if defined(_STK_DEBUG_)
  103. if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
  104. oStream_ << "Fir::tick(): channel and StkFrames arguments are incompatible!";
  105. handleError( StkError::FUNCTION_ARGUMENT );
  106. }
  107. #endif
  108. StkFloat *iSamples = &iFrames[iChannel];
  109. StkFloat *oSamples = &oFrames[oChannel];
  110. unsigned int i, iHop = iFrames.channels(), oHop = oFrames.channels();
  111. for ( unsigned int j=0; j<iFrames.frames(); j++, iSamples += iHop, oSamples += oHop ) {
  112. inputs_[0] = gain_ * *iSamples;
  113. *oSamples = 0.0;
  114. for ( i=(unsigned int)b_.size()-1; i>0; i-- ) {
  115. *oSamples += b_[i] * inputs_[i];
  116. inputs_[i] = inputs_[i-1];
  117. }
  118. *oSamples += b_[0] * inputs_[0];
  119. }
  120. lastFrame_[0] = *(oSamples-oHop);
  121. return iFrames;
  122. }
  123. } // stk namespace
  124. #endif