| Simulink® Fixed Point™ | ![]() |
| On this page… |
|---|
Configuring Blocks with Fixed-Point Output Configuring Blocks with Fixed-Point Parameters Passing Fixed-Point Data Between Simulink® Models and the MATLAB® Software |
You can create a fixed-point model by configuring Simulink® blocks to output fixed-point signals. Simulink blocks that support fixed-point output provide parameters that allow you to specify whether a block should output fixed-point signals and, if so, the size, scaling, and other attributes of the fixed-point output. These parameters typically appear on the Signal Attributes pane of the block's parameter dialog box.

The following sections explain how to use these parameters to configure a block for fixed-point output.
Many Simulink blocks allow you to specify an output data type and scaling using a parameter that appears on the block dialog box. This parameter—typically named Output data type—provides a pull-down menu that lists the data types a particular block supports. In general, you can specify the output data type as a rule that inherits a data type, a built-in data type, an expression that evaluates to a data type, or a Simulink data type object. See Specifying Block Output Data Types for more information.
The Simulink® Fixed Point™ software enables you to configure Simulink blocks with:
Fixed-point data types
Fixed-point data types are characterized by their word size in bits and by their binary point—the means by which fixed-point values are scaled. See Fixed-Point Numbers for more information.
Floating-point data types
Floating-point data types are characterized by their sign bit, fraction (mantissa) field, and exponent field. See Floating-Point Numbers for more information.
To configure blocks with Simulink Fixed Point data types, specify the data type parameter on a block dialog box as an expression that evaluates to a data type. Alternatively, you can use an assistant that simplifies the task of entering data type expressions (see Specifying Fixed-Point Data Types with the Data Type Assistant). The sections that follow describe varieties of fixed-point and floating-point data types, and the corresponding functions that you use to specify them.
Integers. You can specify unsigned and signed integers with the uint and sint functions, respectively.
For example, to configure a 16-bit unsigned integer via the block dialog box, specify the Output data type parameter as uint(16). To configure a 16-bit signed integer, specify the Output data type parameter as sint(16).
For integer data types, the default binary point is assumed to lie to the right of all bits.
Fractional Numbers. You can specify unsigned and signed fractional numbers with the ufrac and sfrac functions, respectively.
For example, to configure the output as a 16-bit unsigned fractional number via the block dialog box, specify the Output data type parameter to be ufrac(16). To configure a 16-bit signed fractional number, specify Output data type to be sfrac(16).
Fractional numbers are distinguished from integers by their default scaling. Whereas signed and unsigned integer data types have a default binary point to the right of all bits, unsigned fractional data types have a default binary point to the left of all bits, while signed fractional data types have a default binary point to the right of the sign bit.
Both unsigned and signed fractional data types support guard bits, which act to guard against overflow. For example, sfrac(16,4) specifies a 16-bit signed fractional number with 4 guard bits. The guard bits lie to the left of the default binary point.
Generalized Fixed-Point Numbers. You can specify unsigned and signed generalized fixed-point numbers with the ufix and sfix functions, respectively.
For example, to configure the output as a 16-bit unsigned generalized fixed-point number via the block dialog box, specify the Output data type parameter to be ufix(16). To configure a 16-bit signed generalized fixed-point number, specify Output data type to be sfix(16).
Generalized fixed-point numbers are distinguished from integers and fractionals by the absence of a default scaling. For these data types, a block typically inherits its scaling from another block.
Note Alternatively, you can use the fixdt function to create integer, fractional, and generalized fixed-point objects. The fixdt function also allows you to specify scaling for fixed-point data types. |
Floating-Point Numbers. The Simulink Fixed Point software supports single-precision and double-precision floating-point numbers as defined by the IEEE® Standard 754-1985 for Binary Floating-Point Arithmetic. You can specify floating-point numbers with the Simulink float function.
For example, to configure the output as a single-precision floating-point number via the block dialog box, specify the Output data type parameter as float('single'). To configure a double-precision floating-point number, specify Output data type as float('double').
You can also specify a nonstandard floating-point number that mimics the IEEE style. For this data type, the fraction (mantissa) can range from 1 to 52 bits and the exponent can range from 1 to 11 bits. For example, to configure a nonstandard floating-point number having 32 total bits and 9 exponent bits, specify Output data type as float(32,9).
Note These numbers are normalized with a hidden leading 1 for all exponents except the smallest possible exponent. However, the largest possible exponent might not be treated as a flag for Infs or NaNs. |
The Data Type Assistant is an interactive graphical tool that simplifies the task of specifying data types for Simulink blocks and data objects. The assistant appears on block and object dialog boxes, adjacent to parameters that provide data type control, such as the Output data type parameter. For more information about accessing and interacting with the assistant, see Using the Data Type Assistant.
You can use the Data Type Assistant to specify a fixed-point data type. When you select Fixed point in the Mode field, the assistant displays fields for describing additional attributes of a fixed-point data type, as shown in this example:

You can set the following fixed-point attributes:
Sign. Select whether you want the fixed-point data to be Signed or Unsigned. Signed data can represent positive and negative quantities. Unsigned data represents positive values only.
Word length. Specify the size (in bits) of the word that will hold the quantized integer. Large word sizes represent large quantities with greater precision than small word sizes. Fixed-point word sizes up to 128 bits are supported for simulation.
Scaling. Specify the method for scaling your fixed-point data to avoid overflow conditions and minimize quantization errors. You can select the following scaling modes:
| Scaling Mode | Description |
|---|---|
| Binary point | If you select this mode, the assistant displays the Fraction length field, specifying the binary point location. Binary points can be positive or negative integers. A positive integer moves the binary point left of the rightmost bit by that amount. For example, an entry of 2 sets the binary point in front of the second bit from the right. A negative integer moves the binary point further right of the rightmost bit by that amount, as in this example:
See Binary-Point-Only Scaling for more information. |
| Slope and bias | If you select this mode, the assistant displays fields for entering the Slope and Bias.
See [Slope Bias] Scaling for more information. |
| Best precision | If you select this mode, the block scales a constant vector or matrix such that the precision of its elements is maximized. This mode is available only for particular blocks. See Constant Scaling for Best Precision for more information. |
Calculate Best-Precision Scaling. The Simulink Fixed Point software can automatically calculate "best-precision" values for both Binary point and Slope and bias scaling, based on the values that you specify for other parameters on the dialog box. To calculate best-precision-scaling values automatically, enter values for the block's Output minimum and Output maximum parameters. Afterward, click the Calculate Best-Precision Scaling button in the assistant.
You specify how fixed-point numbers are rounded with the Round integer calculations toward parameter. The following rounding modes are supported:
Zero — This mode rounds toward zero and is equivalent to the MATLAB® fix function.
Nearest — This mode rounds toward the nearest representable number, with the exact midpoint rounded toward positive infinity. Rounding toward nearest is equivalent to the Fixed-Point Toolbox™ nearest function.
Ceiling — This mode rounds toward positive infinity and is equivalent to the MATLAB ceil function.
Floor — This mode rounds toward negative infinity and is equivalent to the MATLAB floor function.
Simplest — This mode automatically chooses between round toward floor and round toward zero to produce generated code that is as efficient as possible.
For more information about each of these rounding modes, see Rounding.
You control how overflow conditions are handled for fixed-point operations with the Saturate on integer overflow check box.
If this box is selected, overflows saturate to either the maximum or minimum value represented by the data type. For example, an overflow associated with a signed 8-bit integer can saturate to -128 or 127.
If this box is not selected, overflows wrap to the appropriate value that is representable by the data type. For example, the number 130 does not fit in a signed 8-bit integer, and would wrap to -126.
If the output data type is a generalized fixed-point number, you have the option of locking its scaling by selecting the Lock output scaling against changes by the autoscaling tool check box.
When locked, the Fixed-Point Tool and automatic scaling script autofixexp (see Automatic Scaling) do not change the output scaling. Otherwise, the tool and autofixexp script are free to adjust the scaling.
You can configure Data Type Conversion blocks to treat signals as real-world values or as stored integers with the Input and output to have equal parameter.

The possible values are Real World Value (RWV) and Stored Integer (SI).
In terms of the variables defined in Scaling, the real-world value is given by V and the stored integer value is given by Q. You may want to treat numbers as stored integer values if you are modeling hardware that produces integers as output.
Certain Simulink blocks allow you to specify fixed-point numbers as the values of parameters used to compute the block's output, e.g., the Gain parameter of a Gain block.
Note S-functions and the Stateflow® Chart block do not support fixed-point parameters. |
You can specify a fixed-point parameter value either directly by setting the value of the parameter to an expression that evaluates to a fi object, or indirectly by setting the value of the parameter to an expression that refers to a fixed-point Simulink.Parameter object.
Note Simulating or performing data type override on a model with fi objects requires a Fixed-Point Toolbox software license. See Sharing Fixed-Point Models for more information. |
You can specify fixed-point values for block parameters using fi objects (see Working with fi Objects in the Fixed-Point Toolbox™ User's Guide for more information). In the block dialog's parameter field, simply enter the name of a fi object or an expression that includes the fi constructor function.
For example, entering the expression
fi(3.3,true,8,3)
as the Constant value parameter for the Constant block specifies a signed fixed-point value of 3.3, with a word length of 8 bits and a fraction length of 3 bits.

You can specify fixed-point parameter objects for block parameters using instances of the Simulink.Parameter class. To create a fixed-point parameter object, either specify a fi object as the parameter object's Value property, or specify the relevant fixed-point data type for the parameter object's DataType property.
For example, suppose you want to create a fixed-point constant in your model. You could do this using a fixed-point parameter object and a Constant block as follows:
Enter the following command at the MATLAB prompt to create an instance of the Simulink.Parameter class:
my_fixpt_param = Simulink.Parameter
Specify either the name of a fi object or an expression that includes the fi constructor function as the parameter object's Value property:
my_fixpt_param.Value = fi(3.3,true,8,3)
Alternatively, you can set the parameter object's Value and DataType properties separately. In this case, specify the relevant fixed-point data type using a Simulink.AliasType object, a Simulink.NumericType object, or a fixdt expression. For example, the following commands independently set the parameter object's value and data type, using a fixdt expression as the DataType string:
my_fixpt_param.Value = 3.3; my_fixpt_param.DataType = 'fixdt(true,8,2^-3,0)'
Specify the parameter object as the value of a block's parameter. For example, my_fixpt_param specifies the Constant value parameter for the Constant block in the following model:

Consequently, the Constant block outputs a signed fixed-point value of 3.3, with a word length of 8 bits and a fraction length of 3 bits.
You can read fixed-point data from the MATLAB software into your Simulink models, and there are a number of ways in which you can log fixed-point information from your models and simulations to the workspace.
You can read fixed-point data from the MATLAB workspace into a Simulink model via the From Workspace block. To do so, the data must be in structure format with a Fixed-Point Toolbox fi object in the values field. In array format, the From Workspace block only accepts real, double-precision data.
To read in fi data, the Interpolate data parameter of the From Workspace block must not be selected, and the Form output after final data value by parameter must be set to anything other than Extrapolation.
You can write fixed-point output from a model to the MATLAB workspace via the To Workspace block in either array or structure format. Fixed-point data written by a To Workspace block to the workspace in structure format can be read back into a Simulink model in structure format by a From Workspace block.
Note To write fixed-point data to the workspace as a fi object, select the Log fixed-point data as a fi object check box on the To Workspace block dialog. Otherwise, fixed-point data is converted to double and written to the workspace as double. |
For example, you can use the following code to create a structure in the MATLAB workspace with a fi object in the values field. You can then use the From Workspace block to bring the data into a Simulink model.
a = fi([sin(0:10)' sin(10:-1:0)'])
a =
0 -0.5440
0.8415 0.4121
0.9093 0.9893
0.1411 0.6570
-0.7568 -0.2794
-0.9589 -0.9589
-0.2794 -0.7568
0.6570 0.1411
0.9893 0.9093
0.4121 0.8415
-0.5440 0
DataTypeMode: Fixed-point: binary point scaling
Signed: true
WordLength: 16
FractionLength: 15
RoundMode: nearest
OverflowMode: saturate
ProductMode: FullPrecision
MaxProductWordLength: 128
SumMode: FullPrecision
MaxSumWordLength: 128
CastBeforeSum: true
s.signals.values = a
s =
signals: [1x1 struct]
s.signals.dimensions = 2
s =
signals: [1x1 struct]
s.time = [0:10]'
s =
signals: [1x1 struct]
time: [11x1 double]The From Workspace block in the following model has the fi structure s in the Data parameter. In the model, the following parameters in the Solver pane of the Configuration Parameters dialog box have the indicated settings:
Start time — 0.0
Stop time — 10.0
Type — Fixed-step
Solver — discrete (no continuous states)
Fixed-step size (fundamental sample time) — 1.0

The To Workspace block writes the result of the simulation to the MATLAB workspace as a fi structure.
simout.signals.values
ans =
0 -8.7041
13.4634 6.5938
14.5488 15.8296
2.2578 10.5117
-12.1089 -4.4707
-15.3428 -15.3428
-4.4707 -12.1089
10.5117 2.2578
15.8296 14.5488
6.5938 13.4634
-8.7041 0When fixed-point signals are logged to the MATLAB workspace via signal logging, they are always logged as Fixed-Point Toolbox fi objects. To enable signal logging for a signal, select the Log signal data option in the signal's Signal Properties dialog box. For more information, refer to Logging Signals in Using Simulink®.
When you log signals from a referenced model or Stateflow chart in your model, the word lengths of fi objects may be larger than you expect. The word lengths of fixed-point signals in referenced models and Stateflow charts are logged as the next larger data storage container size.
The Simulink software provides an application programming interface (API) that enables programmatic access to block data, such as block inputs and outputs, parameters, states, and work vectors, while a simulation is running. You can use this interface to develop MATLAB programs capable of accessing block data while a simulation is running or to access the data from the MATLAB command line. Fixed-point signal information is returned to you via this API as fi objects. For more information about the API, refer to Accessing Block Data During Simulation in Using Simulink®.
In addition to the features described in the previous sections, the Simulink Fixed Point software provides you with:
An automatic scaling tool
Code generation capabilities
You can use the autofixexp script to automatically change the scaling for each Simulink block that has generalized fixed-point output and does not have its scaling locked. The script uses the maximum and minimum values logged during the last simulation run. The scaling is changed such that the simulation range is covered and the precision is maximized.
As an alternative to, and extension of, the automatic scaling script, you can use the Fixed-Point Tool. This interface allows you to easily control the parameters associated with automatic scaling and display the simulation results for a given model. To learn how to use the Fixed-Point Tool, refer to Fixed-Point Tool.
With the Real-Time Workshop® product, the Simulink Fixed Point software can generate C code. The code generated from fixed-point blocks uses only integer types and automatically includes all operations, such as shifts, needed to account for differences in fixed-point locations.
You can use the generated code on embedded fixed-point processors or rapid prototyping systems even if they contain a floating-point processor. The code is structured so that key operations can be readily replaced by optimized target-specific libraries that you supply. You can also use Target Language Compiler to customize the generated code. Refer to Code Generation for more information about code generation using fixed-point blocks.
![]() | The Development Cycle | Example: Converting from Doubles to Fixed Point | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |