fimath Properties Usage for Fixed-Point Arithmetic
fimath Rules for Fixed-Point Arithmetic
fimath
properties define the rules for performing arithmetic
operations on fi
objects. The fimath
properties that govern fixed-point arithmetic operations can come from a local
fimath
object or the fimath
default
values.
To determine whether a fi
object has a local
fimath
object, use the isfimathlocal
function.
The following sections discuss how fi
objects with local
fimath
objects interact with fi
objects
without local fimath.
Binary Operations
In binary fixed-point operations such as c = a + b
, the
following rules apply:
If both
a
andb
have no local fimath, the operation uses default fimath values to perform the fixed-point arithmetic. The outputfi
objectc
also has no local fimath.If either
a
orb
has a localfimath
object, the operation uses thatfimath
object to perform the fixed-point arithmetic. The outputfi
objectc
has the same localfimath
object as the input.
Unary Operations
In unary fixed-point operations such as b = abs(a)
, the
following rules apply:
If
a
has no local fimath, the operation uses default fimath values to perform the fixed-point arithmetic. The outputfi
objectb
has no local fimath.If
a
has a localfimath
object, the operation uses thatfimath
object to perform the fixed-point arithmetic. The outputfi
objectb
has the same localfimath
object as the inputa
.
When you specify a fimath
object in the function call of a
unary fixed-point operation, the operation uses the fimath
object you specify to perform the fixed-point arithmetic. For example, when you
use a syntax such as b = abs(a,F)
or b =
sqrt(a,F)
, the abs
and sqrt
operations use the
fimath
object F
to compute
intermediate quantities. The output fi
object
b
always has no local fimath.
Concatenation Operations
In fixed-point concatenation operations such as c = [a b]
,
c = [a;b]
and c = bitconcat(a,b)
, the
following rule applies:
The
fimath
properties of the leftmostfi
object in the operation determine thefimath
properties of the outputfi
objectc
.
For example, consider the following scenarios for the operation d =
[a b c]
:
If
a
is afi
object with no local fimath, the outputfi
objectd
also has no local fimath.If
a
has a localfimath
object, the outputfi
objectd
has the same localfimath
object.If
a
is not afi
object, the outputfi
objectd
inherits thefimath
properties of the next leftmostfi
object. For example, ifb
is afi
object with a localfimath
object, the outputfi
objectd
has the same localfimath
object as the inputfi
objectb
.
fimath Object Operations: add, mpy, sub
The output of the fimath
object operations add
, mpy
, and sub
always have no local
fimath. The operations use the fimath
object you specify in
the function call, but the output fi
object never has a local
fimath
object.
MATLAB Function Block Operations
Fixed-point operations performed with the MATLAB Function block use the same rules as fixed-point operations performed in MATLAB®.
All input signals to the MATLAB Function block that you treat as
fi
objects associate with whatever you specify for the
MATLAB Function block fimath parameter. When you set
this parameter to Same as MATLAB
, your
fi
objects do not have local fimath. When you set the
MATLAB Function block fimath parameter to
Specify other
, you can define your own set of
fimath
properties for all fi
objects
in the MATLAB Function block to associate with. You can choose to treat only
fixed-point input signals as fi
objects or both fixed-point
and integer input signals as fi
objects. See Use fimath Objects in MATLAB Function Blocks.
Binary-Point Arithmetic
The fimath
object encapsulates the math properties of
Fixed-Point Designer™ software.
fi
objects only have a local fimath
object
when you explicitly specify fimath
properties in the
fi
constructor. When you use the sfi
or
ufi
constructor or do not specify any
fimath
properties in the fi
constructor,
the resulting fi
object does not have any local fimath and uses
default fimath values.
a = fi(pi)
a = 3.1416 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 13
a.fimath isfimathlocal(a)
ans = RoundingMethod: Nearest OverflowAction: Saturate ProductMode: FullPrecision SumMode: FullPrecision ans = logical 0
To perform arithmetic with +
, -
,
.*
, or *
on two fi
operands with local fimath
objects, the local
fimath
objects must be identical. If one of the
fi
operands does not have a local fimath
,
the fimath
properties of the two operands need not be identical.
See fimath Rules for Fixed-Point Arithmetic for more information.
a = fi(pi); b = fi(8); isequal(a.fimath, b.fimath)
ans = logical 1
a + b
ans = 11.1416 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 19 FractionLength: 13
To perform arithmetic with +
, -
,
.*
, or *
, two fi
operands must also have the same data type. For example, you can add two
fi
objects with data type double
, but you
cannot add an object with data type double
and one with data type
single
:
a = fi(3, 'DataType', 'double')
a = 3 DataTypeMode: Double
b = fi(27, 'DataType', 'double')
b = 27 DataTypeMode: Double
a + b
ans = 30 DataTypeMode: Double
c = fi(12, 'DataType', 'single')
c = 12 DataTypeMode: Single
a + c
Error using + (line 24) Math operations are not allowed on fi objects with different data types.
Fixed-point fi
object operands do not have to have the same
scaling. You can perform binary math operations on a fi
object
with a fixed-point data type and a fi
object with a scaled
doubles data type. In this sense, the scaled double data type acts as a fixed-point
data type:
a = fi(pi)
a = 3.1416 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 13
b = fi(magic(2), ... 'DataTypeMode', 'Scaled double: binary point scaling')
b = 1 3 4 2 DataTypeMode: Scaled double: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 12
a + b
ans = 4.1416 6.1416 7.1416 5.1416 DataTypeMode: Scaled double: binary point scaling Signedness: Signed WordLength: 18 FractionLength: 13
Use the divide
function to perform
division with doubles, singles, or binary point-only scaling fi
objects.
[Slope Bias] Arithmetic
Fixed-Point Designer software supports fixed-point arithmetic using the local
fimath
object or default fimath for all binary point-only
signals. The toolbox also supports arithmetic for [Slope Bias] signals with the
following restrictions:
[Slope Bias] signals must be real.
You must set the
SumMode
andProductMode
properties of the governingfimath
to'SpecifyPrecision'
for sum and multiply operations, respectively.You must set the
CastBeforeSum
property of the governingfimath
to'true'
.Fixed-Point Designer does not support the
divide
function for [Slope Bias] signals.
f = fimath('SumMode', 'SpecifyPrecision', ... 'SumFractionLength', 16)
f = RoundingMethod: Nearest OverflowAction: Saturate ProductMode: FullPrecision SumMode: SpecifyPrecision SumWordLength: 32 SumFractionLength: 16 CastBeforeSum: true
a = fi(pi, 'fimath', f)
a = 3.1416 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 13 RoundingMethod: Nearest OverflowAction: Saturate ProductMode: FullPrecision SumMode: SpecifyPrecision SumWordLength: 32 SumFractionLength: 16 CastBeforeSum: true
b = fi(22, true, 16, 2^-8, 3, 'fimath', f)
b = 22 DataTypeMode: Fixed-point: slope and bias scaling Signedness: Signed WordLength: 16 Slope: 0.00390625 Bias: 3 RoundingMethod: Nearest OverflowAction: Saturate ProductMode: FullPrecision SumMode: SpecifyPrecision SumWordLength: 32 SumFractionLength: 16 CastBeforeSum: true
a + b
ans = 25.1416 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 32 FractionLength: 16 RoundingMethod: Nearest OverflowAction: Saturate ProductMode: FullPrecision SumMode: SpecifyPrecision SumWordLength: 32 SumFractionLength: 16 CastBeforeSum: true
Setting the SumMode
and ProductMode
properties to SpecifyPrecision
are mutually exclusive except when
performing the *
operation between matrices. In this case, you
must set both the SumMode
and ProductMode
properties to SpecifyPrecision
for [Slope Bias] signals. Doing so
is necessary because the *
operation performs both sum and
multiply operations to calculate the result.