Documentation Center

  • Trial Software
  • Product Updates

Debug a Fixed-Point Model

This example shows how to:

  • Identify which parts of a model cause numeric problems.

    The current fixed-point settings on this model cause overflows. You debug the model by overriding the fixed-point settings on one subsystem at a time and simulating the model to determine how these fixed-point settings affect the model behavior.

  • Create and use shortcuts to set up fixed-point instrumentation and data type override settings for different runs.

    To optimize the model for two different inputs, you switch several times between different data type override and fixed-point instrumentation settings. Using shortcuts facilitates changing these settings.

  • Autoscale the model over the complete simulation range for both inputs.

Simulating the Model to See the Initial Behavior

Initially, the input to the Gain block is a sine wave of amplitude 7. Simulate the model using local system settings with logging enabled to see if any overflows or saturations occur.

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

    addpath(fullfile(docroot,'toolbox','fixpoint','examples'))
    ex_fixedpoint_debug

  2. From the model Analysis menu, select Fixed-Point Tool.

  3. In the Fixed-Point Tool, set up a shortcut for the initial system settings:

    On the Shortcuts to set up runs pane, click Add/Edit shortcuts.

  4. In the Shortcut Editor:

    1. On the Model Hierarchy pane, select subsysA>Math1.

    2. In the Name of shortcut field, enter Setting A.

      The editor sets the Run name for this shortcut to Setting A.

    3. Set Fixed-point instrumentation mode to Minimums, maximums and overflows.

    4. Set Data type override to Use local settings.

    5. Click Apply.

    6. On the Model Hierarchy pane, select subsysA>Math2 and repeat steps (c) to (f).

    7. On the Manage shortcuts pane, under Shortcuts, select Setting A then click the right arrow to move this shortcut to the list of shortcuts displayed in the Fixed-Point Tool.

  5. Use this shortcut to set up a run. Use the settings to simulate the model.

    1. On the Fixed-Point Tool Model Hierarchy pane, select ex_fixedpoint_debug.

    2. On the Shortcuts to set up runs pane, click Setting A.

    3. Click the Simulate button,

      The Simulink® software simulates the model using the fixed-point instrumentation and data type settings specified in Setting A. Afterward, on the Contents pane, the Fixed-Point Tool displays the simulation results for each block that logged fixed-point data. The tool stores the results in the run named Setting A. The Fixed-Point tool highlights subsysB/Math2/Add1:Output in red to indicate that there is an issue with this result. The OverflowWraps column for this result shows that the block overflowed 51 times, which indicates a poor estimate for its scaling.

Debugging the Model

To debug the model, first simulate the model using local settings on the subsystem Math1 while overriding the fixed-point settings on Math2 with doubles. Simulating subsystem Math2 with doubles override avoids quantization effects for this subsystem. If overflows occur, you can deduce that there are issues with the fixed-point settings in subsystem Math1.

Next, simulate the model using local settings on Math2 and doubles override on Math1. If overflows occur for this simulation, there are problems with the fixed-point settings for subsystem Math2.

Setting Up Shortcuts

  1. Use the Shortcut Editor to create the following new shortcuts.

    Shortcut NameSubsystemFixed-point instrumentation modeData type overrideData type override applies to
    Setting BMath1MinMaxAndOverflowUse local settingsN/A
    Math2MinMaxAndOverflowDoubleAll numeric types
    Setting CMath1MinMaxAndOverflowDoubleAll numeric types
    Math2MinMaxAndOverflowUse local settingsN/A

  2. On the Manage shortcuts pane, add Setting B and Setting C to the list of buttons to display in the Fixed-Point Tool.

Testing Subsystem Math1 Settings

Simulate the model with original fixed-point settings on Math1 while overriding the fixed-point settings with doubles on Math2.

  1. On the Fixed-Point Tool Model Hierarchy pane, select ex_fixedpoint_debug.

  2. On the Shortcuts to set up runs pane, click Setting B to override fixed-point settings on Math2.

  3. Click the Simulate button.

    The Simulink software simulates the model using the fixed-point instrumentation and data type settings specified in Setting B, using fixed-point settings for Math1 and overriding the fixed-point settings for Math2. No overflows occur, which indicates that the settings on Math1 are not causing the overflows.

Testing Subsystem Math2 Settings

Simulate with original fixed-point settings on Math2 while overriding the fixed-point settings with doubles on Math1.

  1. On the Fixed-Point Tool Model Hierarchy pane, select ex_fixedpoint_debug.

  2. On the Shortcuts to set up runs pane, click Setting C to override the fixed-point settings on Math1.

  3. Click the Simulate button.

    The Simulink software simulates the model using the fixed-point instrumentation and data type settings specified in Setting C, using fixed-point settings for Math2 and overriding the fixed-point settings for Math1. Overflows occur in run Setting C, indicating that the settings on Math2 are causing the overflows.

Simulating the Model Using a Different Input Stimulus

Simulate the model with a different input using the original fixed-point settings on subsystems Math1 and Math2. Because you set up a shortcut for this initial set up, before rerunning the simulation, you can easily configure the model. Before simulating, select to merge the simulation results so that the tool gathers the simulation range for both inputs.

  1. On the Data collection pane, select Merge instrumentation results from multiple simulations.

  2. In the ex_fixedpoint_debug model, double-click the Manual Switch block to select Chirp Signal1 as the input to the Gain block.

  3. On the Fixed-Point Tool Model Hierarchy pane, select ex_fixedpoint_debug and simulate using the original fixed-point settings for Math1 and Math2.

    1. On the Shortcuts to set up runs pane, click Setting A.

    2. Click the Simulate button.

      The Simulink software simulates the model using the fixed-point instrumentation and data type settings specified in Setting A. Afterward, in the Contents pane, the Fixed-Point Tool displays the simulation results for each block that logged fixed-point data. The tool stores the results in the run named Setting A.

        Tip   In the Fixed-Point Tool Contents pane, click Run to sort the results in this column.

Debugging the Model with the New Input

  1. Simulate the model with original fixed-point settings on Math1 while overriding the fixed-point settings with doubles on Math2.

    1. On the Fixed-Point Tool Model Hierarchy pane, select ex_fixedpoint_debug.

    2. On the Shortcuts to set up runs pane, click Setting B.

    3. Click the Start button.

      No overflows occur, which indicates that the settings on Math1 are not causing the overflows.

  2. Simulate with original fixed-point settings on Math2 while overriding the fixed-point settings with doubles on Math1.

    1. On the Fixed-Point Tool Model Hierarchy pane, select ex_fixedpoint_debug.

    2. On the Shortcuts to set up runs pane, click Setting C.

    3. Click the Start button.

      Overflows occur, which indicates that the fixed-point settings on Math2 are causing the overflows. Next, use the Fixed-Point Tool to propose new data types for this subsystem.

Proposing Fraction Lengths for Math2 Based on Simulation Results

  1. On the Fixed-Point Tool Model Hierarchy pane, select Math2.

  2. On the Automatic data typing for selected system pane, click the Propose fraction lengths button.

  3. In the Propose Data Types dialog box, select Setting B as the run to use for proposing data types and click OK. This run simulated Math2 with double override to obtain the 'ideal' behavior of the subsystem based on the simulation results for both input stimuli.

    The Fixed-Point Tool proposes new fixed-point data types for the objects in subsystem Math2 to avoid numerical issues such as overflows.

  4. On the Contents pane ProposedDT column, examine the proposed data types for the objects in Math2. The tool proposed new fixed-point data types with reduced precision for the Add1 block Output and Accumulator.

  5. Because the Fixed-Point Tool marked all the proposed results with a green icon to indicate that the proposed data types pose no issues for these objects, accept the proposals.

    In the Automatic data typing for selected system pane, click the Apply accepted fraction lengths button.

Verifying the New Settings

Verify that the new settings do not cause any numerical problems by simulating the model using local settings for subsystems Math1 and Math2 and logging the results. Use shortcut Setting A that you set up for these settings.

  1. On the Fixed-Point Tool Model Hierarchy pane, select ex_fixedpoint_debug.

  2. On the Shortcuts to set up runs pane, click Setting A.

  3. On the Data collection pane, set Store results in run to Setting A2 and click Apply so that the Fixed-Point Tool does not overwrite the previous results for this shortcut.

  4. Click the Simulate button.

    The Simulink software simulates the model using the new fixed-point settings. Afterward, the Fixed-Point Tool displays the simulation results in run Setting A2. No overflows or saturations occur indicating that the model can now handle the full input range.

Was this topic helpful?