Main Content

Visualize Differences Between Floating-Point and Fixed-Point Results

This example shows how to configure the MATLAB® Coder™ app to use a custom plot function to compare the behavior of the generated fixed-point code against the behavior of the original floating-point MATLAB code.

By default, when the Log inputs and outputs for comparison plots option is enabled, the conversion process uses a time series based plotting function to show the floating-point and fixed-point results and the difference between them. However, during fixed-point conversion you might want to visualize the numerical differences in a view that is more suitable for your application domain. This example shows how to customize plotting and produce scatter plots at the test numerics step of the fixed-point conversion.

Prerequisites

This example requires the following products:

Create a New Folder and Copy Relevant Files

  1. Create a local working folder, for example, c:\custom_plot.

  2. Change to the docroot\toolbox\fixpoint\examples folder. At the MATLAB command line, enter:

    cd(fullfile(docroot, 'toolbox', 'fixpoint', 'examples')) 

  3. Copy the myFilter.m, myFilterTest.m, plotDiff.m, and filterData.mat files to your local working folder.

    TypeNameDescription
    Function codemyFilter.mEntry-point MATLAB function
    Test filemyFilterTest.mMATLAB script that tests myFilter.m
    Plotting functionplotDiff.mCustom plot function
    MAT-filefilterData.matData to filter.

 The myFilter Function

 The myFilterTest File

 The plotDiff Function

Open the MATLAB Coder App

  1. Navigate to the folder that contains the files for this example.

  2. On the MATLAB Toolstrip Apps tab, under Code Generation, click the app icon.

Select Source Files

To add the entry-point function myFilter to the project, browse to the file myFilter.m, and then click Open.

By default, the app saves information and settings for this project in the current folder in a file named myFilter.prj.

Enable Fixed-Point Conversion

  1. Set Numeric Conversion to Convert to fixed point.

  2. Click Next to go to the Define Input Types step.

    The app screens myFilter.m for code violations and code generation readiness issues. The app does not find issues in myFilter.m.

Define Input Types

  1. On the Define Input Types page, to add myFilterTest as a test file, browse to myFilterTest.m, and then click Open.

  2. Click Autodefine Input Types.

    The app determines from the test file that the input type of in is complex(double(1x1)).

  3. Click Next to go to the Check for Run-Time Issues step.

Check for Run-Time Issues

The Check for Run-Time Issues step generates instrumented MEX. myFilter. It runs the test file myFilterTest replacing calls to myFilter with calls to the generated MEX. If the app finds issues, it provides warning and error messages. You can click a message to highlight the problematic code in a window where you can edit the code.

  1. Browse to the test file myFiltertest.m.

  2. Click Check for Issues.

    The app does not detect issues.

  3. Click Next to go to the Convert to Fixed Point step.

Convert to Fixed Point

  1. The app displays compiled information for variables in your code. For more information, see View and Modify Variable InformationView and Modify Variable Information (Fixed-Point Designer).

  2. To open the settings dialog box, click the Settings arrow .

    1. Verify that Default word length is set to 16.

    2. Under Advanced, set Signedness to Signed

    3. Under Plotting and Reporting, set Custom plot function to plotDiff.

  3. Click the Analyze arrow . Verify that the test file is myFilterTest.

  4. Click Analyze.

    The test file, myFilterTest, runs and the app displays simulation minimum and maximum ranges on the Variables tab. Using the simulation range data, the software proposes fixed-point types for each variable based on the default type proposal settings, and displays them in the Proposed Type column.

  5. To convert the floating-point algorithm to fixed point, click Convert.

    The software validates the proposed types and generates a fixed-point version of the entry-point function.

Test Numerics and View Comparison Plots

  1. Click Test arrow , select Log inputs and outputs for comparison plots, and then click Test.

    The app runs the test file that you used to define input types to test the fixed-point MATLAB code. Because you selected to log inputs and outputs for comparison plots and to use the custom plotting function, plotDiff.m, for these plots, the app uses this function to generate the comparison plot. The plot shows that the fixed-point results do not closely match the floating-point results.

  2. In the settings, increase the DefaultWordLength to 24 and then convert to fixed point again.

    The app converts myFilter.m to fixed point and proposes fixed-point data types using the new default word length.

  3. Run the test numerics step again.

    The increased word length improves the results. This time, the plot shows that the fixed-point results match the floating-point results.