Documentation

Convert Floating-Point Model to Fixed Point

In this example, learn how to:

  • Convert a floating-point system to an equivalent fixed-point representation.

    This example shows the recommended workflow for conversion when proposing fraction lengths based on simulation data.

  • Use the Fixed-Point Tool to propose fixed-point data types.

    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 covers the range. The tool allows you to review the data type proposals and then apply them selectively to objects in your model.

  • Handle floating-point inheritance blocks during conversion.

    For floating-point inheritance blocks when inputs are floating point, all internal and output data types are floating point. The model in this example uses a Discrete Filter block, which is a floating-point inheritance block.

Open the Model

Open the ex_fixed_point_workflow model. At the MATLAB® command line, enter:

addpath(fullfile(docroot,'toolbox','fixpoint','examples'));
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.

Prepare System for Conversion

  1. To compare the floating-point behavior of the model with the fixed-point behavior after conversion, you must enable signal logging for at least one signal. In the Controller Subsystem, select the signals at the outputs of the Discrete Filter and Gain blocks.

    Click the Simulation Data Inspector button arrow and select Log Selected Signals.

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

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

  4. Optionally, use the Fixed-Point Advisor to prepare your model. The Fixed-Point Advisor analyzes your model and makes configuration recommendations for autoscaling by the Fixed-Point Tool. To launch the Fixed-Point Advisor, click the Fixed-Point Advisor button . For more information, see Prepare Floating-Point Model for Conversion to Fixed Point Using Fixed-Point Advisor.

Collect Ranges

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

  2. Click the Collect Ranges button to start the simulation. 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. For an example on how to use the visualization, 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. 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.

  2. 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 object's current data type. If you apply data types, the tool will apply the proposed data types to these results.

  3. 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 on the result icon. In this example, no results contain conflicts. For more information, see Examine Results to Resolve Conflicts.

Now that you have reviewed the results and ensured that there are no issues, you are ready to apply the proposed data types to the model.

  1. Click the Apply Data Types button 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 the Simulate with Embedded Types button . 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. In the spreadsheet, select the row corresponding to the Gain block, and then click the Compare Results button.

    The Simulation Data Inspector plots the signal for the Ranges (Double) and Embedded runs, as well as their differences. The difference plot shows that the difference between the floating-point signal and the fixed-point signal is on the order of 10^-3.

Was this topic helpful?