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.

114 lines
3.3KB

  1. #ifndef STK_POLEZERO_H
  2. #define STK_POLEZERO_H
  3. #include "Filter.h"
  4. namespace stk {
  5. /***************************************************/
  6. /*! \class PoleZero
  7. \brief STK one-pole, one-zero filter class.
  8. This class implements a one-pole, one-zero digital filter. A
  9. method is provided for creating an allpass filter with a given
  10. coefficient. Another method is provided to create a DC blocking
  11. filter.
  12. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  13. */
  14. /***************************************************/
  15. class PoleZero : public Filter
  16. {
  17. public:
  18. //! Default constructor creates a first-order pass-through filter.
  19. PoleZero();
  20. //! Class destructor.
  21. ~PoleZero();
  22. //! Set the b[0] coefficient value.
  23. void setB0( StkFloat b0 ) { b_[0] = b0; };
  24. //! Set the b[1] coefficient value.
  25. void setB1( StkFloat b1 ) { b_[1] = b1; };
  26. //! Set the a[1] coefficient value.
  27. void setA1( StkFloat a1 ) { a_[1] = a1; };
  28. //! Set all filter coefficients.
  29. void setCoefficients( StkFloat b0, StkFloat b1, StkFloat a1, bool clearState = false );
  30. //! Set the filter for allpass behavior using \e coefficient.
  31. /*!
  32. This method uses \e coefficient to create an allpass filter,
  33. which has unity gain at all frequencies. Note that the
  34. \e coefficient magnitude must be less than one to maintain
  35. filter stability.
  36. */
  37. void setAllpass( StkFloat coefficient );
  38. //! Create a DC blocking filter with the given pole position in the z-plane.
  39. /*!
  40. This method sets the given pole position, together with a zero
  41. at z=1, to create a DC blocking filter. The argument magnitude
  42. should be close to (but less than) one to minimize low-frequency
  43. attenuation.
  44. */
  45. void setBlockZero( StkFloat thePole = 0.99 );
  46. //! Return the last computed output value.
  47. StkFloat lastOut( void ) const { return lastFrame_[0]; };
  48. //! Input one sample to the filter and return one output.
  49. StkFloat tick( StkFloat input );
  50. //! Take a channel of the StkFrames object as inputs to the filter and replace with corresponding outputs.
  51. /*!
  52. The \c channel argument must be less than the number of
  53. channels in the StkFrames argument (the first channel is specified
  54. by 0). However, range checking is only performed if _STK_DEBUG_
  55. is defined during compilation, in which case an out-of-range value
  56. will trigger an StkError exception.
  57. */
  58. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  59. };
  60. inline StkFloat PoleZero :: tick( StkFloat input )
  61. {
  62. inputs_[0] = gain_ * input;
  63. lastFrame_[0] = b_[0] * inputs_[0] + b_[1] * inputs_[1] - a_[1] * outputs_[1];
  64. inputs_[1] = inputs_[0];
  65. outputs_[1] = lastFrame_[0];
  66. return lastFrame_[0];
  67. }
  68. inline StkFrames& PoleZero :: tick( StkFrames& frames, unsigned int channel )
  69. {
  70. #if defined(_STK_DEBUG_)
  71. if ( channel >= frames.channels() ) {
  72. oStream_ << "PoleZero::tick(): channel and StkFrames arguments are incompatible!";
  73. handleError( StkError::FUNCTION_ARGUMENT );
  74. }
  75. #endif
  76. StkFloat *samples = &frames[channel];
  77. unsigned int hop = frames.channels();
  78. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  79. inputs_[0] = gain_ * *samples;
  80. *samples = b_[0] * inputs_[0] + b_[1] * inputs_[1] - a_[1] * outputs_[1];
  81. inputs_[1] = inputs_[0];
  82. outputs_[1] = *samples;
  83. }
  84. lastFrame_[0] = outputs_[1];
  85. return frames;
  86. }
  87. } // stk namespace
  88. #endif