Filter Frames of a Noisy Sine Wave Signal using Testbench Generator

This example shows how to use the Streaming Testbench Generator app to generate DSP algorithm testbenches. The DSP algorithm generated in this example is similar to the algorithm in the Filter Frames of a Noisy Sine Wave Signal in MATLAB example. That example filters a noisy sine wave signal using a FIR lowpass filter and displays the power spectrum using a spectrum analyzer.

Streaming Testbench Generator Example App

The Streaming Testbench Generator app helps you develop and test streaming signal processing algorithms by enabling you to quickly generate testbenches. To launch the Testbench Generator, enter testbenchGeneratorExampleApp at the MATLAB command prompt. The command launches an interface through which you can:

  1. Select a set of sources and sinks.

  2. Enter the function name of your custom User Algorithm.

  3. Customize the properties of each of the added sources and sinks.

Each source is treated as a separate input to your algorithm, but you can associate more than one sink with the same output from your algorithm.


Inputs - Sine Waves and White Noise

By default, the testbench generator selects a two-channel sine wave source and a white Gaussian noise source. The two channels of the sine wave source have frequencies of 1 kHz and 10 kHz. The sampling frequency is 44.1 kHz. The white Gaussian noise input has mean 0 and standard deviation 0.1. The data is processed in frames of 1024 samples. To add more sources, use the list under Add a new source to the above list of inputs to select one of the supported sources. Alternatively, you can add your custom System object source by selecting Custom System object from the list and clicking Add. The added source appears in the list of inputs.

After adding a source, you can select it and click Configure to change the selected source's properties.

User Algorithm - Lowpass Filter

The default user algorithm dspStreamingPassthrough is a generic function that just passes through the inputs to the outputs. The user algorithm used in this example is a more meaningful function hTestbenchLowpass. You can view the code for this function by entering

edit hTestbenchLowpass

at the MATLAB command prompt. hTestbenchLowpass accepts two inputs, lowpass filters the sum of those two inputs, and returns the filtered signal. It uses a constrained equiripple FIR filter design with a cutoff frequency of 5 kHz. The ripples in the passband and stopband are equal to 0.05 and 0.001. Filtering is performed using dsp.FIRFilter, which is optimized for streaming.

Type hTestbenchLowpass in the User Algorithm text box replacing the default dspStreamingPassthrough. Alternatively, you can bring up a new testbench generator session by entering testbenchGeneratorExampleApp('hTestbenchLowpass') at the MATLAB command prompt.


The power spectrum of the output is displayed on a spectrum analyzer in dBm. You can add more sinks to visualize or post-process the outputs. Similar to inputs, you can use the list under Add a new sink to the above list of outputs to add a new sink, and click Configure to modify the properties of the selected sink.

You can associate a single output from the user algorithm with one or more sinks. For example, you can visualize the same output signal with both a time scope and spectrum analyzer. To do this, add the required sinks and make sure you associate all of the sinks to desired output from the user algorithm by changing the value under the Associate selected sink with list.

Generate Code and Simulate

After you add and configure the sources and sinks and enter a function name in the User Algorithm text box, the testbench generator is ready to generate testbench MATLAB code. To generate code, click on the Generate MATLAB Code button. A new untitled document opens in the MATLAB editor containing the generated testbench code.

You can edit the generated code to customize it before executing it. For the default example, the generated code is included below. Executing this testbench code, you see in the spectrum analyzer that the frequencies above 4 kHz in the source signal are attenuated. The resulting signal maintains the peak at 1 kHz because 1 kHz falls in the passband of the lowpass filter.

Streaming testbench script Generated by Streaming Testbench Generator

% Initialization
numIterations = 10000;

% Construct sources (for all inputs)
src1 = dsp.SineWave('Frequency',[1000 10000], ...
    'SampleRate',44100, ...

% Construct sinks (for all outputs)
sink1 = dsp.SpectrumAnalyzer('SampleRate',44100, ...
    'PlotAsTwoSidedSpectrum',false, ...

% Stream processing loop
clear hTestbenchLowpass;
for i = 1:numIterations
    % Sources
    in1 = src1();
    in2 = 0.1*randn(1024,2);

    % User Algorithm
    out1 = hTestbenchLowpass(in1,in2);

    % Sinks

% Clean up

More Customizations in Testbench Generator

The testbench generator offers additional top-level customizations, which you can configure using the Testbench Generator Settings dialog box. To open this dialog box, select Settings > Testbench Generator Settings ....

You can also tune some of the parameters used in your algorithm during testbench execution. To use the Parameter Tuning UI, check the Enable parameter tuning check box under the User Algorithm and click Edit parameters table to add the details of your tunable parameters before you generate testbench code. Also, make sure that your user algorithm handles parameter tuning during execution. See the MATLAB code for hTestbenchVariableBandwithFIR for an example of how to make your user algorithm work with parameter tuning.