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.8KB

  1. /***************************************************/
  2. /*! \class Fir
  3. \brief STK general finite impulse response filter class.
  4. This class provides a generic digital filter structure that can be
  5. used to implement FIR filters. For filters with feedback terms,
  6. the Iir class should be used.
  7. In particular, this class implements the standard difference
  8. equation:
  9. y[n] = b[0]*x[n] + ... + b[nb]*x[n-nb]
  10. The \e gain parameter is applied at the filter input and does not
  11. affect the coefficient values. The default gain value is 1.0.
  12. This structure results in one extra multiply per computed sample,
  13. but allows easy control of the overall filter gain.
  14. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  15. */
  16. /***************************************************/
  17. #include "Fir.h"
  18. #include <cmath>
  19. namespace stk {
  20. Fir :: Fir()
  21. {
  22. // The default constructor should setup for pass-through.
  23. b_.push_back( 1.0 );
  24. inputs_.resize( 1, 1, 0.0 );
  25. }
  26. Fir :: Fir( std::vector<StkFloat> &coefficients )
  27. {
  28. // Check the arguments.
  29. if ( coefficients.size() == 0 ) {
  30. oStream_ << "Fir: coefficient vector must have size > 0!";
  31. handleError( StkError::FUNCTION_ARGUMENT );
  32. }
  33. gain_ = 1.0;
  34. b_ = coefficients;
  35. inputs_.resize( b_.size(), 1, 0.0 );
  36. this->clear();
  37. }
  38. Fir :: ~Fir()
  39. {
  40. }
  41. void Fir :: setCoefficients( std::vector<StkFloat> &coefficients, bool clearState )
  42. {
  43. // Check the argument.
  44. if ( coefficients.size() == 0 ) {
  45. oStream_ << "Fir::setCoefficients: coefficient vector must have size > 0!";
  46. handleError( StkError::FUNCTION_ARGUMENT );
  47. }
  48. if ( b_.size() != coefficients.size() ) {
  49. b_ = coefficients;
  50. inputs_.resize( b_.size(), 1, 0.0 );
  51. }
  52. else {
  53. for ( unsigned int i=0; i<b_.size(); i++ ) b_[i] = coefficients[i];
  54. }
  55. if ( clearState ) this->clear();
  56. }
  57. } // stk namespace