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.

75 lines
1.9KB

  1. /***************************************************/
  2. /*! \class TwoZero
  3. \brief STK two-zero filter class.
  4. This class implements a two-zero digital filter. A method is
  5. provided for creating a "notch" in the frequency response while
  6. maintaining a constant filter gain.
  7. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  8. */
  9. /***************************************************/
  10. #include "TwoZero.h"
  11. #include <cmath>
  12. namespace stk {
  13. TwoZero :: TwoZero( void )
  14. {
  15. b_.resize( 3, 0.0 );
  16. inputs_.resize( 3, 1, 0.0 );
  17. b_[0] = 1.0;
  18. Stk::addSampleRateAlert( this );
  19. }
  20. TwoZero :: ~TwoZero()
  21. {
  22. Stk::removeSampleRateAlert( this );
  23. }
  24. void TwoZero :: sampleRateChanged( StkFloat newRate, StkFloat oldRate )
  25. {
  26. if ( !ignoreSampleRateChange_ ) {
  27. oStream_ << "TwoZero::sampleRateChanged: you may need to recompute filter coefficients!";
  28. handleError( StkError::WARNING );
  29. }
  30. }
  31. void TwoZero :: setCoefficients( StkFloat b0, StkFloat b1, StkFloat b2, bool clearState )
  32. {
  33. b_[0] = b0;
  34. b_[1] = b1;
  35. b_[2] = b2;
  36. if ( clearState ) this->clear();
  37. }
  38. void TwoZero :: setNotch( StkFloat frequency, StkFloat radius )
  39. {
  40. #if defined(_STK_DEBUG_)
  41. if ( frequency < 0.0 || frequency > 0.5 * Stk::sampleRate() ) {
  42. oStream_ << "TwoZero::setNotch: frequency argument (" << frequency << ") is out of range!";
  43. handleError( StkError::WARNING ); return;
  44. }
  45. if ( radius < 0.0 ) {
  46. oStream_ << "TwoZero::setNotch: radius argument (" << radius << ") is negative!";
  47. handleError( StkError::WARNING ); return;
  48. }
  49. #endif
  50. b_[2] = radius * radius;
  51. b_[1] = -2.0 * radius * cos(TWO_PI * frequency / Stk::sampleRate());
  52. // Normalize the filter gain.
  53. if ( b_[1] > 0.0 ) // Maximum at z = 0.
  54. b_[0] = 1.0 / ( 1.0 + b_[1] + b_[2] );
  55. else // Maximum at z = -1.
  56. b_[0] = 1.0 / ( 1.0 - b_[1] + b_[2] );
  57. b_[1] *= b_[0];
  58. b_[2] *= b_[0];
  59. }
  60. } // stk namespace