Convert MATLAB Function Block to Fixed Point
This example shows how to use the Fixed-Point Tool to convert a model containing a MATLAB® Function block to fixed point.
Best Practices for Working with the MATLAB Function Block in the Fixed-Point Tool
Do not edit the fixed-point variant of your MATLAB Function block algorithm. Use the code view to edit the floating-point variant of your MATLAB code and re-propose and apply data types.
For a successful conversion, only use modeling constructs supported for automated fixed-point conversion. For a list of the supported modeling constructs, see MATLAB Language Features Supported for Automated Fixed-Point Conversion.
While collecting range information, do not edit the MATLAB code in the MATLAB Function block. Editing the code will cause problems if you try to merge results.
During the fixed-point conversion process using the Fixed-Point Tool, do not use the “Save as” option to save the MATLAB Function block with a different name. If you do, you might lose existing results for the original block.
Open the Model
Open the ex_symm_fir
model.
open_system("ex_symm_fir.slx")
The ex_symm_fir
model uses a symmetric FIR filter. Simulate the model and inspect the model output. Inspect the symmetric FIR filter algorithm by double-clicking the MATLAB Function block.
Prepare for Fixed-Point Conversion
To open the Fixed-Point Tool, in the Apps tab, expand the Apps gallery and select Fixed-Point Tool.
In the Fixed-Point Tool, select
Iterative Fixed-Point Conversion
.Under System Under Design (SUD), select the
symmetric_fir
subsystem, which contains the MATLAB Function block, as the system to convert.Under Range Collection Mode, select Simulation ranges as the method of range collection. This configures the model to collect ranges using idealized floating-point data types.
In the Prepare section of the toolstrip, click Prepare.
Collect Range Information
Collect idealized ranges to use for data type proposal. Expand the
Collect Ranges button arrow and select
Double precision
. Click Collect
Ranges to start the simulation.
The Fixed-Point Tool stores the simulation data in a run titled
BaselineRun
. Examine the range information of the MATLAB variables in the spreadsheet.
Propose Data Types
Configure the proposal settings and propose fixed-point data types for the model.
In the Convert section of the toolstrip you can configure the data type proposal settings for the MATLAB Function block variables.
In this example, use the default proposal settings.
Click Propose Data Types.
The data type proposals appear in the ProposedDT column of the spreadsheet.
Note
The SpecifiedDT column is always blank for MATLAB Function block variables.
Inspect Code and Propose Data Types
To inspect the code in the MATLAB Function block and view detailed variable and expression information, open the Instrumentation Report Viewer by double-clicking any variable in the Results spreadsheet.
To view and edit the current proposal settings, click the Settings drop-down.
For this example, the default fimath
properties are sufficient.
Apply Proposed Data Types
When you have finished examining the proposed types and editing proposal settings, apply the proposed data types to the model. You can apply the data types either from the code view, or from the Fixed-Point Tool.
In the Fixed-Point Tool click Apply Data Types.
A variant subsystem is now in the place of the MATLAB Function block. The variant subsystem contains both floating-point and fixed-point versions of the MATLAB Function block. The active version is automatically controlled by the Fixed-Point Tool based on the data type override settings of the model. Data Type Override is not currently active on the model, so the fixed-point version is active.
Verify Results
Return to the Fixed-Point Tool to verify the results of the conversion.
In the Verify section of the toolstrip, click the Simulate with Embedded Types button to simulate the model using the newly applied fixed-point data types. The model simulates with the fixed-point variant as the active variant.