| Simulink® Fixed Point™ | ![]() |
| On this page… |
|---|
The tutorial that follows shows you how to use the Fixed-Point Tool to refine the scaling of fixed-point data types associated with a feedback controller model (see Introduction to the Tutorial). Although the tool enables multiple workflows for converting a digital controller described in ideal double-precision numbers to one realized in fixed-point numbers, this tutorial demonstrates the following approach:
Initial Guess at Scaling. Run an initial "proof of concept" simulation using a reasonable guess at the fixed-point word size and scaling. This task illustrates how difficult it is to guess the best scaling.
Data Type Override. Perform a global override of the fixed-point data types using double-precision numbers. The Simulink® software logs the simulation results to the MATLAB® workspace, and the Fixed-Point Tool displays them.
Automatic Scaling. Perform the automatic scaling procedure, which uses the double-precision simulation results to propose fixed-point scaling for appropriately configured blocks. The Fixed-Point Tool allows you to accept and apply the scaling proposals selectively. Afterward, you determine the quality of the results by examining the input and output of the model's analog plant.
To begin, open both the fxpdemo_feedback model and the Fixed-Point Tool.
Initial guesses for the scaling of each block are already specified in each block mask in the model. This task is included only to illustrate the difficulty of guessing the best scaling.
In the Fixed-Point Tool, click the Start button
.
The Simulink software simulates the fxpdemo_feedback model. Afterward, the Fixed-Point Tool displays in its Contents pane the simulation results for each block that logged fixed-point data. The tool stores the results as an active run, denoted by the Active label in the Run column. The OvfSat column reveals that the Up Cast block saturated 23 times, which indicates a poor guess for its scaling.

In the Contents pane of the Fixed-Point
Tool, select the Transfer Fcn block named Analog Plant and
then click the Time Series Plot button
.
The Fixed-Point Tool plots the signal associated with the plant output.

The preceding plot of the plant output signal reflects the initial guess at scaling. The Bode plot design sought to produce a well-behaved linear response for the closed-loop system. Clearly, the response is nonlinear. Significant quantization effects cause the nonlinear features. An important part of fixed-point design is finding a scaling that reduces quantization effects to acceptable levels.
Data type override mode enables you to perform a global override of the fixed-point data types and scaling using double-precision numbers, thereby avoiding quantization effects. The Fixed-Point Tool will use these simulation results when performing automatic scaling to propose higher fidelity fixed-point scaling.
In the Dialog pane of the Fixed-Point Tool:
Click the Exchange Active and Reference results button. The tool swaps the results that it stores as an active run with those that it stores as a reference run. The existing results are now stored as a reference run, denoted by the Reference label in the Run column, and the active run is empty. This prevents the tool from overwriting the previous set of results.
Ensure that the Logging mode parameter for the fxpdemo_feedback system specifies Minimums, maximums and overflows. This option enables logging for all the model's blocks that can log such data.
Specify the Data type override parameter for the fxpdemo_feedback system as True doubles. This option overrides all local data type settings for the model's subsystems.
The Fixed-Point Tool appears as follows.

In the Fixed-Point Tool, click the Start button
.
The Simulink software simulates the fxpdemo_feedback model in data type override mode and stores the results as an active run. Afterward, the Fixed-Point Tool displays in its Contents pane the active run results along with those of the reference run that you generated previously (see Initial Guess at Scaling). The SimDT (simulation data type) column for the active run shows that the model's blocks used a double data type during simulation.

In the Contents pane of the Fixed-Point
Tool, select the Transfer Fcn block named Analog Plant that
corresponds with either the active or the reference run, and then
click the Time Series Difference (A-R) Plot button
.
The Fixed-Point Tool plots both the active and reference versions of the signal associated with the plant output (upper axes), and plots the difference between the active and reference versions of that signal (lower axes). Compare the ideal (double data type) plant output signal with its fixed-point version.

Using the automatic scaling procedure, you can easily maximize the precision of the output data type while spanning the full simulation range. For a complex model, the absence of such a procedure can make achieving this goal tedious and time consuming.
Note The Fixed-Point Tool yields meaningful autoscaling results when the maximum and minimum simulation values cover the full intended operating range of your design. See the autofixexp reference page for more information. |
Perform automatic scaling for the Controller block. This block is a subsystem that represents software running on the target, and it requires optimization.
In the Model Hierarchy pane of the Fixed-Point Tool, select the Controller subsystem. In the Dialog pane, specify the Percent safety margin parameter as 20.
The Percent safety margin parameter value multiplies the "raw" simulation values by a factor of 1.2. Setting this parameter to a value greater than 1 decreases the likelihood that an overflow will occur when fixed-point data types are being used.
Because of the nonlinear effects of quantization, a fixed-point simulation will produce results that are different from an idealized, doubles-based simulation. Signals in a fixed-point simulation can cover a larger or smaller range than in a doubles-based simulation. If the range increases enough, overflows or saturations could occur. A safety margin decreases the likelihood of this happening, but it might also decrease the precision of the simulation.
In the Fixed-Point Tool, click the Propose
fraction lengths button
.
The Fixed-Point Tool analyzes the scaling of all fixed-point blocks whose:
Lock output scaling against changes by the autoscaling tool parameter is not selected.
Output data type parameter specifies a generalized fixed-point number.
The Fixed-Point Tool uses the minimum and maximum values stored in the active run to propose each block's scaling such that the precision is maximized while the full range of simulation values is spanned. The tool displays the proposed scaling in its Contents pane as shown here:

Review the scaling that the Fixed-Point Tool proposes. You can choose to accept the scaling proposal for each block by selecting the corresponding Accept check box in the Contents pane. By default, the Fixed-Point Tool accepts all scaling proposals that differ from the current scaling. For this example, ensure that the Accept check box associated with the active run is selected for each of the Controller subsystem's blocks.
In the Fixed-Point Tool, click the Apply
accepted fraction lengths button
.
The Fixed-Point Tool applies to the Controller subsystem's blocks the scaling proposals that you accepted in the previous step.
In the Model Hierarchy pane of the Fixed-Point Tool, select the fxpdemo_feedback system. In the Dialog pane:
Click the Exchange Active and Reference results button. The tool swaps the results that it stores as an active run with those that it stores as a reference run. The simulation results corresponding to True doubles override are now stored as a reference run. This prevents the tool from overwriting the results that we want to retain.
Specify the Data type override parameter as Use local settings. This option enables each of the model's subsystems to use its locally specified data type settings.
In the Fixed-Point Tool, click the Start button
.
The Simulink software simulates the fxpdemo_feedback model using the new scaling that you applied in step 4. Afterward, the Fixed-Point Tool displays in its Contents pane information about blocks that logged fixed-point data. The SimDT (simulation data type) column for the active run shows that the Controller subsystem's blocks used fixed-point data types with the new scaling.

In the Model Hierarchy pane of
the Fixed-Point Tool, select the fxpdemo_feedback system.
In the Contents pane, select the Transfer Fcn
block named Analog Plant that corresponds with
either the active or the reference run, and then click the Time
Series Difference (A-R) Plot button
.
The Fixed-Point Tool plots the active and reference versions of the plant output signal, as well as their difference. Compare the ideal plant output signal (of data type double) in the reference run with its fixed-point version in the active run.

Optionally, you can zoom in to view the steady-state region with greater detail. From the Tools menu of the figure window, select Zoom In and then drag the cursor to draw a box around the area you want to view more closely. For instance, the following figure shows a close-up view of a portion of the previous plot.

Note that the plant output signal represented by the active run achieves a steady state, but a small limit cycle is present because of poor A/D design.
![]() | Introduction to the Tutorial | Tutorial: Producing Lookup Table Data | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |