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.

126 lines
3.4KB

  1. /***************************************************/
  2. /*! \class Iir
  3. \brief STK general infinite impulse response filter class.
  4. This class provides a generic digital filter structure that can be
  5. used to implement IIR filters. For filters containing only
  6. feedforward terms, the Fir class is slightly more efficient.
  7. In particular, this class implements the standard difference
  8. equation:
  9. a[0]*y[n] = b[0]*x[n] + ... + b[nb]*x[n-nb] -
  10. a[1]*y[n-1] - ... - a[na]*y[n-na]
  11. If a[0] is not equal to 1, the filter coeffcients are normalized
  12. by a[0].
  13. The \e gain parameter is applied at the filter input and does not
  14. affect the coefficient values. The default gain value is 1.0.
  15. This structure results in one extra multiply per computed sample,
  16. but allows easy control of the overall filter gain.
  17. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  18. */
  19. /***************************************************/
  20. #include "Iir.h"
  21. namespace stk {
  22. Iir :: Iir()
  23. {
  24. // The default constructor should setup for pass-through.
  25. b_.push_back( 1.0 );
  26. a_.push_back( 1.0 );
  27. inputs_.resize( 1, 1, 0.0 );
  28. outputs_.resize( 1, 1, 0.0 );
  29. }
  30. Iir :: Iir( std::vector<StkFloat> &bCoefficients, std::vector<StkFloat> &aCoefficients )
  31. {
  32. // Check the arguments.
  33. if ( bCoefficients.size() == 0 || aCoefficients.size() == 0 ) {
  34. oStream_ << "Iir: a and b coefficient vectors must both have size > 0!";
  35. handleError( StkError::FUNCTION_ARGUMENT );
  36. }
  37. if ( aCoefficients[0] == 0.0 ) {
  38. oStream_ << "Iir: a[0] coefficient cannot == 0!";
  39. handleError( StkError::FUNCTION_ARGUMENT );
  40. }
  41. gain_ = 1.0;
  42. b_ = bCoefficients;
  43. a_ = aCoefficients;
  44. inputs_.resize( b_.size(), 1, 0.0 );
  45. outputs_.resize( a_.size(), 1, 0.0 );
  46. this->clear();
  47. }
  48. Iir :: ~Iir()
  49. {
  50. }
  51. void Iir :: setCoefficients( std::vector<StkFloat> &bCoefficients, std::vector<StkFloat> &aCoefficients, bool clearState )
  52. {
  53. this->setNumerator( bCoefficients, false );
  54. this->setDenominator( aCoefficients, false );
  55. if ( clearState ) this->clear();
  56. }
  57. void Iir :: setNumerator( std::vector<StkFloat> &bCoefficients, bool clearState )
  58. {
  59. // Check the argument.
  60. if ( bCoefficients.size() == 0 ) {
  61. oStream_ << "Iir::setNumerator: coefficient vector must have size > 0!";
  62. handleError( StkError::FUNCTION_ARGUMENT );
  63. }
  64. if ( b_.size() != bCoefficients.size() ) {
  65. b_ = bCoefficients;
  66. inputs_.resize( b_.size(), 1, 0.0 );
  67. }
  68. else {
  69. for ( unsigned int i=0; i<b_.size(); i++ ) b_[i] = bCoefficients[i];
  70. }
  71. if ( clearState ) this->clear();
  72. }
  73. void Iir :: setDenominator( std::vector<StkFloat> &aCoefficients, bool clearState )
  74. {
  75. // Check the argument.
  76. if ( aCoefficients.size() == 0 ) {
  77. oStream_ << "Iir::setDenominator: coefficient vector must have size > 0!";
  78. handleError( StkError::FUNCTION_ARGUMENT );
  79. }
  80. if ( aCoefficients[0] == 0.0 ) {
  81. oStream_ << "Iir::setDenominator: a[0] coefficient cannot == 0!";
  82. handleError( StkError::FUNCTION_ARGUMENT );
  83. }
  84. if ( a_.size() != aCoefficients.size() ) {
  85. a_ = aCoefficients;
  86. outputs_.resize( a_.size(), 1, 0.0 );
  87. }
  88. else {
  89. for ( unsigned int i=0; i<a_.size(); i++ ) a_[i] = aCoefficients[i];
  90. }
  91. if ( clearState ) this->clear();
  92. // Scale coefficients by a[0] if necessary
  93. if ( a_[0] != 1.0 ) {
  94. unsigned int i;
  95. for ( i=0; i<b_.size(); i++ ) b_[i] /= a_[0];
  96. for ( i=1; i<a_.size(); i++ ) a_[i] /= a_[0];
  97. }
  98. }
  99. } // stk namespace