Skip to Main Content Skip to Search
Product Documentation

Constructing fi Objects

fi Object Syntaxes

You can create fi objects using Fixed-Point Toolbox software in any of the following ways:

To get started, type

a = fi(0)

to create a fi object with the default data type and a value of 0.

a =
 
     0


          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 15

This constructor syntax creates a signed fi object with a value of 0, word length of 16 bits, and fraction length of 15 bits. Because you did not specify any fimath object properties in the fi constructor, the resulting fi object a associates itself with the global fimath.

To see all of the fi, sfi, and ufi constructor syntaxes, refer to the respective reference pages.

Examples of Constructing fi Objects

The following examples show you several different ways to construct fi objects. For other, more basic examples of constructing fi objects, see the Examples section of the following constructor function reference pages:

Constructing a fi Object with Property Name/Property Value Pairs

You can use property name/property value pairs to set fi and fimath object properties when you create the fi object:

a = fi(pi, 'roundmode', 'floor', 'overflowmode', 'wrap')
 
a =
 
    3.1415

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 13

             RoundMode: floor
          OverflowMode: wrap
           ProductMode: FullPrecision
  MaxProductWordLength: 128
               SumMode: FullPrecision
      MaxSumWordLength: 128

You do not have to specify every fimath object property in the fi constructor. The fi object inherits all unspecified fimath object properties from the global fimath.

Constructing a fi Object Using a numerictype Object

You can use a numerictype object to define a fi object:

T = numerictype
 
T =
 

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 15

a = fi(pi, T)
 
a =
 
    1.0000


          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 15

You can also use a fimath object with a numerictype object to define a fi object:

F = fimath('RoundMode', 'nearest',...
'OverflowMode', 'saturate',...
'ProductMode','FullPrecision',...
'MaxProductWordLength', 128,...
'SumMode','FullPrecision',...
'MaxSumWordLength', 128) 

F =
 
             RoundMode: nearest
          OverflowMode: saturate
           ProductMode: FullPrecision
  MaxProductWordLength: 128
               SumMode: FullPrecision
      MaxSumWordLength: 128

a = fi(pi, T, F)
 
a =
 
    1.0000


          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 15

             RoundMode: nearest
          OverflowMode: saturate
           ProductMode: FullPrecision
  MaxProductWordLength: 128
               SumMode: FullPrecision
      MaxSumWordLength: 128

Constructing a fi Object Using a fimath Object

You can create a fi object using a specific fimath object. When you do so, a local fimath object is assigned to the fi object you create. If you do not specify any numerictype object properties, the word length of the fi object defaults to 16 bits. The fraction length is determined by best precision scaling:

F = fimath('RoundMode', 'nearest',...
'OverflowMode', 'saturate',...
'ProductMode','FullPrecision',...
'MaxProductWordLength', 128,...
'SumMode','FullPrecision',...
'MaxSumWordLength', 128) 
 
F =
 

             RoundMode: nearest
          OverflowMode: saturate
           ProductMode: FullPrecision
  MaxProductWordLength: 128
               SumMode: FullPrecision
      MaxSumWordLength: 128

F.OverflowMode = 'wrap'
 
F =
 

             RoundMode: nearest
          OverflowMode: wrap
           ProductMode: FullPrecision
  MaxProductWordLength: 128
               SumMode: FullPrecision
      MaxSumWordLength: 128

a = fi(pi, F)
 
a =
 
    3.1416

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 13

             RoundMode: nearest
          OverflowMode: wrap
           ProductMode: FullPrecision
  MaxProductWordLength: 128
               SumMode: FullPrecision
      MaxSumWordLength: 128

You can also create fi objects using a fimath object while specifying various numerictype properties at creation time:

b = fi(pi, 0, F)
 
b =
 
    3.1416

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Unsigned
            WordLength: 16
        FractionLength: 14

             RoundMode: nearest
          OverflowMode: wrap
           ProductMode: FullPrecision
  MaxProductWordLength: 128
               SumMode: FullPrecision
      MaxSumWordLength: 128

c = fi(pi, 0, 8, F)
 
c =
 
    3.1406

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Unsigned
            WordLength: 8
        FractionLength: 6

             RoundMode: nearest
          OverflowMode: wrap
           ProductMode: FullPrecision
  MaxProductWordLength: 128
               SumMode: FullPrecision
      MaxSumWordLength: 128

d = fi(pi, 0, 8, 6, F)
 
d =
 
    3.1406

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Unsigned
            WordLength: 8
        FractionLength: 6

             RoundMode: nearest
          OverflowMode: wrap
           ProductMode: FullPrecision
  MaxProductWordLength: 128
               SumMode: FullPrecision
      MaxSumWordLength: 128

Building fi Object Constructors in a GUI

When you are working with files in MATLAB, you can build your fi object constructors using the Insert fi Constructor dialog box. After specifying the value and properties of the fi object in the dialog box, you can insert the prepopulated fi object constructor string at a specific location in your file.

For example, to create a signed fi object with a value of pi, a word length of 16 bits and a fraction length of 13 bits, perform the following steps:

  1. Open the Insert fi Constructor dialog box by selecting Tools > Fixed-Point Toolbox > Insert fi Constructor from the editor menu.

  2. Use the edit boxes and drop-down menus to specify the following properties of the fi object:

    • Value = pi

    • Data type mode = Fixed-point: binary point scaling

    • Signedness = Signed

    • Word length = 16

    • Fraction length = 13

  3. To insert the fi object constructor string in your file, place your cursor at the desired location in the file, and click OK on the Insert fi Constructor dialog box. Clicking OK closes the Insert fi Constructor dialog box and automatically populates the fi object constructor string in your file:

Determining Property Precedence

The value of a property is taken from the last time it is set. For example, create a numerictype object with a value of true for the Signed property and a fraction length of 14:

T = numerictype('Signed', true, 'FractionLength', 14)
 
T =
 

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 14        

Now, create the following fi object in which you specify the numerictype property after the Signed property, so that the resulting fi object is signed:

a = fi(pi,'Signed',false,'numerictype',T)
 
a =
 
    1.9999

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 14

Contrast the fi object in this code sample with the fi object in the following code sample. The numerictype property in the following code sample is specified before the Signed property, so the resulting fi object is unsigned:

b = fi(pi,'numerictype',T,'Signed',false)
 
b =
 
    3.1416

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Unsigned
            WordLength: 16
        FractionLength: 14

Copying a fi Object

To copy a fi object, simply use assignment, as in the following example:

a = fi(pi)
 
a =
 
    3.1416

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 13
b = a
 
b =
 
    3.1416

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 13
  


Free Early Verification Kit

Learn how to apply early verification to your development process through these technical resources.

How much time do you spend on testing to ensure implementation meets system-level requirements?

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS