Main Content

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.

Once a snapshot is ready, we start a Cadence® Xcelium® simulator session. (You must have the Xcelium 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.

Start the MATLAB Server

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


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 xmsim through the hdlsimmatlab Tcl command. All commands preceded with -input are passed to xmsim and are executed in the xmsim Tcl shell. These commands will :

  • compile the verilog filter

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

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

  • 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 = {  ['exec xmvlog -64bit lowpass_filter.v'],...
             'exec xmelab -64bit -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 Xcelium® 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 xmsim 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.