Convert input signal to specified data type
Signal Attributes
The Data Type Conversion block converts an input signal of any Simulink^{®} data type to the data type that you specify.
The input can be any real- or complex-valued signal. If the input is real, the output is real. If the input is complex, the output is complex.
Note To control the output data type by specifying block parameters, or to inherit a data type from a downstream block, use the Data Type Conversion block. To inherit a data type from a different signal in the model, use the Data Type Conversion Inherited block. |
When you convert between fixed-point data types, the Input and output to have equal parameter controls block behavior. If neither input nor output use fixed-point scaling, because they are not of a fixed-point data type or have trivial fixed-point scaling, this parameter does not change the behavior of the block. For more information about fixed-point numbers, see Fixed-Point Numbers in Simulink.
To convert a signal from one data type to another by attempting
to preserve the real-world value of the input signal, select Real
World Value (RWV)
, the default setting. The block accounts
for the scaling of the input and output and, within the limits of
the specified data types, attempts to generate an output of equal
real-world value.
To change the real-world value of the input signal by performing
a scaling reinterpretation of the stored integer value, select Stored
Integer (SI)
. Within the limits of the specified data
types, the block attempts to preserve the stored integer value of
the signal during conversion. A best practice is to specify input
and output data types using the same word length and signedness so
that the block changes only the scaling of the signal. Specifying
a different signedness or word length for the input and output could
produce unexpected results such as range loss or unexpected sign extensions.
For an example, see Reinterpret Signal Using a Fixed-Point Data Type.
If you select Stored Integer (SI)
,
the block does not perform a lower-level bit reinterpretation of a
floating-point input signal. For example, if the input is of the data
type single
and has value 5
,
the bits that store the input in memory are given in hexadecimal by
the following command.
num2hex(single(5))
40a00000
However, the Data Type Conversion block does
not treat the stored integer value as 40a00000
,
but instead as the real-world value, 5
. After conversion,
the stored integer value of the output is 5
.
Use a Data Type Conversion block to cast enumerated signals as follows:
To cast a signal of enumerated type to a signal of any numeric type.
The underlying integers of all enumerated values input to the Data Type Conversion block must be within the range of the numeric type. Otherwise, an error occurs during simulation.
To cast a signal of any integer type to a signal of enumerated type.
The value input to the Data Type Conversion block must match the underlying value of an enumerated value. Otherwise, an error occurs during simulation.
You can enable the block's Saturate on integer overflow parameter so that Simulink uses the default value of the enumerated type when the value input to the block does not match the underlying value of an enumerated value. See Type Casting for Enumerations.
You cannot use a Data Type Conversion block in the following cases:
To cast a non-integer numeric signal to an enumerated signal.
To cast a complex signal to an enumerated signal, regardless of the data types of the complex signal's real and imaginary parts.
See Simulink Enumerations for information on working with enumerated types.
The Data Type Conversion block handles any data type that Simulink supports, including fixed-point and enumerated data types.
For more information, see Data Types Supported by Simulink in the Simulink documentation.
Display the Data Type Assistant.
The Data Type Assistant helps you set the Output data type parameter.
For more information, see Control Signal Data Types.
Select to lock the output data type setting of this block against changes by the Fixed-Point Tool and the Fixed-Point Advisor.
Default: Off
Locks the output data type setting for this block.
Allows the Fixed-Point Tool and the Fixed-Point Advisor to change the output data type setting for this block.
Parameter: LockScale |
Type: character vector |
Value: 'off' | 'on' |
Default: 'off' |
For more information, see Use Lock Output Data Type Setting.
Specify which type of input and output must be equal, in the context of fixed point data representation.
Default: Real
World Value (RWV)
Real World Value (RWV)
Specifies the goal of making the Real World Value
(RWV)
of the input equal to the Real World
Value (RWV)
of the output.
Stored Integer (SI)
Specifies the goal of making the Stored Integer
(SI)
value of the input equal to the Stored
Integer (SI)
value of the output.
For the command-line information, see Block-Specific Parameters.
Specify the rounding mode for fixed-point operations.
Default: Floor
Ceiling
Rounds both positive and negative numbers toward positive infinity.
Equivalent to the MATLAB^{®} ceil
function.
Convergent
Rounds number to the nearest representable value. If a tie occurs,
rounds to the nearest even integer. Equivalent to the Fixed-Point Designer™ convergent
function.
Floor
Rounds both positive and negative numbers toward negative infinity.
Equivalent to the MATLAB floor
function.
Nearest
Rounds number to the nearest representable value. If a tie occurs,
rounds toward positive infinity. Equivalent to the Fixed-Point Designer nearest
function.
Round
Rounds number to the nearest representable value. If a tie occurs,
rounds positive numbers toward positive infinity and rounds negative
numbers toward negative infinity. Equivalent to the Fixed-Point Designer round
function.
Simplest
Automatically chooses between round toward floor and round toward zero to generate rounding code that is as efficient as possible.
Zero
Rounds number toward zero. Equivalent to the MATLAB fix
function.
Parameter: RndMeth |
Type: character vector |
Value: 'Ceiling' | 'Convergent' | 'Floor' | 'Nearest' | 'Round' | 'Simplest' | 'Zero' |
Default: 'Floor' |
For more information, see Rounding in the Fixed-Point Designer documentation.
Specify whether overflows saturate.
Default: Off
Overflows saturate to either the minimum or maximum value that the data type can represent.
For example, an overflow associated with a signed 8-bit integer can saturate to -128 or 127.
Overflows wrap to the appropriate value that the data type can represent.
For example, the number 130 does not fit in a signed 8-bit integer and wraps to -126.
Consider selecting this check box when your model has a possible overflow and you want explicit saturation protection in the generated code.
Consider clearing this check box when you want to optimize efficiency of your generated code.
Clearing this check box also helps you to avoid overspecifying how a block handles out-of-range signals. For more information, see Checking for Signal Range Errors.
When you select this check box, saturation applies to every internal operation on the block, not just the output or result.
In general, the code generation process can detect when overflow is not possible. In this case, the code generator does not produce saturation code.
Parameter: SaturateOnIntegerOverflow |
Type: character vector |
Value: 'off' | 'on' |
Default: 'off' |
Note:
This parameter is not visible in the block dialog box unless
it is explicitly set to a value other than |
Lower value of the output range that Simulink checks.
Default: []
(unspecified)
Specify this number as a finite, real, double, scalar value.
Simulink uses the minimum to perform:
Parameter range checking (see Specify Minimum and Maximum Values for Block Parameters) for some blocks.
Simulation range checking (see Signal Ranges and Enabling Simulation Range Checking).
Automatic scaling of fixed-point data types.
Optimization of the code that you generate from the model. This optimization can remove algorithmic code and affect the results of some simulation modes such as SIL or external mode. For more information, see Optimize using the specified minimum and maximum values.
Note: Output minimum does not saturate or clip the actual output signal. Use the Saturation block instead. |
Parameter: OutMin |
Type: character vector |
Value: '[ ]' |
Default: '[ ]' |
Upper value of the output range that Simulink checks.
Default: []
(unspecified)
Specify this number as a finite, real, double, scalar value.
Simulink uses the maximum value to perform:
Parameter range checking (see Specify Minimum and Maximum Values for Block Parameters) for some blocks.
Simulation range checking (see Signal Ranges and Enabling Simulation Range Checking).
Automatic scaling of fixed-point data types.
Optimization of the code that you generate from the model. This optimization can remove algorithmic code and affect the results of some simulation modes such as SIL or external mode. For more information, see Optimize using the specified minimum and maximum values.
Note: Output maximum does not saturate or clip the actual output signal. Use the Saturation block instead. |
Parameter: OutMax |
Type: character vector |
Value: '[ ]' |
Default: '[ ]' |
Specify the output data type.
Default: Inherit:
Inherit via back propagation
Inherit: Inherit via back propagation
Use data type of the driving block.
double
Output data type is double
.
single
Output data type is single
.
int8
Output data type is int8
.
uint8
Output data type is uint8
.
int16
Output data type is int16
.
uint16
Output data type is uint16
.
int32
Output data type is int32
.
uint32
Output data type is uint32
.
boolean
Output data type is boolean
. The Data Type
Conversion block converts real, nonzero numeric values (including NaN
and Inf
)
to boolean
true
(1
).
fixdt(1,16,0)
Output data type is fixed point fixdt(1,16,0)
.
fixdt(1,16,2^0,0)
Output data type is fixed point fixdt(1,16,2^0,0)
.
Enum: <class name>
Use an enumerated data type, for example, Enum: BasicColors
.
<data type expression>
Use a data type object, for example, Simulink.NumericType
.
See Block-Specific Parameters for the command-line information.
For more information, see Control Signal Data Types.
Select the category of data to specify.
Default: Inherit
Inherit
Inheritance rules for data types. Selecting Inherit
enables Inherit
via back propagation
.
Built in
Built-in data types. Selecting Built in
enables
a second menu/text box to the right. Select one of the following choices:
double
(default)
single
int8
uint8
int16
uint16
int32
uint32
boolean
Fixed point
Fixed-point data types.
Enumerated
Enumerated data types. Selecting Enumerated
enables
a second menu/text box to the right, where you can enter the class
name.
Expression
Expressions that evaluate to data types. Selecting Expression
enables
a second menu/text box to the right, where you can enter the expression.
Clicking the Show data type assistant button enables this parameter.
See Block-Specific Parameters for the command-line information.
See Specify Data Types Using Data Type Assistant.
Specify data type override mode for this signal.
Default: Inherit
Inherit
Inherits the data type override setting from its context, that
is, from the block, Simulink.Signal
object or Stateflow^{®} chart
in Simulink that is using the signal.
Off
Ignores the data type override setting of its context and uses the fixed-point data type specified for the signal.
The ability to turn off data type override for an individual data type provides greater control over the data types in your model when you apply data type override. For example, you can use this option to ensure that data types meet the requirements of downstream blocks regardless of the data type override setting.
This parameter appears only when the Mode is Built
in
or Fixed point
.
Specify whether you want the fixed-point data as signed or unsigned.
Default: Signed
Signed
Specify the fixed-point data as signed.
Unsigned
Specify the fixed-point data as unsigned.
Selecting Mode > Fixed
point
enables this parameter.
For more information, see Specifying a Fixed-Point Data Type.
Specify the bit size of the word that holds the quantized integer.
Default: 16
Minimum: 0
Maximum: 32
Selecting Mode > Fixed
point
enables this parameter.
For more information, see Specifying a Fixed-Point Data Type.
Specify the method for scaling your fixed-point data to avoid overflow conditions and minimize quantization errors.
Default: Best
precision
Binary point
Specify binary point location.
Slope and bias
Enter slope and bias.
Best precision
Specify best-precision values.
Selecting Mode > Fixed
point
enables this parameter.
Selecting Binary point
enables:
Fraction length
Calculate Best-Precision Scaling
Selecting Slope and bias
enables:
Slope
Bias
Calculate Best-Precision Scaling
For more information, see Specifying a Fixed-Point Data Type.
Specify fraction length for fixed-point data type.
Default: 0
Binary points can be positive or negative integers.
Selecting Scaling > Binary
point
enables this parameter.
For more information, see Specifying a Fixed-Point Data Type.
Specify slope for the fixed-point data type.
Default: 2^0
Specify any positive real number.
Selecting Scaling > Slope
and bias
enables this parameter.
For more information, see Specifying a Fixed-Point Data Type.
Specify bias for the fixed-point data type.
Default: 0
Specify any real number.
Selecting Scaling > Slope
and bias
enables this parameter.
For more information, see Specifying a Fixed-Point Data Type.
The example model ex_data_type_conversion_rwv_si
uses Data
Type Conversion blocks to show the meaning of the real-world
value and the stored integer of a signal. For basic information about
fixed-point scaling, see Scaling in the Fixed-Point Designer documentation.
The Fixed-Point Constant block represents the real-world value 15
by
using a fixed-point data type with binary-point scaling 2^{-5}.
Due to the scaling, the output signal uses a stored integer value
of 480
.
The model uses Data Type Conversion blocks to convert the signal to a fixed-point data type with binary-point scaling 2^{-2}.
The Fixed to Fixed: Preserve RWV block converts the
input signal by preserving the real-world value, 15
.
The parameter Input and output to have equal is
set to Real World Value (RWV)
.
The output signal has the same real-world value as the input,
that is, 15
. Due to the fixed-point scaling, the
output uses a stored integer value of 60
.
The Fixed to Fixed: Preserve SI block converts the
input signal by preserving the stored integer value, 480
.
The parameter Input and output to have equal is
set to Stored Integer (SI)
.
The output signal uses the same stored integer value as the
input, that is, 480
. Due to the fixed-point scaling,
the output has a real-world value of 120
.
The figure shows the conversion mechanism for the two blocks.
The Double Constant block represents the real-world value 15
by
using the floating-point data type double
. The
output signal does not use fixed-point scaling.
The model uses Data Type Conversion blocks to
convert the double
signal to a fixed-point data
type with binary-point scaling 2^{-2}.
The Float to Fixed: Preserve RWV block converts the
input signal by preserving the real-world value, 15
.
The output signal has the same real-world value. Due to the fixed-point
scaling, the output uses a stored integer value of 60
.
The Float to Fixed: Preserve SI block converts the
input signal by attempting to preserve the stored integer value. However,
the block does not use the underlying bits that store the floating-point
signal in memory. Instead, the block uses the real-world value of
the input, 15
, as the stored integer of the output
signal. Due to the fixed-point scaling, the real-world value of the
output is 3.75
.
The figure shows the conversion mechanism for the two blocks.
The blocks also use these mechanisms if the input uses the floating-point
data type single
.
Suppose your hardware uses the data type uint8
to
store data from a temperature sensor. Also suppose that the minimum
stored integer value 0
represents –20
degrees
Celsius while the maximum 255
represents 60
degrees.
The following model uses a Data Type Conversion block
to convert the stored integer value of the sensor data to degrees
Celsius.
The Data Type Conversion block parameter Input
and output to have equal is set to Stored
Integer (SI)
. The block output signal is of a fixed-point
data type with word length 8
, slope 80/255
,
and bias -20
.
The Data Type Conversion block reinterprets the
integer input, 127
, as a Celsius output, 19.84
degrees.
The block output uses the specified slope and bias to scale the stored
integer of the input.
Data Types | Double | Single | Boolean | Base Integer | Fixed-Point | Enumerated |
Sample Time | Inherited from driving block |
Direct Feedthrough | Yes |
Multidimensional Signals | Yes |
Variable-Size Signals | Yes |
Zero-Crossing Detection | No |
Code Generation | Yes |