DSPRelated.com
Free Books

Testing a Faust Filter Section

It takes a bit of experience to write a correct Faust program on the first try. Therefore, we often have to debug our programs by some technique. Typically, inspecting the automatically generated block diagrams and listening to the results are tools enough for debugging Faust source code. However, sometimes it is useful to verify the output signal(s) in more detail. For this purpose, Faust has a useful ``architecture file'' named plot.cpp which results in generation of a main C++ program that simply prints the output signal(s) to the standard output. This printout can be used to plot the output (using, e.g., gnuplot) or compare it to the output of some other program. This section gives an example of comparing the impulse response of the filter in Fig.K.1 to the output of a matlab version (Chapter 2). Specifically, we will compare the printed output from the Faust-generated program to the output of the matlab test program shown in Fig.K.5.

Figure K.5: Constant Peak-Gain Resonator--matlab version

 
SR = 44100; % Sampling rate

fr = 1000;  % Resonant frequency
bw = 100;   % Bandwidth
g  = 1;     % Peak gain
N = 10;     % Samples to generate in test

R = exp(-pi*bw/SR); % pole radius
A = 2*pi*fr/SR;     % pole angle (radians)
firpart = g * [1 0 -1] * (1-R^2)/2;
feedback = [1 -2*R*cos(A) R^2]; % freq-domain coeffs
freqz(firpart,feedback);        % freq-response display
format long;
h = impz(firpart,feedback,N)    % print impulse response

In our Faust program, we need a test impulse, e.g.,

  process = 1-1' : firpart : + ~ feedback
  with { ... <same as before> ... };
The signal $ \texttt{1}=[1,1,1,\ldots]$ is the unit-step signal consisting of all ones, and $ \texttt{1'}=[0,1,1,\ldots]$ is the unit step delayed by one sample. Therefore, 1-1' is the impulse signal $ \delta = [1,0,0,\ldots]$.

Suppose the file cpgrir.dsp (``Constant-Peak-Gain Resonator Impulse-Response'') contains our test Faust program. Then we can generate the impulse-response printout as follows at the command line:

  > faust -a plot.cpp -o cpgrir-print.cpp cpgrir.dsp
  > g++ -Wall -g -lm -lpthread cpgrir-print.cpp -o cpgrir-print
  > cpgrir-print -n 10
The first line generates the C++ program cpgrir.cpp from the Faust source file cpgrir.dsp using the architecture file plot.cpp. The second line compiles the C++ file to produce the executable program cpgrir-print. Finally, the third line generates and prints the first 10 samples of the output signal (anything more than the number of filter coefficients is usually enough), which is our desired impulse response:K.7
   h = [    0.00707331  0.0139039   0.013284
            0.012405    0.0112882   0.00995947
            0.00844865  0.00678877  0.00501544
            0.00316602      ... ]
The matlab version produces the following impulse response:
h =
 [ 0.00707328459864603 0.01390382707778288 0.01328399389241600
   0.01240496991806334 0.01128815312793390 0.00995943544693653
   0.00844861689634155 0.00678874919376101 0.00501542304704597
   0.00316601431505539 ... ]
Since matlab uses double-precision floating-point while Faust only supports single-precision floats, we will normally see differences after six or so decimal digits.


Next Section:
A Look at the Generated C++ code
Previous Section:
Generating Faust Block Diagrams