Convert Floating-Point Model to Fixed Point
In this example, learn how to:
Convert a floating-point system to an equivalent fixed-point representation.
The Fixed-Point Tool automates the task of specifying fixed-point data types in a system. In this example, the tool collects range data for model objects, either from design minimum and maximum values that you specify explicitly for signals and parameters, or from logged minimum and maximum values that occur during simulation. Based on these values, the tool proposes fixed-point data types that maximize precision and cover the range. The tool allows you to review the data type proposals and then apply them selectively to objects in your model.
Replace blocks that are not supported for conversion with a lookup table approximation.
During the preparation stage of the conversion, the Fixed-Point Tool isolates any blocks that do not support fixed-point conversion by placing these blocks inside a subsystem surrounded by Data Type Conversion blocks. You can use the Lookup Table Optimizer to replace the unsupported blocks with a lookup table approximation.
For a visual walk-through of these steps, watch the video:
Set up the Model
Open the model and configure it for fixed-point conversion.
open_system('ex_fixed_point_workflow')
The model consists of a source, a Controller Subsystem that you want to convert to fixed point, and a scope to visualize the subsystem outputs. Configuring a model in this way helps you to determine the effect of fixed-point data types on a system. Using this approach, you convert only the subsystem because this is the system of interest. There is no need to convert the source or scope to fixed point.
This configuration allows you to modify the inputs and collect simulation data for multiple stimuli. You can then examine the behavior of the subsystem with different input ranges and scale your fixed-point data types to provide maximum precision while accommodating the full simulation range.
To compare the behavior before and after conversion, enable signal logging at the outputs of the system under design.
ph = get_param('ex_fixed_point_workflow/Controller Subsystem','PortHandles'); set_param(ph.Outport(1),'DataLogging','on') set_param(ph.Outport(2),'DataLogging','on')
Prepare System for Conversion
To convert the model to fixed point, use the Fixed-Point Tool.
In the Apps gallery of the
ex_fixed_point_workflow
model, select Fixed-Point Tool.In the Fixed-Point Tool, under New workflow, select
Iterative Fixed-Point Conversion
.Under System Under Design (SUD), select the subsystem you want to convert to fixed point. In this example, select
Controller Subsystem
.Under Range Collection Mode, select Simulation Ranges as the range collection method.
Under Simulation Inputs, you can specify
Simulink.SimulationInput
objects to exercise your design over its full operating range. In this example, set Simulation inputs toUse default model inputs
.To specify tolerances for the system, in the table under Signal Tolerances, specify tolerances for any signal in the model with signal logging enabled. For more information, see Specify Signal Tolerances.
Set the relative tolerance (Rel Tol) of the signals that you logged to 15%.
In the toolstrip, click Prepare. The Fixed-Point Tool checks the system under design for compatibility with the conversion process and reports any issues found in the model. When possible, the Fixed-Point Tool automatically changes settings that are not compatible. For more information, see Use the Fixed-Point Tool to Prepare a System for Conversion.
The subsystem under design contains an Exp block, which does not support fixed-point data types. The Fixed-Point Tool surrounds this block with Data Type Conversion blocks and places it inside a subsystem. When you finish converting the rest of the subsystem to fixed point, you can replace the subsystem with a lookup table approximation of the
exp
function.
Collect Ranges
By default, the Fixed-Point Tool uses the current data type override set on the model. For this example, override data types in the model with double precision for the range collection run.
Expand the Collect Ranges button arrow and select Double precision.
Click Collect Ranges to simulate the model.
BaselineRun
. You can view the collected ranges in the
SimMin and SimMax columns of the spreadsheet, or
in the Result Details pane.The Visualization of Simulation Data pane offers another view of the simulation results. Select the Explore tab of the Fixed-Point Tool for additional tools for sorting and filtering the data in the spreadsheet and the visualization.
Convert Data Types
Use the Fixed-Point Tool to propose fixed-point data types for appropriately configured
blocks based on the double-precision simulation results stored in the run
BaselineRun
.
In the Convert section of the toolstrip, click the Propose Data Types button.
The Fixed-Point Tool analyzes the scaling of all fixed-point blocks whose Lock output data type setting against changes by the fixed-point tools parameter is not selected.
The Fixed-Point Tool uses the default proposal settings to propose data types with 16-bit word length and best-precision fraction length and updates the results in the spreadsheet.
You can edit the proposal settings by clicking the Settings button in the Convert section of the toolstrip before proposing types.
The tool displays the proposed data types in the ProposedDT column in the spreadsheet.
By default, it selects the Accept check box for each result where the proposed data type differs from the current data type of the object. If you apply data types, the tool applies these proposed data types to the system under design.
Examine the results to resolve any issues and to ensure that you want to accept the proposed data type for each result. The Visualization of Simulation Data pane indicates results that would contain overflows or underflows with a red or yellow triangle, respectively. Underflows can be sources of numerical issues, but can sometimes be safely ignored.
The Fixed-Point Tool indicates results whose proposed data type conflicts with another type with a red icon . In this example, no results contain conflicts. For more information, see Examine Results to Resolve Conflicts.
After reviewing the results and ensuring that there are no issues, you are ready to apply the proposed data types to the model. Click Apply Data Types to write the proposed data types to the model.
The Fixed-Point Tool applies the data type proposals to the blocks in the system under design.
Verify New Settings
Next, simulate the model again using the new fixed-point settings. You then use the
Simulation Data Inspector plotting capabilities to compare the results from the
floating-point BaselineRun
run with the fixed-point results.
Click Simulate with Embedded Types. The Fixed-Point Tool simulates the model using the new fixed-point data types and stores the run information in a new run titled
EmbeddedRun
.Afterward, the Fixed-Point Tool displays information about blocks that logged fixed-point data. The CompiledDT column for the run shows that the Controller Subsystem blocks use the new fixed-point data types.
Examine the histograms in the Visualization of Simulation Data pane to verify that there are no overflows or saturations. Overflows and saturations are marked with a red triangle .
The workflow browser indicates that all signals for which you specified tolerances passed.
Click Compare Results to open the Simulation Data Inspector. In the Simulation Data Inspector, select one of the logged signals to view the fixed-point simulation behavior.
Replace Unsupported Blocks with a Lookup Table Approximation
In the Prepare System for Conversion step of the workflow, the Fixed-Point Tool placed the Exp block, which is not supported for conversion, inside a subsystem surrounded with Data Type Conversion blocks. In this step, you replace the subsystem with a lookup table approximation.
To get a list of all of the subsystems the Fixed-Point Tool decoupled for conversion, at the command line enter:
decoupled = DataTypeWorkflow.findDecoupledSubsystems('ex_fixed_point_workflow')
decoupled = 1×2 table ID BlockPath __ ____________________________________________________ 1 {'ex_fixed_point_workflow/Controller Subsystem/Exp'}
The
DataTypeWorkflow.findDecoupledSubsystems
function returns a table containing the block path of any subsystems that were created by the Fixed-Point Tool to isolate an unsupported block.Open the Lookup Table Optimizer. In the Apps gallery, select Lookup Table Optimizer.
On the Objective page of the Lookup Table Optimizer, select Simulink block or subsystem. Click Next.
Under Block Information, copy from the command line and paste the path to the subsystem created by the Fixed-Point Tool.
Click the Collect Current Values from Model button to update the model diagram and allow the Lookup Table Optimizer to automatically gather information needed for the optimization process. Click Next.
Specify the constraints to use in the optimization. For this example, use the default values. To create the lookup table, click Optimize. Click Next.
Click Replace Original Function. The Lookup Table Optimizer replaces the Math Function
exp
block with a new variant subsystem containing the lookup table approximation.
Verify Behavior of System with Lookup Table Approximation
Now that the system under design is fully converted, verify that the system still meets the tolerances you specified before conversion.
In the Fixed-Point Tool, in the Verify section of the toolstrip, click Simulate with Embedded Types.
The Fixed-Point Tool simulates the model, which now contains the lookup table approximation, and saves the result as
EmbeddedRun_2
.The Workflow Browser shows that the signals with specified tolerances pass in the model using the lookup table approximation.