Sie sind auf Seite 1von 12

Fixed-Point Numbers :: Data Types and Scaling (Simulink Fixed Point)

1 of 12

jar:file:///C:/Program%20Files/MATLAB/R2011a/help/toolbox/fixpoint/...

Fixed-Point Numbers
On this page
About Fixed-Point Numbers
Signed Fixed-Point Numbers
Binary Point Interpretation
Scaling
Quantization
Range and Precision
Constant Scaling for Best Precision
Fixed-Point Data Type and Scaling Notation
Scaled Doubles
Example: Scaled Doubles
Example: Port Data Type Display

About Fixed-Point Numbers


Fixed-point numbers and their data types are characterized by their word size in bits, binary point, and whether they are
signed or unsigned. The Simulink Fixed Point software supports integers, fixed-point numbers. The main difference among
these data types is their binary point.
Note Fixed-point numbers can have a word size up to 128 bits.

A common representation of a binary fixed-point number , either signed or unsigned, is shown in the following figure.

where
bi are the binary digits (bits)
ws is the word length in bits
The most significant bit (MSB) is the leftmost bit, and is represented by location bws1
The least significant bit (LSB) is the rightmost bit, and is represented by location b0
The binary point is shown four places to the left of the LSB
Back to Top

Signed Fixed-Point Numbers


Computer hardware typically represents the negation of a binary fixed-point number in three different ways:
sign/magnitude, one's complement, and two's complement. Two's complement is the preferred representation of signed
fixed-point numbers and supported by the Simulink Fixed Point software.
Negation using two's complement consists of a bit inversion (translation into one's complement) followed by the addition of
a one. For example, the two's complement of 000101 is 111011.
Whether a fixed-point value is signed or unsigned is usually not encoded explicitly within the binary word; that is, there is
no sign bit. Instead, the sign information is implicitly defined within the computer architecture.

11/26/2014 11:41 AM

Fixed-Point Numbers :: Data Types and Scaling (Simulink Fixed Point)

2 of 12

jar:file:///C:/Program%20Files/MATLAB/R2011a/help/toolbox/fixpoint/...

Back to Top

Binary Point Interpretation


The binary point is the means by which fixed-point numbers are scaled. It is usually the software that determines the
binary point. When performing basic math functions such as addition or subtraction, the hardware uses the same logic
circuits regardless of the value of the scale factor. In essence, the logic circuits have no knowledge of a scale factor. They
are performing signed or unsigned fixed-point binary algebra as if the binary point is to the right of b0.
Back to Top

Scaling
The dynamic range of fixed-point numbers is much less than floating-point numbers with equivalent word sizes. To avoid
overflow conditions and minimize quantization errors, fixed-point numbers must be scaled.
With the Simulink Fixed Point software, you can select a fixed-point data type whose scaling is defined by its binary point,
or you can select an arbitrary linear scaling that suits your needs. This section presents the scaling choices available for
fixed-point data types.
You can represent a fixed-point number by a general slope and bias encoding scheme

where
V is an arbitrarily precise real-world value.
is the approximate real-world value.
Q, the stored value, is an integer that encodes V.
E

S = F 2 is the slope.
B is the bias.
The slope is partitioned into two components:
E

2 specifies the binary point. E is the fixed power-of-two exponent.


F is the slope adjustment factor. It is normalized such that 1 F < 2.
Note S and B are constants and do not show up in the computer hardware directly. Only the quantization
value Q is stored in computer memory.

The scaling modes available to you within this encoding scheme are described in the sections that follow. For detailed
information about how the supported scaling modes effect fixed-point operations, refer to Recommendations for Arithmetic
and Scaling.

Binary-Point-Only Scaling
Binary-point-only or power-of-two scaling involves moving the binary point within the fixed-point word. The advantage of
this scaling mode is to minimize the number of processor arithmetic operations.
With binary-point-only scaling, the components of the general slope and bias formula have the following values:
F=1
E

S = F2 = 2
B=0

The scaling of a quantized real-world number is defined by the slope S, which is restricted to a power of two. The negative
of the power-of-two exponent is called the fraction length. The fraction length is the number of bits to the right of the binary
point. For Binary-Point-Only scaling, specify fixed-point data types as
signed types fixdt(1, WordLength, FractionLength)
unsigned types fixdt(0, WordLength, FractionLength)

11/26/2014 11:41 AM

Fixed-Point Numbers :: Data Types and Scaling (Simulink Fixed Point)

3 of 12

jar:file:///C:/Program%20Files/MATLAB/R2011a/help/toolbox/fixpoint/...

Integers are a special case of fixed-point data types. Integers have a trivial scaling with slope 1 and bias 0, or equivalently
with fraction length 0. Specify integers as
signed integer fixdt(1, WordLength, 0)
unsigned integer fixdt(0, WordLength, 0)

Slope and Bias Scaling


When you scale by slope and bias, the slope S and bias B of the quantized real-world number can take on any value. The
slope must be a positive number. Using slope and bias, specify fixed-point data types as
fixdt(Signed, WordLength, Slope, Bias)

Unspecified Scaling
Specify fixed-point data types with an unspecified scaling as
fixdt(Signed, WordLength)
Simulink signals, parameters, and states must never have unspecified scaling. When scaling is unspecified, you must use
some other mechanism such as automatic best precision scaling to determine the scaling that the Simulink software uses.
Back to Top

Quantization
The quantization Q of a real-world value V is represented by a weighted sum of bits. Within the context of the general
slope and bias encoding scheme, the value of an unsigned fixed-point quantity is given by

while the value of a signed fixed-point quantity is given by

where
bi are binary digits, with bi = 1,0, for i = 0,1,...,ws1.
The word size in bits is given by ws, with ws = 1, 2, 3,..., 128.
E

S is given by F2 , where the scaling is unrestricted because the binary point does not have to be contiguous with
the word.
i

bi are called bit multipliers and 2 are called the weights.

Example: Fixed-Point Format


Formats for 8-bit signed and unsigned fixed-point values are shown in the following figure.

Note that you cannot discern whether these numbers are signed or unsigned data types merely by inspection since this
information is not explicitly encoded within the word.
The binary number 0011.0101 yields the same value for the unsigned and two's complement representation because the

11/26/2014 11:41 AM

Fixed-Point Numbers :: Data Types and Scaling (Simulink Fixed Point)

4 of 12

jar:file:///C:/Program%20Files/MATLAB/R2011a/help/toolbox/fixpoint/...

MSB = 0. Setting B = 0 and using the appropriate weights, bit multipliers, and scaling, the value is

Conversely, the binary number 1011.0101 yields different values for the unsigned and two's complement representation
since the MSB = 1.
Setting B = 0 and using the appropriate weights, bit multipliers, and scaling, the unsigned value is

while the two's complement value is

Back to Top

Range and Precision


The range of a number gives the limits of the representation, while the precision gives the distance between successive
numbers in the representation. The range and precision of a fixed-point number depends on the length of the word and the
scaling.

Range
The range of representable numbers for an unsigned and two's complement fixed-point number of size ws, scaling S, and
bias B is illustrated in the following figure.

ws

For both the signed and unsigned fixed-point numbers of any data type, the number of different bit patterns is 2 .
For example, if the fixed-point data type is an integer with scaling defined as S =1 and B = 0, then the maximum unsigned
ws1
value is 2
, because zero must be represented. In two's complement, negative numbers must be represented as well
ws1
as zero, so the maximum value is 2
1. Additionally, since there is only one representation for zero, there must be an
ws1
ws1
unequal number of positive and negative numbers. This means there is a representation for 2
but not for 2
.

Precision
The precision of a data type is given by the slope. In this usage, precision means the difference between neighboring

11/26/2014 11:41 AM

Fixed-Point Numbers :: Data Types and Scaling (Simulink Fixed Point)

5 of 12

jar:file:///C:/Program%20Files/MATLAB/R2011a/help/toolbox/fixpoint/...

representable values.

Fixed-Point Data Type Parameters


The low limit, high limit, and default binary-point-only scaling for the supported fixed-point data types discussed in Binary
Point Interpretation are given in the following table. See Limitations on Precision and Limitations on Range for more
information.
Fixed-Point Data Type Range and Default Scaling

Name

Data Type

Low Limit

High Limit

Unsigned
Integer

fixdt(0,ws,0)

Signed Integer

fixdt(1,ws,0)

-2

Unsigned Binary fixdt(0,ws,fl)


Point

ws - 1

ws

ws - 1- fl

Signed Binary
Point

fixdt(1,ws,fl)

-2

Unsigned Slope
Bias

fixdt(0,ws,s,b)

Signed Slope
Bias

fixdt(1,ws,s,b)

-s(2

(2

ws

ws - 1

)+b

- 1)2

s(2

ws

-1

ws - 1

s(2

-1

ws - 1

(2

Default Scaling
(~Precision)

-fl

- 1)2

-fl

- 1) + b

ws - 1

- 1) + b

-fl

-fl

s = Slope, b = Bias, ws = WordLength, fl = FractionLength

Range of an 8-Bit Fixed-Point Data Type Binary-Point-Only Scaling


The precisions, range of signed values, and range of unsigned values for an 8-bit generalized fixed-point data type with
1
binary-point-only scaling are listed in the follow table. Note that the first scaling value (2 ) represents a binary point that is
not contiguous with the word.

Scaling

2
2
2
2
2

Precision

Range of Signed Values (Low, Range of Unsigned Values (Low,


High)
High)

2.0

-256, 254

0, 510

1.0

-128, 127

0, 255

-1

0.5

-64, 63.5

0, 127.5

-2

0.25

-32, 31.75

0, 63.75

-3

0.125

-16, 15.875

0, 31.875

11/26/2014 11:41 AM

Fixed-Point Numbers :: Data Types and Scaling (Simulink Fixed Point)

6 of 12

Scaling

2
2
2
2
2

jar:file:///C:/Program%20Files/MATLAB/R2011a/help/toolbox/fixpoint/...

Precision

Range of Signed Values (Low, Range of Unsigned Values (Low,


High)
High)

-4

0.0625

-8, 7.9375

0, 15.9375

-5

0.03125

-4, 3.96875

0, 7.96875

-6

0.015625

-2, 1.984375

0, 3.984375

-7

0.0078125

-1, 0.9921875

0, 1.9921875

-8

0.00390625

-0.5, 0.49609375

0, 0.99609375

Range of an 8-Bit Fixed-Point Data Type Slope and Bias Scaling


The precision and ranges of signed and unsigned values for an 8-bit fixed-point data type using slope and bias scaling are
listed in the following table. The slope starts at a value of 1.25 with a bias of 1.0 for all slopes. Note that the slope is the
same as the precision.

Bias

Slope/Precision

Range of Signed Values


(low, high)

Range of Unsigned Values


(low, high)

1.25

-159, 159.75

1, 319.75

0.625

-79, 80.375

1, 160.375

0.3125

-39, 40.6875

1, 80.6875

0.15625

-19, 20.84375

1, 40.84375

0.078125

-9, 10.921875

1, 20.921875

0.0390625

-4, 5.9609375

1, 10.9609375

0.01953125

-1.5, 3.48046875

1, 5.98046875

0.009765625

-0.25, 2.240234375

1, 3.490234375

0.0048828125

0.375, 1.6201171875

1, 2.2451171875

Back to Top

Constant Scaling for Best Precision


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

11/26/2014 11:41 AM

Fixed-Point Numbers :: Data Types and Scaling (Simulink Fixed Point)

7 of 12

jar:file:///C:/Program%20Files/MATLAB/R2011a/help/toolbox/fixpoint/...

Discrete FIR Filter


Gain
Relay
Repeating Sequence Stair
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 Using the 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-002
3.3333e-001

3.3333e-004
3.3333e-003
3.3333e-002

3.3333e-005
3.3333e-004
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
-7
binary-point-only scaling of 2 (that is, the binary point is located seven places to the left of the right most bit). With
this data format, M becomes
0
3.1250e-002
3.3594e-001

0
0
3.1250e-002

0
0
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
3.3203e-002
3.3301e-001

0
2.9297e-003
3.3203e-002

0
0
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.
Back to Top

11/26/2014 11:41 AM

Fixed-Point Numbers :: Data Types and Scaling (Simulink Fixed Point)

8 of 12

jar:file:///C:/Program%20Files/MATLAB/R2011a/help/toolbox/fixpoint/...

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

where
V is the real-world value.
is the approximate real-world value.
E

S = F2 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

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

10^

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

Negative

n31 equals -31

Decimal point

1p5 equals 1.5

Container Type

Number Encoding

p2 equals 0.2
Scaling Encoding

11/26/2014 11:41 AM

Fixed-Point Numbers :: Data Types and Scaling (Simulink Fixed Point)

9 of 12

jar:file:///C:/Program%20Files/MATLAB/R2011a/help/toolbox/fixpoint/...

Symbol

Description

Example

Slope

ufix16_S5_B7 is a 16-bit unsigned


fixed-point data type with Slope of 5 and
Bias of 7

Bias

ufix16_S5_B7 is a 16-bit unsigned


fixed-point data type with Slope of 5 and
Bias of 7

Fixed exponent (2^)

sfix32_En31 is a 32-bit signed fixed-point


data type with a fraction length of 31

A negative fixed exponent describes


the fraction length
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

No example available. For backwards


compatibility only.

Note If you pass this string


to the
slDataTypeAndScale
function, it returns a valid
fixdt data type.

T or t

Compressed encoding for Slope


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

To identify and replace calls to


slDataTypeAndScale, use the Check for
calls to slDataTypeAndScale Model Advisor
check.

No example available. For .backwards


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

Back to Top

Scaled Doubles
What are Scaled Doubles?
Scaled doubles are a hybrid between floating-point and fixed-point numbers. The Simulink Fixed Point 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 Simulink Fixed Point 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.

11/26/2014 11:41 AM

Fixed-Point Numbers :: Data Types and Scaling (Simulink Fixed Point)

10 of 12

jar:file:///C:/Program%20Files/MATLAB/R2011a/help/toolbox/fixpoint/...

Consider an example where you are storing 0.75001 degrees Celsius in a data type sfix16_En13. For this data type:
The slope, S = 2
The bias, B = 0.

13

Using the scaling equation

where V is the real-world value and Q is the stored value.

B = 0.
= SQ = 2

13

Q = 0.75001.

= 0.75001/2

-13

= 6144.08192.

The data type sfix16_En13 can only represent integers, 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 demonstrates 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
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.
Back to Top

Example: Scaled Doubles


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

11/26/2014 11:41 AM

Fixed-Point Numbers :: Data Types and Scaling (Simulink Fixed Point)

11 of 12

jar:file:///C:/Program%20Files/MATLAB/R2011a/help/toolbox/fixpoint/...

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.
2. From the model menu, select Tools > Fixed-Point > 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 > Start.
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.
Back to Top

Example: Port Data Type Display


To display the data types for the ports in your model.
1. From the Simulink Format menu, point to Port/Signal Displays, and then click Port Data Types .

11/26/2014 11:41 AM

Fixed-Point Numbers :: Data Types and Scaling (Simulink Fixed Point)

12 of 12

jar:file:///C:/Program%20Files/MATLAB/R2011a/help/toolbox/fixpoint/...

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.
Back to Top
Was this topic helpful?

Overview

Yes

No

Floating-Point Numbers

1984-2011 The MathWorks, Inc. Terms of Use Patents Trademarks Acknowledgments

11/26/2014 11:41 AM

Das könnte Ihnen auch gefallen