Next  |  Prev  |  Up  |  Top  |  Index  |  JOS Index  |  JOS Pubs  |  JOS Home  |  Search

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. 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)

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.

 
// 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()));
  }
}


Next  |  Prev  |  Up  |  Top  |  Index  |  JOS Index  |  JOS Pubs  |  JOS Home  |  Search

[How to cite this work]  [Order a printed hardcopy]  [Comment on this page via email]

``Introduction to Digital Filters with Audio Applications'', by Julius O. Smith III, (September 2007 Edition)
Copyright © 2024-09-03 by Julius O. Smith III
Center for Computer Research in Music and Acoustics (CCRMA),   Stanford University
CCRMA