Converting a Model from Floating- to Fixed-Point Using Simulation Data

About This Example

This example steps you through using the Fixed-Point Advisor to prepare the fxpdemo_fpa model for conversion from using floating-point data types to using fixed-point data types. This example shows you how to:

  • Set model-wide configuration options.

  • Set block-specific parameters.

  • Obtain an initial fixed-point data types for the model.

  • Validate the fixed-point data types against the floating-point model.

Starting the Preparation

  1. Open the model. fxpdemo_fpafxpdemo_fpa.

  2. To start the conversion:

    1. Right-click Controller System and, from the subsystem context menu, select Fixed-Point Tool.

    2. On the Fixed-Point Tool Fixed-point preparation for selected system pane, click the Fixed-Point Advisor button.

    The Fixed-Point Advisor opens for the subsystem Controller System.

Preparing Model for Conversion

First, validate model-wide settings and create reference simulation data.

  1. For the purpose of this tutorial, run the tasks in the Fixed-Point Advisor Prepare Model for Conversion folder one at a time. In the left pane, select Verify model simulation settings and, in the right pane, click Run This Task.

    This task validates that model simulation settings allow signal logging and disable data type override to facilitate conversion to fixed point. These settings ensure that fixed-point data can be logged in downstream tasks.

    The task passes.

  2. Select and run Verify update diagram status.

    Your model must be able to successfully complete an update diagram action to run the checks in the Fixed-Point Advisor.

    The task passes.

  3. Select and run Address unsupported blocks.

    This task identifies blocks that do not support fixed-point data types. The Fixed-Point Advisor cannot convert these blocks. To complete the conversion of your model, replace these blocks with Simulink® built-in blocks that do support fixed-point data types. If a replacement block is not available, you can temporarily isolate the unsupported block with Data Type Conversion blocks.

    The task fails because the model contains a block that does not support fixed-point data types.

  4. Fix the failure by replacing the TrigFcn block with the provided replacement:

    1. Click the Preview link to view the replacement block.

    2. Click the link to the original block and view its settings.

    3. Double-click the replacement block and verify its settings match the settings of the original block.

        Note:   If the settings on the replacement block differ from the settings on the original block, set up the replacement block to match the original block.

    4. In the Controller System subsystem, right-click the original TrigFcn block. From the context menu, select Replace with Lookup Table.

      The Fixed-Point Advisor replaces the original block.

    5. In the Fixed-Point Advisor, rerun the task. The task passes.

  5. Select and run Set up signal logging. Because you are using simulation minimum and maximum data, you must specify at least one signal to use in analysis and comparison in downstream checks. At a minimum, you should log the unique input and output signals.

    The task runs and the Fixed-Point Advisor warns that signal logging is not specified for any signals.

  6. Because you want to propose data types based on simulation data, fix the warning:

    1. Click the Explore Result button.

    2. In the Model Advisor Result Explorer, select the signals that you want to log and select the EnableLogging check box.

      For this tutorial, log the signals connected to the Inport and Outport blocks:

      • Ctr_in

      • Ctr_out

    3. Close the Model Advisor Result Explorer.

    4. In the Fixed-Point Advisor, rerun the task.

      The task passes because signal logging is enabled for at least one signal.

  7. Select and run Create simulation reference data. The Fixed-Point Advisor simulates the model using the current solver settings, and creates and archives reference signal data to use for analysis and comparison in later conversion tasks.

    The task runs and the Fixed-Point Advisor warns that logging is not enabled.

    If the simulation is set up to have a long simulation time, after starting this task, you can stop the simulation by selecting the waitbar and then pressing Ctrl+C. This allows you to change the simulation time and continue without waiting for the long simulation.

  8. To fix the failure, in the Action box click Modify All.

    The Modify All action configures the model to the settings recommended in the Analysis Result. The Action Result box displays a table of changes.

      Note:   Prior to automatically fixing failures, you should review the Analysis Result box to ensure that you want to apply all the recommended actions.

  9. Click the Run This Task button.

    The task passes and the tool stores the results in a run named FPA_Reference. You can view these results in the Fixed-Point Tool Contents pane.

  10. Open the Verify Fixed-Point Conversion Guidelines folder. Select and run Check model configuration data validity diagnostic parameters settings. This task verifies that the Model Configuration Parameters > Diagnostics > Data Validity > Parameters options are all set to warning. If these options are set to error, the model update diagram action fails in later tasks.

    The task passes.

  11. Select and run Implement logic signals as Boolean data. This task verifies that Model Configuration Parameters > Optimization > Implement logic signals as Boolean data is selected. If it is cleared, the code generated in downstream checks is not optimized.

    The task passes.

  12. Select and run Check bus usage. This task identifies:

    • Mux blocks that are bus creators

    • Bus signals that the top-level model treats as vectors

      Note:   This is a Simulink check. For more information, see Check bus usage in the Simulink documentation.

    The task passes.

  13. Select and run Simulation range checking. This tasks verifies that the Model Configuration Parameters > Diagnostics > Data Validity > Simulation range checking option is not set to none. A warning is displayed because Simulation range checking is currently set to none. The recommended setting is warning so that warnings are generated when signals exceed the specified minimum or maximum values.

  14. Fix the warning by applying the recommended setting using the Modify All button. Rerun the task.

    The task passes.

  15. Select and run Check for implicit signal resolution. This task checks for models that use implicit signal resolution. To use the Fixed-Point Advisor for Simulink signal object scaling, turn off implicit signal resolution by setting the Diagnostics > Data Validity > Signal resolution property in the Configuration Parameters dialog box to Explicit only. Enforce resolution for each of the signals and states that currently resolve successfully. For more information, see Signal resolution in the Simulink documentation.

    The task passes because the model contains no Simulink signal objects.

The run all action has completed for the Prepare Model for Conversion folder. At this point, you can review the results report found at the folder level, or continue to the next folder.

Prepare for Data Typing and Scaling

The tasks in this folder prepare the model for automatic data typing by the Fixed-Point Tool. This folder contains tasks that set the block configuration options and output minimum and maximum values for blocks. The block settings from this task simplify the initial data typing and scaling. The optimal block configuration is achieved in later stages.

  1. Right-click Prepare for Data Typing and Scaling and select Run All.

    The Fixed-Point Advisor runs the Review locked data type settings task. This task identifies blocks that have their data type settings locked down, which excludes them from automatic data typing.

    The task passes because it finds no blocks with locked data types.

  2. The Fixed-Point Advisor runs the Remove output data type inheritance task. This task identifies blocks with the Output data type property set to Inherit. Inherited data types might lead to data type propagation errors.

    The task fails because some blocks in the model have inherited output data types.

  3. Fix the failure using the Modify All button to explicitly configure the output data types to the recommended values. Rerun the task.

    The task passes.

  4. Continue running to failure. Relax input data type settings runs. This task identifies blocks with input data type constraints that might lead to data type propagation errors.

    The task passes because all blocks have flexible input data types.

  5. Verify Stateflow charts that have strong data typing with Simulink runs. This task verifies that all Stateflow® charts are configured to have strong data typing with Simulink I/O.

    The task passes because the model does not have any Stateflow charts.

  6. Remove redundant specification between signal objects and blocks runs. This task identifies and removes redundant data type specification originating from blocks and Simulink signal objects.

    The task passes because the model contains no resolved Simulink signal objects.

  7. Verify hardware selection runs. This task identifies the hardware device information on the Hardware Implementation pane of the Configuration Parameters dialog box.

    The task fails because the Model Configuration Parameters > Hardware Implementation option does not provide values for the Device vendor and Device type parameters.

  8. Fix the failure:

    1. Click the Hardware Implementation Device settings link.

    2. In the Configuration Parameters dialog box Hardware Implementation pane, change:

      • Device vendor to Generic

      • Device type to 32-bit Embedded Processor

    3. Click OK to apply the settings.

  9. In the Fixed-Point Advisor window, rerun the task.

    The task fails because you must specify a default data type for floating-point data types that is suitable for the chosen hardware.

  10. Fix the failure by setting Default data type for all floating-point signals to int16.

    The software uses this default data type for all output signals. The Fixed-Point Advisor proposes the Same as embedded hardware integer setting, which is int32. However, use int16 because the model performs many multiplications and you want the product to fit into int32.

  11. Rerun the task.

    The task passes.

  12. Select and run Specify block minimum and maximum values.

    The Fixed-Point Advisor warns you that you have not specified any minimum and maximum values. Optimally, specify block output and parameter minimum and maximum values for, at minimum, the Inport blocks in the system. You can specify the minimum and maximum values for any block in this step. Typically, these values are determined during the design process based on the system that you are creating.

  13. Fix the warning by specifying minimum and maximum values for Inport blocks:

    1. Click the Explore Result button.

      The Model Advisor Result Explorer opens, displaying the Inport blocks that do not have an output minimum and maximum specified.

    2. On the Model Advisor Result Explorer center pane, select Ctr_in. For the purpose of this tutorial, specify the output minimum and maximum values for this block. Set OutMin to -5 and set OutMax to 5.

    3. Close the Model Advisor Result Explorer.

    4. In the Fixed-Point Advisor, rerun the task.

      The task passes because minimum and maximum values are specified for all Inport blocks.

    5. For the purpose of this tutorial, do not specify other minimum and maximum values for other blocks.

    6. Review the results report found at the folder level.

  14. Select and run Return to the Fixed-Point Tool to perform data typing and scaling.

  15. On the Fixed-Point Tool Contents pane, examine the results for the simulation reference run. One of the TrigFcn block outputs overflowed multiple times, indicating that the fixed-point settings on this block are not suitable for the input range. To refine the fixed-point data types, first run the model with a global override of the fixed-point data types using double-precision numbers to avoid quantization effects. This action provides a floating-point benchmark that represents the ideal output. Then, propose new data types based on these "ideal" results.

Propose Data Types Based on the Simulation Reference Run

Use the Fixed-Point Tool to propose fixed-point data types based on the simulation reference (FPA_Reference) run.

  1. In the Fixed-Point Tool:

    1. Click the Propose fraction lengths button .

    2. Because you are proposing data types based on fixed-point results, the tool issues a warning. In the warning dialog box, click Yes.

      The Fixed-Point Tool proposes new data types for objects in the model and updates the results on the Contents pane.

  2. In the Fixed-Point Tool, set the Column View to Automatic Data Typing with Simulation Min/Max View to display information relevant to the proposal. The tool displays the proposed scaling in the ProposedDT column in the Contents pane.

    To accommodate the full simulation range, the Fixed-Point Tool proposes new data types for some blocks in the model. Because the TrigFcn block is a linked library, the tool does not propose new data types for this block.

  3. Examine the results to resolve any conflicts and to ensure that you want to accept the proposed data type for each result.

    In the Fixed-Point Tool toolbar, select Show > Conflicts with proposed data types.

    The Fixed-Point Tool detects no conflicts, so you are ready to apply the new data types as described in Apply the New Fixed-Point Data Types.

Apply the New Fixed-Point Data Types

  1. Click Apply accepted fraction lengths to write the proposed data types to the model.

  2. In the Fixed-Point Tool toolbar, select Show > All results.

    The tool has set all the specified data types to the proposed types.

Simulate the Model Using New Fixed-Point Settings

  1. On the Shortcuts to set up runs pane, click the Model-wide no override and full instrumentation button to use the locally specified data type settings.

  2. On the Fixed-Point Tool Model Hierarchy pane, select the Controller Subsystem.

  3. Click Simulate to run the simulation.

    The Simulink software simulates the model using the new fixed-point settings that you applied in the previous step and stores the results in the NoOverride run.

  4. Examine the results. Because the tool did not propose new data types for the TrigFcn block, this block still overflows.

Was this topic helpful?