Products & Services Solutions Academia Support User Community Company

Tutorial: Converting a Model from Floating- to Fixed-Point

About This Tutorial

This tutorial steps you through using the Fixed-Point Advisor to convert fxpdemo_fpa from using floating-point data types to using fixed-point data types. This tutorial shows you how to use the Fixed-Point Advisor to:

For more information about a task, see the Fixed-Point Advisor Reference.

Starting the Fixed-Point Advisor

  1. Open the model: fxpdemo_fpa.mdl.

  2. To start the Fixed-Point Advisor, right-click Controller System and select Fixed-Point > Fixed-Point Advisor from the subsystem context menu.

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

Prepare Model for Conversion

This folder contains tasks for validating model-wide settings and creating reference simulation data.

  1. In the Task Hierarchy pane, right-click the Prepare Model for Conversion folder and select Run to Failure from the folder context menu.

  2. The first task, Verify model simulation settings runs. This task validates that the model Fixed-point instrumentation mode and Data type override options are consistent with fixed-point conversion goals, ensuring that fixed-point data can be logged in downstream tasks.

    A waitbar appears while the task runs. When the run is complete, the result shows the task failed. Information about the failure is displayed in the Analysis Result box.

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

      Caution   You should review the Analysis Result box prior to automatically fixing failures to ensure that you want to apply all of the recommended actions.

  4. Click Run This Task.

    Running the task after using the Modify All action verifies that you made the necessary changes. The Analysis Result box updates to display a passed result. When a task passes, the Analysis Result box displays information about why the task passes.

  5. Right-click Verify update diagram status and select Continue from the task context menu.

    The Fixed-Point Advisor continues running the tasks in the folder from where you left off.

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

    The task passes.

  6. Address unsupported blocks runs. This task identifies blocks that do not support fixed-point data types and therefore cannot be converted. To complete the conversion of your model, you must 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 has blocks that do not support fixed-point data types.

  7. Fix the failure:

    1. Replace the TrigFcn block with the Sine block following the instructions in the Analysis Result box.

    2. Rerun the task. The task passes.

    3. Continue the run to failure.

  8. Set up signal logging runs. You must specify at least one signal for the Fixed-Point Advisor to use for analysis and comparison in downstream checks. You should log, at minimum, the unique input and output signals.

    The task fails because signal logging is not specified for any signals.

  9. Fix the failure using the Model Advisor Result Explorer:

    1. Click the Explore Result button.

      The Model Advisor Result Explorer opens.

    2. Select the signals you want to log and select the EnableLogging check box.

        Tip  

        • The menus at the top of the Model Advisor Result Explorer allow you to change the information displayed in the center pane.

        • You can edit properties in the Model Advisor Result Explorer using any of the techniques described in Changing Property Values in the Simulink documentation. This includes selecting and changing multiple properties at one time.

        • When you update a property value in the center pane, the Signal Properties (right) pane updates to contain the same information.

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

      • Ctr_in

      • Ctr_out

    3. In the Fixed-Point Advisor window, click Run This Task.

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

  10. For the purpose of this tutorial, run the remainder of the tasks in the Prepare Model for Conversion folder one at a time. 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 downstream checks.

      Tip   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 having to wait for the long simulation to complete.

    The task passes.

  11. Open the Verify Fixed-Point Conversion Guidelines folder. Select and run Check model configuration data validity diagnostic parameters settings. This task verifies that the 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 downstream checks.

    The task passes.

  12. Select and run Implement logic signals as Boolean data. This task verifies that 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.

  13. Select and run Check for proper bus usage. This task identifies:

    • Mux blocks that are bus creators

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

    The task passes.

  14. Select and run Simulation range checking. This tasks verifies that the Configuration Parameters > Diagnostics > 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.

  15. Fix the warning by applying the recommended setting using the Modify All button and rerun the task.

    The task passes.

  16. 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 and 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 to failure 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

This folder contains tasks that set the block configuration options and set output minimum and maximum values for blocks. The block settings from this task simplify the initial scaling. The optimal block configuration is achieved in later stages. The tasks in this folder prepare the model for scaling in Perform Data Typing and Scaling.

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

    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.

  2. Fix the failure using the Modify All button to explicitly configure the output data types to the recommended values, and rerun the task.

    The task passes.

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

  4. Continue running to failure. 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.

  5. Continue running to failure. 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.

  6. Continue running to failure. Verify hardware selection runs. This task identifies the hardware device information in the Hardware Implementation pane of the Configuration Parameters dialog box.

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

  7. Fix the failure:

    1. Click the Hardware Implementation Device settings link.

      The Configuration Parameters dialog box opens.

    2. In the Configuration Parameters dialog box change:

      • Device vendor to Generic

      • Device type to 32-bit Embedded Processor

    3. Click OK to apply the settings.

      The changes are applied and the Configuration Parameters dialog box closes.

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

    The task passes and displays the current device type and vendor.

  9. For the purpose of this tutorial, run the remainder of the tasks in the Prepare for Data Typing and Scaling folder one at a time. Select and run Specify block minimum and maximum values. Ideally, you should 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, they are determined during the design process based on the system you are creating.

    The Fixed-Point Advisor warns you that you have not specified any minimum and maximum values.

  10. 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. In the center pane, select Ctr_in. For the purpose of this tutorial, you want to specify the output minimum and maximum values for this block. Set OutMin to -5 and set OutMax to 5.

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

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

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

  11. Select and run Summarize blocks with locked scaling. This task summarizes blocks that currently have their scaling locked down, which excludes them from autoscaling.

    The task passes because it finds no blocks with locked scaling.

The run to failure action has completed for the Prepare for Data Typing and Scaling folder. At this point, you can review the results report found at the folder level, or continue to the next folder.

Perform Data Typing and Scaling

This folder contains tasks that assist you in specifying data type information and proposing initial scaling for all blocks.

How the Fixed-Point Advisor Computes Recommended Data Types and Scaling

When generating scaling proposals, the Fixed-Point Advisor uses the following types of range data for model objects:

The Fixed-Point Advisor computes recommended data types and scaling based on the following rules and rationales:

For more information, see Propose data type and scaling in the Fixed-Point Advisor Reference.

Performing Data Typing and Scaling

  1. For the purpose of this tutorial, you run the tasks in the Perform Data Typing and Scaling folder one at a time.

    The Fixed-Point Advisor uses:

    • The type in the Top-level Inport data type field with range information to recommend scaling for Inport blocks

    • The type in the Constant data type field with range information to recommend scaling for Constant blocks

    • The type in the All other data types field with range information to recommend scaling for all other blocks

    Specify int16 in the Top-level Inport data type, Constant data type, or All other data types fields. Select and run Propose data type and scaling. This task provides fixed-point data type and scaling for all scalable blocks.

    The Fixed-Point Advisor warns you that some blocks do not have fixed-point data type scaling. The Analysis Result box displays the recommended fixed-point data type scaling for these blocks. You can apply the recommendations by clicking the Modify All button, or use the Model Advisor Result Explorer dialog to manually enter fixed-point data type scaling.

  2. Apply the recommendations using the Modify All button and rerun the task.

    The task passes.

  3. Select and run Check for numerical errors. This task summarizes simulation numerical errors.

    The task passes because there are no numerical errors.

  4. Select and run Analyze logged signals. This task summarizes the differences between current simulation data and the reference data that you archived in a previous task.

    The task passes. You can view the differences by clicking the link to the Fixed-Point Tool in the Analysis Result box. For convenience, you can view limited results by clicking the View Results link.

  5. Select and run Summarize data types. This task summarizes the data types used in the system. The Analysis Result box displays the number of:

    • Floating-point data types

    • Integer word lengths less than or equal to the native word size

    • Integer word lengths greater than the native word size

    The task passes because all signals are fixed-point data types.

You have completed the tasks in the Perform Data Typing and Scaling folder. At this point, you can review the results report found at the folder level, or continue to the next folder.

Prepare for Code Generation

This folder contains tasks that assist you in identifying settings that might lead to nonoptimal results in code generation.

  1. For the purpose of this tutorial, you run the tasks in the Prepare for Code Generation folder one at a time.

    Select and run Disable signal logging. This task disables unnecessary signal logging to avoid declaring extra signal memory in generated code.

    The result is a warning because there are signals that are logged.

  2. Fix the warning using the Modify All button to disable signal logging on all signals and rerun the task.

    The task passes.

  3. Select and run Identify blocks that generate expensive saturation and rounding code. This task optimizes the code to eliminate unnecessary saturation and rounding.

    The result is a warning because there are settings that can result in nonoptimized code.

  4. Fix the failure conditions by following the instructions in the Analysis Result box. Rerun the task.

    The task passes.

  5. Select and run Identify questionable fixed-point operations. This task identifies fixed-point operations that can lead to nonoptimal results.

    The task results in a warning because your model is not set up for optimal code generation.

  6. View the recommend actions in the Analysis Result box to see the types of conditions causing the warning.

You have completed the tasks in the Prepare for Code Generation folder. This completes the tutorial. At this point, you can review the results report found at the folder level.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS