Fixed-Point Numbers in Simulink

Constant Scaling for Best Precision

The following fixed-point Simulink® blocks provide a mode for scaling parameters whose values are constant vectors or matrices:

This scaling mode is based on binary-point-only scaling. Using this mode, you can scale a constant vector or matrix such that a common binary point is found based on the best precision for the largest value in the vector or matrix.

Constant scaling for best precision is available only for fixed-point data types with unspecified scaling. All other fixed-point data types use their specified scaling. You can use the Data Type Assistant (see Specify Data Types Using Data Type Assistant) on a block dialog box to enable the best precision scaling mode.

  1. On a block dialog box, click the Show data type assistant button .

    The Data Type Assistant appears.

  2. In the Data Type Assistant, and from the Mode list, select Fixed point.

    The Data Type Assistant displays additional options associated with fixed-point data types.

  3. From the Scaling list, select Best precision.

To understand how you might use this scaling mode, consider a 3-by-3 matrix of doubles, M, defined as

  3.3333e-003  3.3333e-004  3.3333e-005
  3.3333e-002  3.3333e-003  3.3333e-004
  3.3333e-001  3.3333e-002  3.3333e-003

Now suppose you specify M as the value of the Gain parameter for a Gain block. The results of specifying your own scaling versus using the constant scaling mode are described here:

  • Specified Scaling

    Suppose the matrix elements are converted to a signed, 10-bit generalized fixed-point data type with binary-point-only scaling of 2-7 (that is, the binary point is located seven places to the left of the right most bit). With this data format, M becomes

    0            0            0
    3.1250e-002  0            0
    3.3594e-001  3.1250e-002  0

    Note that many of the matrix elements are zero, and for the nonzero entries, the scaled values differ from the original values. This is because a double is converted to a binary word of fixed size and limited precision for each element. The larger and more precise the conversion data type, the more closely the scaled values match the original values.

  • Constant Scaling for Best Precision

    If M is scaled based on its largest matrix value, you obtain

    2.9297e-003  0            0
    3.3203e-002  2.9297e-003  0
    3.3301e-001  3.3203e-002  2.9297e-003

    Best precision would automatically select the fraction length that minimizes the quantization error. Even though precision was maximized for the given word length, quantization errors can still occur. In this example, a few elements still quantize to zero.

Fixed-Point Data Type and Scaling Notation

Simulink data type names must be valid MATLAB® identifiers with less than 128 characters. The data type name provides information about container type, number encoding, and scaling.

You can represent a fixed-point number using the fixed-point scaling equation

VV~=SQ+B,

where

  • V is the real-world value.

  • V~ is the approximate real-world value.

  • S=F2E is the slope.

  • F is the slope adjustment factor.

  • E is the fixed power-of-two exponent.

  • Q is the stored integer.

  • B is the bias.

For more information, see Scaling.

The following table provides a key for various symbols that appear in Simulink products to indicate the data type and scaling of a fixed-point value.

Symbol

Description

Example

Container Type

ufix

Unsigned fixed-point data type

ufix8 is an 8-bit unsigned fixed-point data type

sfix

Signed fixed-point data type

sfix128 is a 128-bit signed fixed-point data type

fltu

Scaled Doubles override of an unsigned fixed-point data type (ufix)

fltu32 is a scaled doubles override of ufix32

flts

Scaled Doubles override of a signed fixed-point data type (sfix)

flts64 is a scaled doubles override of sfix64

Number Encoding

e

10^

125e8 equals 125*(10^(8))

n

Negative

n31 equals -31

p

Decimal point

1p5 equals 1.5

p2 equals 0.2

Scaling Encoding

S

Slope

ufix16_S5_B7 is a 16-bit unsigned fixed-point data type with Slope of 5 and Bias of 7

B

Bias

ufix16_S5_B7 is a 16-bit unsigned fixed-point data type with Slope of 5 and Bias of 7

E

Fixed exponent (2^)

A negative fixed exponent describes the fraction length

sfix32_En31 is a 32-bit signed fixed-point data type with a fraction length of 31

F

Slope adjustment factor

ufix16_F1p5_En50 is a 16-bit unsigned fixed-point data type with a SlopeAdjustmentFactor of 1.5 and a FixedExponent of -50

C,c,D, or d

Compressed encoding for Bias

    Note:   If you pass this string to the slDataTypeAndScale function, it returns a valid fixdt data type.

No example available. For backwards compatibility only.

To identify and replace calls to slDataTypeAndScale, use the Check for calls to slDataTypeAndScale Model Advisor check.

T or t

Compressed encoding for Slope

    Note:   If you pass this string to the slDataTypeAndScale, it returns a valid fixdt data type.

No example available. For backwards compatibility only.

To identify and replace calls to slDataTypeAndScale, use the Check for calls to slDataTypeAndScale Model Advisor check.

Scaled Doubles

What Are Scaled Doubles?

Scaled doubles are a hybrid between floating-point and fixed-point numbers. The Fixed-Point Designer™ software stores them as doubles with the scaling, sign, and word length information retained. For example, the storage container for a fixed-point data type sfix16_En14 is int16. The storage container of the equivalent scaled doubles data type, flts16_En14 is floating-point double. For details of the fixed-point scaling notation, see Fixed-Point Data Type and Scaling Notation. The Fixed-Point Designer software applies the scaling information to the stored floating-point double to obtain the real-world value. Storing the value in a double almost always eliminates overflow and precision issues.

What is the Difference between Scaled Double and Double Data Types?.  The storage container for both the scaled double and double data types is floating-point double. Therefore both data type override settings, Double and Scaled double, provide the range and precision advantages of floating-point doubles. Scaled doubles retain the information about the specified data type and scaling, but doubles do not retain this information. Because scaled doubles retain the information about the specified scaling, they can also be used for overflow detection.

Consider an example where you are storing 0.75001 degrees Celsius in a data type sfix16_En13. For this data type:

  • The slope, S=213.

  • The bias, B=0.

Using the scaling equation VV~=SQ+B, where V is the real-world value and Q is the stored value.

  • B=0.

  • V~=SQ=213Q=0.75001.

Because the storage container of the data type sfix16_En13 is 16 bits, the stored integer Q can only be represented as an integer within these 16 bits, so the ideal value of Q is quantized to 6144 causing precision loss.

If you override the data type sfix16_En13 with Double, the data type changes to Double and you lose the information about the scaling. The stored-value equals the real-world value 0.75001.

If you override the data type sfix16_En13 with Scaled Double, the data type changes to flts16_En13. The scaling is still given by _En13 and is identical to that of the original data type. The only difference is the storage container used to hold the stored value which is now double so the stored-value is 6144.08192. This example shows one advantage of using scaled doubles: the virtual elimination of quantization errors.

When to Use Scaled Doubles

The Fixed-Point Tool enables you to perform various data type overrides on fixed-point signals in your simulations. Use scaled doubles to override the fixed-point data types and scaling using double-precision numbers to avoid quantization effects. Overriding the fixed-point data types provides a floating-point benchmark that represents the ideal output.

Scaled doubles are useful for:

  • Testing and debugging

  • Detecting overflows

  • Applying data type overrides to individual subsystems

    If you apply a data type override to subsystems in your model rather than to the whole model, Scaled doubles provide the information that the fixed-point portions of the model need for consistent data type propagation.

Use Scaled Doubles to Avoid Precision Loss

This example uses the ex_scaled_double model to show how you can avoid precision loss by overriding the data types in your model with scaled doubles.

About the Model

In this model:

  • The Constant block output data type is fixdt(1,8,4).

  • The Bitwise Operator block uses the AND operator and the bit mask 0xFF to pass the input value to the output. Because the Treat mask as parameter is set to Stored Integer, the block outputs the stored integer value, S, of its input. The encoding scheme is V=SQ+B, where V is the real-world value and Q is the stored integer value. For more information, see Scaling.

Running the Example

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

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

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

    The Fixed-Point Tool opens.

  3. In the Fixed-Point Tool, set the Data type override parameter to Use local settings and click Apply.

  4. From the model menu, select Simulation > Run.

    The simulation runs and the Display block displays 4.125 as the output value of the Constant block. The Stored Integer Display block displays 0100 0010, which is the binary equivalent of the stored integer value. Precision loss occurs because the output data type, fixdt(1,8,4), cannot represent the output value 4.1 exactly.

  5. In the Fixed-Point Tool, set the Data type override parameter to Scaled double and the Data type override applies to parameter to All numeric types. Then click Apply and rerun the simulation.

      Note:   You cannot use a Data type override setting of Double because the Bitwise Operator block does not support floating-point data types.

    The simulation runs and this time the Display block correctly displays 4.1 as the output value of the Constant block. The Stored Integer Display block displays 65, which is the binary equivalent of the stored integer value. Because the model uses scaled doubles to override the data type fixdt(1,8,4), the compiled output data type changes to flts8_En4, which is the scaled doubles equivalent of fixdt(1,8,4). No precision loss occurs because the scaled doubles retain the information about the specified data type and scaling, and they use a double to hold the stored value.

Display Port Data Types

To display the data types for the ports in your model.

  1. From the Simulink Display menu, select Signals and Ports, and then select Port Data Types.

The port display for fixed-point signals consists of three parts: the data type, the number of bits, and the scaling. These three parts reflect the block Output data type parameter value or the data type and scaling that is inherited from the driving block or through back propagation.

The following model displays its port data types.

In the model, the data type displayed with the In1 block indicates that the output data type name is sfix16_Sp2_B10. This corresponds to fixdt(1, 16, 0.2, 10) which is a signed 16 bit fixed-point number with slope 0.2 and bias 10.0. The data type displayed with the In2 block indicates that the output data type name is sfix16_En6. This corresponds to fixdt(1, 16, 6) which is a signed 16 bit fixed-point number with fraction length of 6.

Was this topic helpful?