Documentation

Debug a Fixed-Point Model

This example shows how to use the Data Type Visualizer to identify which parts of a model are causing numeric problems.

Using the Visualizer, you can view a summary of histograms of the bits used by each object in your model. Each column in the data type visualization represents a histogram for one object in your model. Each bin in a histogram corresponds to a bit in the binary word.

Selecting a column highlights the corresponding model object in the spreadsheet of the Fixed-Point Tool, and populates the Result Details pane with more detailed information about the selected result.

You can use the data type visualization to see a summary of the ranges of objects in your model and to spot sources of overflows, underflows, and inefficient data types. To view the data type visualization, simulate a system with fixed-point instrumentation or signal logging turned on. Overflows are marked with a red triangle above the column representing the model object. Underflows are marked with a yellow triangle.

The current fixed-point settings on this example model cause overflows. You debug the model by enabling fixed-point instrumentation and simulating the model, then examining the Vizualization of Simulation Data pane to determine which model objects affect the model behavior.

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 Data Type Design > Fixed-Point Tool.

  3. In the Prepare System section of the toolstrip, under System under design, select ex_fixedpoint_debug as the system you want to convert.

  4. In the Collect Ranges section of the toolstrip, select Simulation Ranges as the method for range collection.

  5. Click the Collect Ranges button.

    Simulink® simulates the model while overriding the currently specified data types with doubles. Afterward, 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 Ranges (Double). The Fixed-Point Tool Visualization of Simulation Data pane highlights subsysB/Math2/Add1:Output with a red triangle to indicate that the result would overflow with the currently specified type. The Result Details pane for this result shows the same histogram of simulation data for the selected result.

Simulating the Model Using a Different Input Stimulus

Simulate the model with a different input. Before simulating, select to merge the simulation results so that the tool gathers the simulation range for both inputs.

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

  2. In the Collect Ranges section of the toolstrip, click the Simulation Ranges button arrow and select Merge min/max.

  3. Click the Collect Ranges button.

    Simulink simulates the model using double-precision data types. Afterward, the Fixed-Point Tool displays the simulation results for each block that logged fixed-point data. The tool stores the merged results in the run named Ranges (Double).

    The subsysB/Math2/Add1:Output is still the only result which contains an overflow. You can view more detail about the result in the Result Details pane.

Proposing Fraction Lengths Based on Simulation Results

  1. In the Convert Data Types section of the toolstrip, click Propose Data Types.

    The Fixed-Point Tool proposes new fixed-point data types for the objects in the model to avoid numerical issues such as overflows. The Vizualization of Simulation Data pane updates to show the effect that the proposed data types would have on the model. The subsysB/Math2/Add1:Output result no longer contains any overflows.

  2. Sometimes, to avoid overflows while obeying the specified proposal settings, the Fixed-Point Tool must reduce the precision of a result. In some cases this can cause underflows.

    For example, edit the ProposedDT field of the subsysA/Math1/Gainresult to fixdt(1,16,2). The histograms in the Vizualization of Simulation Data pane update and now show an underflow, marked by a yellow triangle.

    Change the proposed data type for the result back to the scaling originally proposed by the tool, fixdt(1,16,16).

  3. 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, apply the proposals.

    Click the Apply Data Types button.

Verifying the New Settings

Verify that the new settings do not cause any numerical problems by simulating the model using the new fixed-point scalings and logging the results.

Click the Simulate with Embedded Types button.

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

Related Topics

Was this topic helpful?