Main Content

Visualize Differences Between Floating-Point and Fixed-Point Results

To complete this example, you must install the following products:

Inspect Example Files

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

Set Up Configuration Object

  1. Create a coder.FixptConfig object.

    fxptcfg = coder.config('fixpt');
  2. Specify the test file name and custom plot function name. Enable logging and numerics testing.

    fxptcfg.TestBenchName = 'myFilterTest';
    fxptcfg.PlotFunction = 'plotDiff';
    fxptcfg.TestNumerics = true; 
    fxptcfg.LogIOForComparisonPlotting = true;
    fxptcfg.DefaultWordLength = 16;
    

Convert to Fixed Point

Convert the floating-point MATLAB function, myFilter, to fixed-point MATLAB code. You do not need to specify input types for the codegen command because it infers the types from the test file.

codegen -args {complex(0, 0)} -float2fixed fxptcfg myFilter

The conversion process generates fixed-point code using a default word length of 16 and then runs a fixed-point simulation by running the myFilterTest.m function and calling the fixed-point version of myFilter.m.

Because you selected to log inputs and outputs for comparison plots and to use the custom plotting function, plotDiff.m, for these plots, the conversion process uses this function to generate the comparison plot.

The plot shows that the fixed-point results do not closely match the floating-point results.

Increase the word length to 24 and then convert to fixed point again.

fxptcfg.DefaultWordLength = 24;
codegen -args {complex(0, 0)} -float2fixed fxptcfg myFilter

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