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.
Open the model and configure it for fixed-point conversion.
In this example, you convert the Controller Subsystem to fixed-point. 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')
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
In the Fixed-Point Tool, under System Under Design, select the
subsystem you want to convert to fixed point. In this example, select
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 to
Use default model inputs.
To specify tolerances for the system, under Signal Tolerances (Optional) in the table, specify tolerances for any signal in the model with signal logging enabled.
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 Converter 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
Click the Collect Ranges button to simulate the model. The Fixed-Point Tool overrides the
data types in the model with doubles and collects the minimum and maximum values for each
object in your model that occur during the simulation. The Fixed-Point Tool stores this
range information in a run titled
Ranges (Double). 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.
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
In the Convert Data Types section, 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 Data Types 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 because the proposed data type differs from the current data type of the object. If you apply data types, the tool applies the proposed data types to these results.
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. In this example, the
Output result will contain an underflow once the proposed data type is
applied. Underflows can be sources of numerical issues, but can sometimes be safely
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 subsystem under design.
Next, simulate the model again using the new fixed-point settings. You then use
Simulation Data Inspector plotting capabilities to compare the results from the
Ranges (Double) run with the fixed-point results.
In the Verify section of the toolstrip, 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
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 the icon to open Simulation Data Inspector. In the Simulation Data Inspector, select one of the logged signals to view the fixed-point simulation behavior.
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'
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. 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 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 to replace the decoupled
subsystem with a new subsystem containing the lookup table approximation for the
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 the Simulate with Embedded Types drop-down button.
Edit the Run name so that you do not overwrite the previous embedded run.
Click Simulate with Embedded Types.
The Fixed-Point Tool simulates the model, which now contains the lookup table approximation.
The Workflow Browser shows that the signals with specified tolerances pass in the model using the lookup table approximation.