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.

The file lowpass_filter.v contains the low pass filter generated by Filter Design HDL Coder.

The file filter_tb_incisive.m contains the MATLAB test bench.

srcfile = fullfile(matlabroot,'toolbox','edalink','extensions','incisive','incisivedemos','Filter','lowpass_filter.v');

Create Project Directory

We create a temporary working directory in which the project will be compiled.

projdir = tempname;
warnstatus = warning('off','MATLAB:MKDIR:DirectoryExists');

Start the MATLAB Server

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

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

  • enable the clock

  • reset the module after 22 ns

  • drive a 10 ns clock

  • initialize the filter input to 0

tclcmd = {  ['cd ',projdir],...
            ['exec ncvlog ' srcfile],...
            'exec ncelab -access +wc lowpass_filter',...
            ['hdlsimmatlab -gui  lowpass_filter ', ...
             ' -input "{@matlabtb lowpass_filter 10ns -repeat 10ns -mfunc filter_tb_incisive}"',...
             ' -input "{@force lowpass_filter.clk_enable 1 -after 0ns}"',...
             ' -input "{@force lowpass_filter.reset 1 -after 0ns 0 -after 22ns}"',...
             ' -input "{@force lowpass_filter.clk 1 -after 0ns 0 -after 5ns -repeat 10ns}"',...
             ' -input "{@deposit lowpass_filter.filter_in 0}"',...

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.


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.

Was this topic helpful?