DSPRelated.com
Free Books

Software Implementation in C++

Figure 3.3 gives a C++ program for implementing the same filter using the Synthesis Tool Kit (STK). The example writes a sound file containing white-noise followed by filtered-noise from the example filter.

Figure: C++ main program for computing filtered noise using the example digital filter. The Synthesis Tool Kit (STK), version 4.2, supplies the Noise and Filter classes used.

 
/* main.cpp - filtered white noise example */

#include "Noise.h"  /* Synthesis Tool Kit (STK) class */
#include "Filter.h" /* STK class */
#include "FileWvOut.h"  /* STK class */
#include <cmath>   /* for pow() */
#include <vector>

int main()
{
  Noise *theNoise = new Noise(); // Noise source

  /* Set up the filter */
  StkFloat bCoefficients[5] = {1,0,0,pow(0.5,3)};
  std::vector<StkFloat> b(bCoefficients, bCoefficients+5);
  StkFloat aCoefficients[7] = {1,0,0,0,0,pow(0.9,5)};
  std::vector<StkFloat> a(aCoefficients, aCoefficients+7);
  Filter *theFilter = new Filter;
  theFilter->setNumerator(b);
  theFilter->setDenominator(a);

  FileWvOut output("main");  /* write to main.wav */

  /* Generate one second of white noise */
  StkFloat amp = 0.1; // noise amplitude
  for (long i=0;i<SRATE;i++)   {
    output.tick(amp*theNoise->tick());
  }

  /* Generate filtered noise for comparison */
  for (long i=0;i<SRATE;i++)   {
    output.tick(theFilter->tick(amp*theNoise->tick()));
  }
}


Next Section:
Software Implementation in Faust
Previous Section:
Software Implementation in Matlab