Main Content

fi

Construct fixed-point numeric object

Description

To assign a fixed-point data type to a number or variable, create afiobject using theficonstructor. You can specify numeric attributes and math rules in the constructor or using thenumerictypeandfimathobjects.

Creation

Description

example

a= fi返回一个fiobject with no value, 16-bit word length, and 15-bit fraction length.

example

a= fi(v)返回一个fixed-point object with valuevand default property values.

example

a= fi(v,s)返回一个fixed-point object with signedness (signed or unsigned)s.

example

a= fi(v,s,w)creates a fixed-point object with word length specified byw.

example

a= fi(v,s,w,f)creates a fixed-point object with fraction length specified byf.

example

a= fi(v,s,w,slope,bias)creates a fixed-point object using slope and bias scaling.

example

a= fi(v,s,w,slopeadjustmentfactor,fixedexponent,bias)creates a fixed-point object using slope and bias scaling.

example

a= fi(v,T)creates a fixed-point object with valuev, and numeric type properties,T.

example

a= fi(___,F)creates a fixed-point object with math settings specified byfimathobjectF.

example

a= fi(___,Name,Value)creates a fixed-point object with property values specified by one or moreName,Valuepair arguments.Namemust appear inside single quotes (''). You can specify several name-value pair arguments in any order asName1,Value1,...,NameN,ValueN.

Input Arguments

expand all

Value of thefiobject, specified as a scalar, vector, matrix, or multidimensional array.

The value of the outputfiobject is the value of the input quantized to the data type specified in theficonstructor.

You can specify the non-finite values-Inf,Inf, andNaNas the value only if you fully specify the numeric type of thefiobject. Whenfiis specified as a fixed-point numeric type,

  • NaNmaps to0.

  • When the'OverflowAction'property of thefiobject is set to'Wrap',-Inf, andInfmap to0.

  • When the'OverflowAction'property of thefiobject is set to'Saturate',Infmaps to the largest representable value, and-Infmaps to the smallest representable value.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical|fi

Signedness of thefiobject, specified as a boolean. A value of1, ortrue, indicates a signed data type. A value of0, orfalse, indicates an unsigned data type.

Data Types:logical

Word length, in bits, of thefiobject, specified as a scalar integer.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical

Fraction length, in bits, of thefiobject, specified as a scalar integer. If you do not specify a fraction length, thefiobject automatically uses the fraction length that gives the best precision while avoiding overflow for the specified value, word length, and signedness.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical

Slope of the scaling, specified as a scalar integer. The following equation represents the real-world value of a slope bias scaled number.

r e a l - w o r l d v a l u e = ( s l o p e × i n t e g e r ) + b i a s

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical

Bias of the scaling, specified as a scalar. The following equation represents the real-world value of a slope bias scaled number.

r e a l - w o r l d v a l u e = ( s l o p e × i n t e g e r ) + b i a s

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical

The slope adjustment factor of a slope bias scaled number. The following equation demonstrates the relationship between the slope, fixed exponent, and slope adjustment factor.

s l o p e = s l o p e a d j u s t m e n t f a c t o r × 2 f i x e d e x p o n e n t

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical

The fixed exponent of a slope bias scaled number. The following equation demonstrates the relationship between the slope, fixed exponent, and slope adjustment factor.

s l o p e = s l o p e a d j u s t m e n t f a c t o r × 2 f i x e d e x p o n e n t

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical

Numeric type properties of thefiobject, specified as anumerictypeobject. For more information, seenumerictype.

Fixed-point math properties of thefiobject, specified as afimathobject. For more information, seefimath.

Examples

collapse all

Create a signedfiobject with a value ofpi, a word length of eight bits, and a fraction length of 3 bits.

a = fi(pi,1,8,3)
a = 3.1250 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 8 FractionLength: 3

Create an array offiobjects with 16-bit word length and 12-bit fraction length.

a = fi((magic(3)/10), 1, 16, 12)
a=3×3 object0.8000 0.1001 0.6001 0.3000 0.5000 0.7000 0.3999 0.8999 0.2000 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 12

When you specify only the value and the signedness of thefiobject, the word length defaults to 16 bits, and the fraction length is set to achieve the best precision possible without overflow.

a = fi(pi, 1)
a = 3.1416 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 13

If you do not specify a fraction length, input argumentf, the fraction length of thefiobject defaults to the fraction length that offers the best precision.

a = fi(pi,1,8)
a = 3.1562 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 8 FractionLength: 5

The fraction length offiobjectais five because three bits are required to represent the integer portion of the value when the data type is signed. If thefiobject uses an unsigned data type, only two bits are needed to represent the integer portion, leaving six fractional bits.

b = fi(pi,0,8)
b = 3.1406 DataTypeMode: Fixed-point: binary point scaling Signedness: Unsigned WordLength: 8 FractionLength: 6

The real-world value of a slope bias scaled number is represented by:

real world value = ( slope × integer ) + bias

To create afiobject that uses slope and bias scaling, include theslopeandbiasarguments after the word length in the constructor.

a = fi(pi, 1, 16, 3, 2)
a = 2 DataTypeMode: Fixed-point: slope and bias scaling Signedness: Signed WordLength: 16 Slope: 3 Bias: 2

TheDataTypeModeproperty of thefiobject,a, isslope and bias scaling.

When the value input argument,v, of afiobject is a non-double, and you do not specify the word length or fraction length properties, the resultingfiobject retains the numeric type of the input,v.

Create afiobject from a built-in integer

When the input is a built-in integer, the fixed-point attributes match the attributes of the integer type.

v1 = uint32(5); a1 = fi(v1)
a1 = 5 DataTypeMode: Fixed-point: binary point scaling Signedness: Unsigned WordLength: 32 FractionLength: 0
v2 = int8(5); a2 = fi(v2)
a2 = 5 DataTypeMode:定点:二进制scaling Signedness: Signed WordLength: 8 FractionLength: 0

Create afiobject from afiobject

When the input value is afi对象,输出使用相同的字长,压裂tion length, and signedness of the inputfiobject.

v = fi(pi, 1, 24, 12); a = fi(v)
a = 3.1416 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 24 FractionLength: 12

Create afiobject from a logical

When the inputvis logical, theDataTypeModeproperty of the outputfiobject isBoolean.

v = true; a = fi(v)
a = 1 DataTypeMode: Boolean

Create afiobject from a single

When the input is single, theDataTypeModeproperty of the output isSingle.

v = single(pi); a = fi(v)
a = 3.1416 DataTypeMode: Single

The arithmetic attributes of afiobject are defined by afimathobject which is attached to thatfiobject.

Create afimathobject and specify theOverflowAction,RoundingMethod, andProductModeproperties.

F = fimath('OverflowAction','Wrap','RoundingMethod','Floor','ProductMode','KeepMSB')
F = RoundingMethod: Floor OverflowAction: Wrap ProductMode: KeepMSB ProductWordLength: 32 SumMode: FullPrecision

Create afiobject and specify thefimathobject,F, in the constructor.

a = fi(pi, F)
a = 3.1415 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 13 RoundingMethod: Floor OverflowAction: Wrap ProductMode: KeepMSB ProductWordLength: 32 SumMode: FullPrecision

Use theremovefimathfunction to remove the associatedfimathobject and restore the math settings to their default values.

a = removefimath(a)
a = 3.1415 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 13

Anumerictypeobject contains all of the data type information of afiobject. By transitivity,numerictypeproperties are also properties offiobjects.

You can create afiobject that uses all of the properties of an existingnumerictypeobject by specifying thenumerictypeobject in theficonstructor.

T = numerictype(0,24,16)
T = DataTypeMode: Fixed-point: binary point scaling Signedness: Unsigned WordLength: 24 FractionLength: 16
a = fi(pi, T)
a = 3.1416 DataTypeMode: Fixed-point: binary point scaling Signedness: Unsigned WordLength: 24 FractionLength: 16

When you use binary-point representation for a fixed-point number, the fraction length can be greater than the word length. In this case, there are implicit leading zeros (for positive numbers) or ones (for negative numbers) between the binary point and the first significant binary digit.

Consider a signed value with a word length of 8, fraction length of 10, and a stored integer value of 5. Calculate the real-world value using the following equation.

real world value = stored integer × 2 - fraction length

realWorldValue = 5*2^(-10)
realWorldValue = 0.0049

Create a signedfiobject with valuerealWorldValue, a word length of 8 bits, and a fraction length of 10 bits.

a = fi(realWorldValue, 1, 8, 10)
a = 0.0049 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 8 FractionLength: 10

Get the stored integer value ofausing theintfunction.

int(a)
ans =int85

Use thebinfunction to view the stored integer value in binary.

bin(a)
ans = '00000101'

Because the fraction length is two bits longer than the word length, the binary value of the stored integer isX.XX00000101, whereXis a placeholder for implicit zeroes. 0.0000000101 (binary) is equivalent to 0.0049 (decimal).

When you use binary-point representation for a fixed-point number, the fraction length can be negative. In this case, there are implicit trailing zeros (for positive numbers) or ones (for negative numbers) between the binary point and the first significant binary digit.

Consider a signed data type with a word length of 8, fraction length of -2 and a stored integer value of 5. Calculate the stored integer value using the following equation.

real world value = stored integer × 2 - fraction length

realWorldValue = 5*2^(2)
realWorldValue = 20

Create a signedfiobject with valuerealWorldValue, a word length of 8 bits, and a fraction length of -2 bits.

a = fi(realWorldValue, 1, 8, -2)
= 20 DataTypeMode:定点:二进制scaling Signedness: Signed WordLength: 8 FractionLength: -2

Get the stored integer value ofausing theintfunction.

int(a)
ans =int85

Get the binary value ofausing thebinfunction.

bin(a)
ans = '00000101'

Because the fraction length is negative, the binary value of the stored integer is00000101XX, whereXis a placeholder for implicit zeros. 0000010100 (binary) is equivalent to 20 (decimal).

You can set math properties, such as rounding and overflow modes during the creation of thefiobject.

a = fi(pi,'RoundingMethod','Floor','OverflowAction','Wrap')
a = 3.1415 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 13 RoundingMethod: Floor OverflowAction: Wrap ProductMode: FullPrecision SumMode: FullPrecision

TheRoundingMethodandOverflowActionproperties are properties of thefimathobject. Specifying these properties in theficonstructor associates a localfimathobject with thefiobject.

Use theremovefimathfunction to remove the localfimathand set the math properties back to their default values.

a = removefimath(a)
a = 3.1415 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 13

When using afiobject as an index, the value of thefiobject must be an integer.

Set up an array to index into.

x = 10:-1:1;

Create an integer valuedfiobject and use it to index intox.

a = fi(3); y = x(a)
y = 8

Usefias the index in aforloop

Createfiobjects to use as the index of a for loop. The values of the indices must be integers.

a = fi(1, 0, 8, 0); b = fi(2, 0, 8, 0); c = fi(10, 0, 8, 0);forx = a:b:c xend
x = 1 DataTypeMode: Fixed-point: binary point scaling Signedness: Unsigned WordLength: 8 FractionLength: 0
x = 3 DataTypeMode: Fixed-point: binary point scaling Signedness: Unsigned WordLength: 8 FractionLength: 0
x = 5 DataTypeMode: Fixed-point: binary point scaling Signedness: Unsigned WordLength: 8 FractionLength: 0
x = 7 DataTypeMode: Fixed-point: binary point scaling Signedness: Unsigned WordLength: 8 FractionLength: 0
x = 9 DataTypeMode: Fixed-point: binary point scaling Signedness: Unsigned WordLength: 8 FractionLength: 0

Thefiprefobject defines the display and logging attributes for allfiobjects. Use theDataTypeOverridesetting of thefiprefobject to overridefiobjects with doubles, singles, or scaled doubles.

Save the currentfiprefsettings to restore later.

fp = fipref; initialDTO = fp.DataTypeOverride;

Create afiobject with the default settings and originalfiprefsettings.

a = fi(pi)
a = 3.1416 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 13

Turn on data type override to doubles and create a newfiobject without specifying itsDataTypeOverrideproperty so that it uses the data type override settings specified usingfipref.

fipref('DataTypeOVerride','TrueDoubles')
ans = NumberDisplay: 'RealWorldValue' NumericTypeDisplay: 'full' FimathDisplay: 'full' LoggingMode: 'Off' DataTypeOverride: 'TrueDoubles' DataTypeOverrideAppliesTo: 'AllNumericTypes'
a = fi(pi)
a = 3.1416 DataTypeMode: Double

Now create afiobject and set itsDataTypeOverridesetting tooffso that it ignores the data type override settings of thefiprefobject.

b = fi(pi,'DataTypeOverride','Off')
b = 3.1416 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 13

Restore the fipref settings saved at the start of the example.

fp.DataTypeOverride = initialDTO;

To use the non-numeric values-Inf,Inf, andNaNas fixed-point values withfi, you must fully specify the numeric type of the fixed-point object. Automatic best-precision scaling is not supported for these values.

Saturate on Overflow

When the numeric type of thefiobject is specified to saturate on overflow, thenInfmaps to the largest representable value of the specified numeric type, and-Infmaps to the smallest representable value.NaNmaps to zero.

x = [-inf nan inf]; a = fi(x,1,8,0,'OverflowAction','Saturate') b = fi(x,0,8,0,'OverflowAction','Saturate')
a = -128 0 127 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 8 FractionLength: 0 RoundingMethod: Nearest OverflowAction: Saturate ProductMode: FullPrecision SumMode: FullPrecision b = 0 0 255 DataTypeMode: Fixed-point: binary point scaling Signedness: Unsigned WordLength: 8 FractionLength: 0 RoundingMethod: Nearest OverflowAction: Saturate ProductMode: FullPrecision SumMode: FullPrecision

Wrap on Overflow

When the numeric type of thefiobject is specified to wrap on overflow, then-Inf,Inf, andNaNmap to zero.

x = [-inf nan inf]; a = fi(x,1,8,0,'OverflowAction','Wrap') b = fi(x,0,8,0,'OverflowAction','Wrap')
a = 0 0 0 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 8 FractionLength: 0 RoundingMethod: Nearest OverflowAction: Wrap ProductMode: FullPrecision SumMode: FullPrecision b = 0 0 0 DataTypeMode: Fixed-point: binary point scaling Signedness: Unsigned WordLength: 8 FractionLength: 0 RoundingMethod: Nearest OverflowAction: Wrap ProductMode: FullPrecision SumMode: FullPrecision

Compatibility Considerations

expand all

Behavior changed in R2020b

Extended Capabilities

HDL Code Generation
Generate Verilog and VHDL code for FPGA and ASIC designs using HDL Coder™.

Introduced in R2006a