Main Content

This example shows you how to use the Fixed-Point Tool to refine the scaling of fixed-point data types associated with the feedback controller model. 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 example uses the following approach:

Range Collection — Use the range collection workflow to explore the numerical behavior of the model.

Perform a global override of the fixed-point data types using double-precision numbers. The Simulink

^{®}software logs the simulation results and the Fixed-Point Tool displays them.Run an initial simulation using a reasonable guess at the fixed-point word size and scaling, then compare the simulation results to the double-precision run. This task illustrates how difficult it is to guess the best scaling.

Propose Fraction Lengths Using Simulation Range Data — Use the iterative fixed-point conversion workflow to autoscale the model.

The Fixed-Point Tool uses 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 open the Simulink feedback design model for this tutorial, at the MATLAB^{®} command line, type `fxpdemo_feedback`

.

The model consists of the following blocks and subsystems:

**Reference**This Signal Generator block generates a continuous-time reference signal. It is configured to output a square wave.

**Sum**This Add block subtracts the plant output from the reference signal.

**ZOH**The Zero-Order Hold block samples and holds the continuous signal. This block is configured so that it quantizes the signal in time by 0.01 seconds.

**Analog to Digital Interface**The analog to digital (A/D) interface consists of a Data Type Conversion block that converts a

`double`

to a fixed-point data type. It represents any hardware that digitizes the amplitude of the analog input signal. In the real world; its characteristics are fixed.**Controller**The digital controller is a subsystem that represents the software running on the hardware target. Refer to Digital Controller Realization.

**Digital to Analog Interface**The digital to analog (D/A) interface consists of a Data Type Conversion block that converts a fixed-point data type into a

`double`

. It represents any hardware that converts a digitized signal into an analog signal. In the real world, its characteristics are fixed.**Analog Plant**The analog plant is described by a transfer function, and is controlled by the digital controller. In the real world, its characteristics are fixed.

**Scope**The model includes a Scope block that displays the plant output signal.

To set up this kind of fixed-point feedback controller simulation:

Identify all design components.

In the real world, there are design components with fixed characteristics (the hardware) and design components with characteristics that you can change (the software). In this feedback design, the main hardware components are the A/D hardware, the D/A hardware, and the analog plant. The main software component is the digital controller.

Develop a theoretical model of the plant and controller.

For the feedback design in this tutorial, the plant is characterized by a transfer function.

The digital controller model in this tutorial is described by a

*z*-domain transfer function and is implemented using a direct-form realization.Evaluate the behavior of the plant and controller.

You evaluate the behavior of the plant and the controller with a Bode plot. This evaluation is idealized, because all numbers, operations, and states are double-precision.

Simulate the system.

You simulate the feedback controller design using Simulink and Fixed-Point Designer™ software. In a simulation environment, you can treat all components (software

*and*hardware) as though their characteristics are not fixed.

Open loop (controller and plant) and plant-only Bode plots for the “Scaling a Fixed-Point Control Design” model are shown in the following figure. The open loop Bode plot results from a digital controller described in the idealized world of continuous time, double-precision coefficients, storage of states, and math operations.

The Bode plots were created using workspace variables produced by a script named
`preload_feedback.m`

.

In this simulation, the digital controller is implemented using the fixed-point direct form realization shown in the following diagram. The hardware target is a 16-bit processor. Variables and coefficients are generally represented using 16 bits, especially if these quantities are stored in ROM or global RAM. Use of 32-bit numbers is limited to temporary variables that exist briefly in CPU registers or in a stack.

The digital controller realization consists of these blocks:

**Up Cast**Up Cast is a Data Type Conversion block that connects the A/D hardware with the digital controller. It pads the output word size of the A/D hardware with trailing zeros to a 16-bit number (the base data type).

**Numerator Terms**and**Denominator Terms**Each of these Discrete FIR Filter blocks represents a weighted sum carried out in the CPU target. The word size and precision in the calculations reflect those of the accumulator. Numerator Terms multiplies and accumulates the most recent inputs with the FIR numerator coefficients. Denominator Terms multiples and accumulates the most recent delayed outputs with the FIR denominator coefficients. The coefficients are stored in ROM using the base data type. The most recent inputs are stored in global RAM using the base data type.

**Combine Terms**Combine Terms is a Add block that represents the accumulator in the CPU. Its word size and precision are twice that of the RAM (double bits).

**Down Cast**Down Cast is a Data Type Conversion block that represents taking the number from the CPU and storing it in RAM. The word size and precision are reduced to half that of the accumulator when converted back to the base data type.

**Prev Out**Prev Out is a Unit Delay block that delays the feedback signal in memory by one sample period. The signals are stored in global RAM using the base data type.

**Direct Form Realization. **The controller directly implements this equation:

$$y\left(k\right)={\displaystyle \sum _{i=0}^{N}{b}_{i}u\left(k-1\right)-{\displaystyle \sum _{i=1}^{N}{a}_{i}}}y\left(k-1\right),$$

where:

*u*(*k*– 1) represents the input from the previous time step.*y*(*k*) represents the current output, and*y*(*k*– 1) represents the output from the previous time step.*b*represents the FIR numerator coefficients._{i}*a*represents the FIR denominator coefficients._{i}

The first summation in *y*(*k*) represents the multiplication and accumulation of the most recent
inputs and numerator coefficients in the accumulator. The second summation in *y*(*k*) represents the multiplication and accumulation of the most recent
outputs and denominator coefficients in the accumulator. Because the FIR coefficients,
inputs, and outputs are all represented by 16-bit numbers (the base data type), any multiplication involving these numbers produces a 32-bit output (the
accumulator data type).

Initial guesses for the scaling of each block are already specified in each block mask in the model. This task illustrates the difficulty of guessing the best fixed-point scaling. In this example, you compare the behavior of the model with an idealized floating-point version using the range collection workflow in the Fixed-Point Tool.

Open the

`fxpdemo_feedback`

model.Open the Fixed-Point Tool. In the

**Apps**gallery, select**Fixed-Point Tool**.In the Fixed-Point Tool, click

**New**, and select`Range Collection`

.You can use the range collection workflow to explore the numerical behavior of your model, and compare it to an idealized, floating-point version.

Under

**System Under Design (SUD)**, select the subsystem you want to analyze. In this example, select`Controller`

.Under

**Range Collection Mode**, select**Simulation Ranges**as the range collection method.Under

**Simulation Inputs**, use the default model inputs for simulation.Click the

**Collect Ranges**button arrow and select`Double precision`

. Click the**Collect Ranges**button to start the simulation.The Simulink software simulates the

`fxpdemo_feedback`

model in data type override mode and stores the results in`BaselineRun`

. Data type override enables you to perform a global override of the fixed-point data types with double-precision data types, thereby avoiding quantization effects. In the**Results**spreadsheet, the Fixed-Point Tool displays the run results. The compiled data type (**CompiledDT**) column for`BaselineRun`

shows that the blocks in the model used a`double`

data type during simulation.Next, simulate the system using the fixed-point data types specified in the model. Click the

**Settings**button arrow and select**Specified data types**. Click**Simulate with Embedded Types**.The Fixed-Point Tool simulates the model using the currently specified fixed-point data types and stores the range information in

`EmbeddedRun`

. You can view the collected ranges in the**SimMin**and**SimMax**columns of the spreadsheet.The Fixed-Point Tool highlights the row containing the

`Up Cast`

block to indicate that there is an issue with this result. The**Result Details**pane shows that the block saturated 23 times, which indicates a poor guess for its scaling.**Tip**You can use the

**Explore**tab to explore and filter results.Click

**Compare Results**to open the Simulation Data Inspector.In the Simulation Data Inspector, select

`PlantOutput`

as the signal to compare.Simulation Data Inspector plots the signal associated with the plant output for the

`BaselineRun`

and the`EmbeddedRun`

.The plot of the plant output signal for

`EmbeddedRun`

reflects the initial guess at scaling. The Bode plot design sought to produce a well-behaved linear response for the closed-loop system, represented by the ideal`BaselineRun`

. However, the response of the`EmbeddedRun`

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.

Using automatic data typing, you can maximize the precision of the output data type while spanning the full simulation range. The iterative fixed-point conversion workflow in the Fixed-Point Tool lets you maximize the precision of the output data types while spanning the full simulation range. This process is known as autoscaling.

Because no design range information is supplied in this example, the Fixed-Point Tool
uses simulation range data for proposing data types. The **Safety margin for
simulation min/max (%)** parameter value multiplies the “raw”
simulation values. 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. For more information
about how the Fixed-Point Tool calculates data type proposals, see How the Fixed-Point Tool Proposes Data Types.

Because of the nonlinear effects of quantization, a fixed-point simulation produces 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 this likelihood, but it might also decrease the precision of the simulation.

**Note**

When the maximum and minimum simulation values cover the full, intended operating range of your design, the Fixed-Point Tool yields meaningful automatic data typing results.

Autoscale the `Controller`

subsystem. This subsystem represents
software running on the target, and requires optimization.

In the Fixed-Point Tool, click

**New**, and select`Iterative Fixed-Point Conversion`

.**Tip**Switching workflows in the Fixed-Point tool clears the settings and any data collected during the active workflow. The model remains in its current state.

Under

**System Under Design (SUD)**, select the`Controller`

subsystem as the system to analyze and convert.Under

**Range Collection Mode**, select**Simulation ranges**.Under

**Simulation Inputs**, use the default model inputs for simulation.Click

**Prepare**to create a restore point and automatically prepare the system under design for conversion.Click the

**Collect Ranges**button arrow and select`Double precision`

. Click**Collect Ranges**to start the simulation.The Simulink software simulates the

`fxpdemo_feedback`

model in data type override mode and stores the results in`BaselineRun_2`

.In the

**Convert**section, click the**Settings**button. Set the**Safety margin for simulation min/max (%)**parameter to`20`

. Use the default settings for all other parameters.Click

**Propose Data Types**.The Fixed-Point Tool analyzes the scaling of all fixed-point blocks whose

**Lock output data type setting against changes by the fixed-point tools**parameter is cleared.The Fixed-Point Tool uses the minimum and maximum values stored in

`BaselineRun_2`

to propose each block's data types such that the precision is maximized while the full range of simulation values is spanned. The tool displays the proposed data types in the**Results**spreadsheet.Review the scaling that the Fixed-Point Tool proposes. You can choose to accept the scaling proposal for each block. In the

**Results**spreadsheet, select the corresponding**Accept**check box. 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 is selected for each of the Controller subsystem's blocks.Click the

**Apply Data Types**button.The Fixed-Point Tool applies the scaling proposals that you accepted in the previous step to the blocks in the

`Controller`

subsystem.In the

**Verify**section, click the**Simulate with Embedded Types**button.Simulink simulates the

`fxpdemo_feedback`

model using the new scaling that you applied. Information about this simulation is stored in a run named`EmbeddedRun_2`

. Afterward, the Fixed-Point Tool displays information about blocks that logged fixed-point data. The compiled data type (**CompiledDT**) column for`EmbeddedRun_2`

shows that the Controller subsystem's blocks used fixed-point data types with the new scaling.Click

**Compare Results**to open the Simulation Data Inspector.In the Simulation Data Inspector, select

`PlantOutput`

as the signal to compare.Simulation Data Inspector plots the signal associated with the plant output for

`BaselineRun_2`

and`EmbeddedRun_2`

, as well as their difference.The plant output signal represented by the fixed-point run achieves a steady state, but a small limit cycle is present because of non-optimal A/D design.