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 walk-through of these steps, watch the video:

This video shows how to use the fixed point tool and the lookup table optimizer to convert a floating point model to fixed point. This floating point model consists of a source, a controller subsystem to convert to used fixed point data types, and a scope to visualize the controller outputs. Configuring the model in this way helps you to determine the effects of fixed point data types on the system under design.
You can modify the source inputs to collect simulation data for multiple stimuli, then examine the behavior of the subsystem and scale the fixed point data types accordingly. To compare the behavior of the controller subsystem before and after conversion, enable signal logging at the outputs. Save the model. Open the fixed point tool from the app's gallery. The fixed-point tool provides several workflows for fixed-point conversion.
For this example, select iterative fixed-point conversion. Use the setup pane to give the tool information about your system and specifications. Select the controller subsystem as the system under design. Select simulation ranges to tell the tool to collect range data for model objects using logged minimum and maximum values that occur during the simulation. You can optionally use a simulation input object to specify additional simulation inputs.
For this example, use the default. You can specify tolerances for any signal in the model with signal logging enabled. Set the relative tolerance of these signals to 15%. Next, prepare the system for fixed-point conversion. The tool checks the system for compatibility with the conversion process and reports any issues found. The tool indicates that the model has passed all preparation checks.
When possible, the tool changes settings that are not compatible with conversion. The unsupported constructs check indicates the model contains a block that does not support fixed-point data types. Click the link to inspect how the tool has changed your model. The controller subsystem contains an exponential block, which does not support fixed-point data types. The tool isolates this block from the system under design by surrounding it with data type conversion blocks and places it inside a subsystem.
Next, collect range data. Select double precision to override data types in the model with doubles during the range collection step. Start the range collection simulation. The tool overrides the data types in the model with doubles and collects the minimum and maximum values that occur for each object in your model during the simulation.
The tool stores the collected range information in a run named baseline run. You can view the collected ranges in the simulation minimum and simulation maximum columns. The histograms provide a visual summary for each object in your model. Select the row in the table to view more details. The Explore tab provides additional options for sorting and filtering the data.
Propose fixed-point data types based on the collected range data. The tool analyzes the scaling of all blocks whose output data type settings are not locked against changes by fixed-point tools. The tool proposes fixed-point data types with 16-bit word length and best precision fraction length by default. Review the results to determine whether the proposed data types meet your specifications. By default, the tool accepts the results for each object whose proposed data type differs from the current data type.
If the proposed data types do not meet your specifications, you can change the settings and propose new data types. When you are satisfied with the proposed data types and the tool indicates that there are no issues, apply the proposed data types to your model. To verify the new fixed-point data types simulate the model again. The tool stores the information collected during simulation in a new run called embedded run.
The tool displays information about blocks that logged fixed-point data. The compiled data type column for the embedded run shows that the controller subsystem block uses the new fixed-point data types. Verify that there are no overflows or under flows. The icon shows that all signals with specified tolerances passed. Use the simulation data inspector to view the fixed-point simulation behavior and specify tolerances for logged signals in your model.
In the preparation step, the exponent block was isolated. You can now replace the exponential block with a lookup table approximation. First, get a list of all the subsystems that were decoupled for conversion. This function returns the block path of all subsystems created by the fixed-point tool to isolate unsupported blocks.
Open the lookup table optimizer. Create a LookUp Table for a Simulink block. Copy and paste the path to the exponential subsystem you want to replace with a lookup table approximation. Next, update the model diagram and gather information needed for the optimization process. Specify constraints to use for the lookup table optimization.
For this example, accept the default values. Optimize the lookup table approximation. Replace the exponential subsystem with the lookup table approximation. The app replaces the exponential subsystem with a new variant subsystem containing the lookup table approximation. Now that the design is fully converted, verify that it still meets your specified tolerances.
The tool simulates the model, which now contains the lookup table approximation and saves the result as embeddedrun_2. The green checkmark indicates that the signals still meet the specified tolerances.
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.