To open the Simulink^{®} feedback design model for this tutorial, at the MATLAB^{®} command line, type `fxpdemo_feedback`

.

The Simulink model of the feedback design 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 Sum 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 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 Sum 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),$$

*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 multiplication and accumulation of the most recent inputs
and numerator coefficients in the accumulator. The second summation in *y*(*k*) represents 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).

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:

Initial Guess at Scaling. Run an initial 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.

Range Collection. 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.

Automatic Data Typing. Perform the automatic data typing 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.

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

Open the

`fxpdemo_feedback`

model.Open the Fixed-Point Tool. In the model, in the

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

**System Under Design**, select the subsystem you want to convert to fixed point. 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**Use current settings**. Click the**Collect Ranges**button to start the simulation. The Fixed-Point Tool simulates the model using the currently specified data types and stores collected range information in a run titled`Run 1`

. You can view the collected ranges in the**SimMin**and**SimMax**columns of the spreadsheet, or in the**Result Details**pane.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.In the model, click on the antenna icon at the output of the Transfer Fcn block named

`Analog Plant`

.Simulation Data Inspector plots the signal associated with the plant output.

The 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 enables you to perform a global override of the fixed-point data types with double-precision data types, thereby avoiding quantization effects. When performing automatic scaling to propose higher fidelity fixed-point scaling, the Fixed-Point Tool uses these simulation results.

In the Fixed-Point Tool, click the

**Collect Ranges**button arrow and select**Double Precision**.Click the

**Collect Ranges**button.The Simulink software simulates the

`fxpdemo_feedback`

model in data type override mode and stores the results in the`Ranges (Double)`

run. Afterward, in the spreadsheet, the Fixed-Point Tool displays the run results. The compiled data type (**CompiledDT**) column for the`Ranges (Double)`

run shows that the model's blocks used a`double`

data type during simulation.In the model, click on the antenna icon at the output of the Transfer Fcn block named

`Analog Plant`

again.Simulation Data Inspector plots both the

`Ranges (Double)`

and`Run 1`

versions of the signal associated with the plant output (upper axes). Click**Compare**to plot the difference between the active and reference versions of that signal. Compare the ideal (`double`

data type) plant output signal with its fixed-point version.### Tip

From the Simulation Data Inspector menu bar, use the zoom tools to zoom in on an area.

Using the automatic data typing procedure, you can maximize the precision of the output data type while spanning the full simulation range.

Because no design range information is supplied, the Fixed-Point Tool uses the
simulation range data that was collected during the simulation run 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.

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.

Perform automatic data typing for the `Controller`

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

In the Fixed-Point Tool, in the

**Convert Data Types**section of the toolstrip, 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**.In the dialog box, select

**Ranges (Double)**, and then click**OK**.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 the

`Ranges (Double)`

run 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 spreadsheet.Review the scaling that the Fixed-Point Tool proposes. You can choose to accept the scaling proposal for each block. In the 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.In the Fixed-Point Tool, click the

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

`Controller`

subsystem's blocks.In the

**Verify**section of the toolstrip, 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`Embedded`

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

run shows that the Controller subsystem's blocks used fixed-point data types with the new scaling.In the model, click on the antenna icon at the output of the Transfer Fcn block named

`Analog Plant`

again. In Simulation Data Inspector, click the**Compare**button.Next to

**Baseline**, select the`Ranges (Double)`

run. Next to**Compare to**, select the`Embedded`

run.Click

**Compare**.Simulation Data Inspector plots the fixed-point and double override versions of the plant output signal, 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.