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. Assuming the STK is installed in the usual place (as by Planet CCRMA), the following Makefile will compile and link it on a typical Linux system:
STK_LIB = -lstk INCLUDE = -I/usr/include/stk filterNoise: filterNoise.cpp g++ $(INCLUDE) filterNoise.cpp -o filterNoise $(STK_LIB)
// filterNoise.cpp - filtered white noise example // Tested with STK 4.4.2 #include "Noise.h" // Synthesis Tool Kit (STK) class #include "Iir.h" // STK class #include "FileWvOut.h" // STK class #include <cmath> // for pow() #include <vector> using namespace stk; 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); Iir *filter = new Iir; filter->setNumerator(b); filter->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(filter->tick(amp*theNoise->tick())); } } |