Set fi Object Properties - MATLAB & Simulink (original) (raw)

Set fi object properties when you create the fi object.

Set Fixed-Point Properties at Object Creation

You can set properties of fi objects at the time of object creation by including properties after the arguments of the fi constructor function. For example, to set the overflow action toWrap and the rounding method to Convergent in the fi constructor.

a = fi(pi,'OverflowAction','Wrap',... 'RoundingMethod','Convergent')

a =

3.1416

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

    RoundingMethod: Convergent
    OverflowAction: Wrap
       ProductMode: FullPrecision
           SumMode: FullPrecision

To set the stored integer value of a fi object, use the parameter name-value pair arguments for the 'int' property when you create the object. For example, create a signed fi object with a stored integer value of 4, 16-bit word length, and 15-bit fraction length.

x = fi(0,1,16,15,'int',4);

Verify that the fi object has the expected integer setting.

Use Subscripted Assignment to Set Real-World Value of fi Object

You can set the real-world value of a fi object via subscripted assignment.

a =

 2

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

Direct Property Referencing to Read fi Object Properties

You can read fi object property values using MATLABĀ® structure-like referencing. You do this by using a period to index into a property by name.

For example, to get the WordLength ofa.

Best Practices for Code Generation

The following methods for setting fi object properties are recommended for compatibility with code generation.

First, define the fi object a.

a = fi(pi,'OverflowAction','Wrap',... 'RoundingMethod','Convergent')

a =

3.1416

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

    RoundingMethod: Convergent
    OverflowAction: Wrap
       ProductMode: FullPrecision
           SumMode: FullPrecision

You can get the fimath using dot indexing, use thefimath constructor to change the fimath settings, then use setfimath to set the localfimath object back into fi objecta.

F = fimath(a.fimath,'OverflowAction','Saturate'); a = setfimath(a,F)

a =

3.1416

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

    RoundingMethod: Convergent
    OverflowAction: Saturate
       ProductMode: FullPrecision
           SumMode: FullPrecision

The setfimath function is useful for changing out thefimath altogether. For example:

a = fi(pi); F = fixed.fimathLike(a); a = setfimath(a,F)

a =

3.1416

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

    RoundingMethod: Floor
    OverflowAction: Wrap
       ProductMode: SpecifyPrecision
 ProductWordLength: 16

ProductFractionLength: 13 SumMode: SpecifyPrecision SumWordLength: 16 SumFractionLength: 13 CastBeforeSum: true

Alternatively, you can call the fi object constructor with the value input set to the original fi object, then add newfimath parameters directly in the fi object constructor. For example:

a = fi(pi,'OverflowAction','Wrap',... 'RoundingMethod','Convergent')

a =

3.1416

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

    RoundingMethod: Convergent
    OverflowAction: Wrap
       ProductMode: FullPrecision
           SumMode: FullPrecision

a = fi(a,'OverflowAction','Saturate')

a =

3.1416

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

    RoundingMethod: Convergent
    OverflowAction: Saturate
       ProductMode: FullPrecision
           SumMode: FullPrecision

Note that using dot indexing to write fimath andnumerictype object properties is not compatible with code generation. For example:

a.OverflowAction = 'Saturate' % Works in interpreted MATLAB only

Remove Local fimath Properties from fi Object

If you have a fi object b with a localfimath object, you can remove the localfimath object and force b to use defaultfimath values.

b = fi(pi,1,'RoundingMethod','Floor')

b =

3.1415

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

    RoundingMethod: Floor
    OverflowAction: Saturate
       ProductMode: FullPrecision
           SumMode: FullPrecision

b =

3.1415

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

See Also

fi | fimath | numerictype | fi Properties | fimath Properties | numerictype Properties