This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

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.

Set up the Model

Open the model and configure it for fixed-point conversion.

open_system('ex_fixed_point_workflow')

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')

Prepare System for Conversion

To convert the model to fixed point, use the Fixed-Point Tool.

  1. In the ex_fixed_point_workflow model menu, select Analysis > Data Type Design > Fixed-Point Tool.

  2. In the Fixed-Point Tool, in the Prepare System section of the toolstrip, under System Under Design, select the subsystem you want to convert to fixed point. In this example, select Controller Subsystem.

  3. In the Collect Ranges section, select Simulation Ranges as the range collection method.

  4. 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 Preparing 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 exp function.

Collect Ranges

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. For an example, see Debug a Fixed-Point Model.

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 Ranges (Double).

  1. To specify tolerances for the system, click the Signal Tolerances button. The drop-down has a table containing all signals in the model with signal logging enabled.

    Set the relative tolerance (Rel Tol) of the signals that you logged to 15%.

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

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

  4. 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 Sum1 : 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 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.

  5. 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 subsystem blocks.

Verify New Settings

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 floating-point Ranges (Double) run with the fixed-point results.

  1. 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 run titled Embedded.

    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.

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

  3. The run browser indicates that all signals for which you specified tolerances passed.

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

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.

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

  2. Open the Lookup Table Optimizer. In the model menu, select Analysis > Data Type Design > Lookup Table Optimizer.

  3. On the Objective page of the Lookup Table Optimizer, select Simulink Block. Click Next.

  4. Under Block Information, copy from the command line and paste the path to the subsystem created by the Fixed-Point Tool.

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

  6. Specify the constraints to use in the optimization. For this example, use the default values. To create the lookup table, click Optimize. Click Next.

  7. Click Replace Original Function to replace the decoupled subsystem with a new subsystem containing the lookup table approximation for the Math Function exp block.

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.

  1. In the Fixed-Point Tool, in the Verify section of the toolstrip, click the Simulate with Embedded Types drop-down button.

  2. Edit the Run name so that you do not overwrite the previous embedded run.

  3. Click Simulate with Embedded Types.

    The Fixed-Point Tool simulates the model, which now contains the lookup table approximation.

  4. The Run Browser shows that the signals with specified tolerances pass in the model using the lookup table approximation.

Related Topics