Testing a Filter Component in MATLAB®

This example shows how MATLAB® can be used as a test bench for an HDL component. We compile an HDL low pass filter and then test it using matlabtb.

A temporary directory is used for the compiled and elaborated HDL files. Once a snapshot is ready, we start a Cadence® Incisive® simulator session. (You must have the Incisive simulator executables on your PATH.) We use a shared memory connection between MATLAB the HDL simulator, so both must be on the same computer.

In this example, a Verilog low pass filter has been designed and generated with the MathWorks™ Filter Design HDL Coder™ product and our job is to test it by comparing it to the MATLAB filter. We will not be running the filter designer in this example. The filter has a sample time of 10 ns (a sample rate of 100 MHz) with a passband Fpass = 20 MHz and a stopband Fstop = 25 MHz. The Verilog filter has a delay of two samples.

Create Project Directory

Start the MATLAB Server

We start the MATLAB server, hdldaemon, such that it uses shared memory communication.

hdldaemon;

HDLDaemon shared memory server is running with 0 connections

Specify Tcl Commands

Next we specify the Tcl commands to execute in the HDL simulator before the simulation is run. The following lists of commands will execute in a Tcl shell. The commands will compile and elaborate the project and then launch ncsim through the hdlsimmatlab Tcl command. All commands preceded with -input are passed to ncsim and are executed in the ncsim Tcl shell. These commands will :

change to the working directory

compile the verilog filter

elaborate the filter and turn on read write access to the ports

start ncsim with MATLAB test bench support by calling hdlsimmatlab, remaining commands are executed in ncsim

schedule the MATLAB function filter_tb_incisive to be called every 10 ns

Start the Incisive® Simulator

Now we start the HDL simulator via the nclaunch command. The 'tclstart' property causes the specified Tcl commands to be run at startup. Once the HDL simulator is launched, begin the simulation using the run command in the ncsim console, specifying the appropriate simulation time. For example type run 100000.

nclaunch('tclstart',tclcmd);

At this point a MATLAB figure with two subplots will open. The upper plot shows the spectrum of the input signal. The lower plot shows the spectrum of the HDL filter output overlayed with the spectrum of the MATLAB filter output. You will have to zoom in to see the difference between the output spectrums. The running mean and maximum of the absolute error in the time domain is also shown below the plots. The figure will be updated every 1024 samples.

This concludes this example.

Be sure to quit the HDL simulator once you are done with this example as each time the example is run, a new simulator session is started. Also keep in mind that this example created some temporary files in a temporary directory.

Open Example

A modified version of this example exists on your system. Do you want to open this version instead?