Propose Fixed-Point Data Types Based on Simulation Ranges
This example shows how to propose fixed-point data types based on simulation range data using the MATLAB® Coder™ app.
This example requires the following products:
Create a New Folder and Copy Relevant Files
Create a local working folder, for example,
Change to the
docroot\toolbox\fixpoint\examplesfolder. At the MATLAB command line, enter:
cd(fullfile(docroot, 'toolbox', 'fixpoint', 'examples'))
ex_2ndOrder_filter_test.mfiles to your local working folder.
Type Name Description Function code
Entry-point MATLAB function Test file
MATLAB script that tests
Open the MATLAB Coder App
Navigate to the work folder that contains the file for this example.
On the MATLAB Toolstrip Apps tab, under Code Generation, click the app icon.
Select Source Files
To add the entry-point function
the project, browse to the file
and then click Open. By default, the app saves
information and settings for this project in the current folder in
a file named
Enable Fixed-Point Conversion
Set Numeric Conversion to
Convert to fixed point.
Click Next to go to the Define Input Types step.
The app screens
ex_2ndOrder_filter.mfor code violations and code generation readiness issues. The app does not find issues in
Define Input Types
On the Define Input Types page, to add
ex_2ndOrder_filter_testas a test file, browse to
ex_2ndOrder_filter_test, and then click Open.
Click Autodefine Input Types.
The test file runs and displays the outputs of the filter for each of the input signals.
The app determines from the test file that the input type of
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. It runs the test file
ex_2ndOrder_filter with calls to the
generated MEX function. 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.
On the Check for Run-Time Issues page, the app populates the test file field with
ex_2ndOrder_filter_test, the test file that you used to define the input types.
Click Check for Issues.
The app does not detect issues.
Click Next to go to the Convert to Fixed Point step.
Convert to Fixed Point
The app displays compiled information—type, size, and complexity—for variables in your code. See View and Modify Variable Information.
On the Function Replacements tab, the app displays functions that are not supported for fixed-point conversion. See Running a Simulation.
Click the Analyze arrow . Verify that Analyze ranges using simulation is selected and that the test bench file is
ex_2ndOrder_filter_test. You can add test files and select to run more than one test file during the simulation. If you run multiple test files, the app merges the simulation results.
Select Log data for histogram.
By default, the Show code coverage option is selected. This option provides code coverage information that helps you verify that your test file is testing your algorithm over the intended operating range.
The simulation runs and the app displays a color-coded code coverage bar to the left of the MATLAB code. Review this information to verify that the test file is testing the algorithm adequately. The dark green line to the left of the code indicates that the code runs every time the algorithm executes. The orange bar indicates that the code next to it executes only once. This behavior is expected for this example because the code initializes a persistent variable. If your test file does not cover all of your code, update the test or add more test files.
If a value has
...next to it, the value is rounded. Pause over the
...to view the actual value.
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. The app enables the Convert option.
You can manually enter static ranges. These manually entered ranges take precedence over simulation ranges. The app uses the manually entered ranges to propose data types. You can also modify and lock the proposed type.
Examine the proposed types and verify that they cover the full simulation range. To view logged histogram data for a variable, click its Proposed Type field.
To modify the proposed data types, either enter the required type into the Proposed Type field or use the histogram controls. For more information about the histogram, see Log Data for Histogram.
To convert the floating-point algorithm to fixed point, click Convert.
During the fixed-point conversion process, the software validates the proposed types and generates the following files in the
codegen\ex_2ndOrder_filter\fixptfolder in your local working folder.
ex_2ndOrder_filter_fixpt.m— the fixed-point version of
ex_2ndOrder_filter_wrapper_fixpt.m— this file converts the floating-point data values supplied by the test file to the fixed-point types determined for the inputs during conversion. These fixed-point values are fed into the converted fixed-point design,
ex_2ndOrder_filter_fixpt_report.html— this report shows the generated fixed-point code and the fixed-point instrumentation results.
ex_2ndOrder_filter_report.html— this report shows the original algorithm and the fixed-point instrumentation results.
ex_2ndOrder_filter_fixpt_args.mat— MAT-file containing a structure for the input arguments, a structure for the output arguments and the name of the fixed-point file.
If errors or warnings occur during validation, you see them on the Output tab. See Validating Types.
In the Output Files list, select
ex_2ndOrder_filter_fixpt.m. The app displays the generated fixed-point code.
Click the Test arrow . Select Log inputs and outputs for comparison plots, and then click Test.
To test the fixed-point MATLAB code, the app runs the test file that you used to define input types. Optionally, you can add test files and select to run more than one test file to test numerics. The software runs both a floating-point and a fixed-point simulation and then calculates the errors for the output variable
y. Because you selected to log inputs and outputs for comparison plots, the app generates a plot for each input and output. The app docks these plots in a single figure window.
The app also reports error information on the Verification Output tab. The maximum error is less than 0.03%. For this example, this margin of error is acceptable.
If the difference is not acceptable, modify the fixed-point data types or your original algorithm. For more information, see Testing Numerics.
On the Verification Output tab, the app provides a link to a report that shows the generated fixed-point code and the proposed type information.
Click Next to go to the Generate Code page.
Generate Fixed-Point C Code
In the Generate dialog box, set Build source to
Fixed-Pointand Build type to
Set Language to C.
Click Generate to generate a library using the default project settings.
MATLAB Coder builds the project and generates a C static library and supporting files in the default subfolder,
The app displays the generated code for
ex_2ndOrder_filter.c. In the generated C code, variables are assigned fixed-point data types.
Click Next to go to the Finish Workflow page.
On the Finish Workflow page, the app displays a project summary and links to generated output files.