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 .
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 .
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 .
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 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 . 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.