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.

96 lines
2.9KB

  1. /***************************************************/
  2. /*! \class BlitSquare
  3. \brief STK band-limited square wave class.
  4. This class generates a band-limited square wave signal. It is
  5. derived in part from the approach reported by Stilson and Smith in
  6. "Alias-Free Digital Synthesis of Classic Analog Waveforms", 1996.
  7. The algorithm implemented in this class uses a SincM function with
  8. an even M value to achieve a bipolar bandlimited impulse train.
  9. This signal is then integrated to achieve a square waveform. The
  10. integration process has an associated DC offset so a DC blocking
  11. filter is applied at the output.
  12. The user can specify both the fundamental frequency of the
  13. waveform and the number of harmonics contained in the resulting
  14. signal.
  15. If nHarmonics is 0, then the signal will contain all harmonics up
  16. to half the sample rate. Note, however, that this setting may
  17. produce aliasing in the signal when the frequency is changing (no
  18. automatic modification of the number of harmonics is performed by
  19. the setFrequency() function). Also note that the harmonics of a
  20. square wave fall at odd integer multiples of the fundamental, so
  21. aliasing will happen with a lower fundamental than with the other
  22. Blit waveforms. This class is not guaranteed to be well behaved
  23. in the presence of significant aliasing.
  24. Based on initial code of Robin Davies, 2005
  25. Modified algorithm code by Gary Scavone, 2005 - 2010.
  26. */
  27. /***************************************************/
  28. #include "BlitSquare.h"
  29. namespace stk {
  30. BlitSquare:: BlitSquare( StkFloat frequency )
  31. {
  32. if ( frequency <= 0.0 ) {
  33. oStream_ << "BlitSquare::BlitSquare: argument (" << frequency << ") must be positive!";
  34. handleError( StkError::FUNCTION_ARGUMENT );
  35. }
  36. nHarmonics_ = 0;
  37. this->setFrequency( frequency );
  38. this->reset();
  39. }
  40. BlitSquare :: ~BlitSquare()
  41. {
  42. }
  43. void BlitSquare :: reset()
  44. {
  45. phase_ = 0.0;
  46. lastFrame_[0] = 0.0;
  47. dcbState_ = 0.0;
  48. lastBlitOutput_ = 0;
  49. }
  50. void BlitSquare :: setFrequency( StkFloat frequency )
  51. {
  52. if ( frequency <= 0.0 ) {
  53. oStream_ << "BlitSquare::setFrequency: argument (" << frequency << ") must be positive!";
  54. handleError( StkError::WARNING ); return;
  55. }
  56. // By using an even value of the parameter M, we get a bipolar blit
  57. // waveform at half the blit frequency. Thus, we need to scale the
  58. // frequency value here by 0.5. (GPS, 2006).
  59. p_ = 0.5 * Stk::sampleRate() / frequency;
  60. rate_ = PI / p_;
  61. this->updateHarmonics();
  62. }
  63. void BlitSquare :: setHarmonics( unsigned int nHarmonics )
  64. {
  65. nHarmonics_ = nHarmonics;
  66. this->updateHarmonics();
  67. }
  68. void BlitSquare :: updateHarmonics( void )
  69. {
  70. // Make sure we end up with an even value of the parameter M here.
  71. if ( nHarmonics_ <= 0 ) {
  72. unsigned int maxHarmonics = (unsigned int) floor( 0.5 * p_ );
  73. m_ = 2 * (maxHarmonics + 1);
  74. }
  75. else
  76. m_ = 2 * (nHarmonics_ + 1);
  77. a_ = m_ / p_;
  78. }
  79. } // stk namespace