Main Content

Perform Binary-Point Scaling

This example shows how to perform binary point scaling using a fi object.

Construct fi Object

Use the fi constructor, a = fi(v,s,w,f), to return a fi object with value v, signedness s, word length w, and fraction length f. If s is true (signed), the leading or most significant bit (MSB) in the resulting fi object is always the sign bit. The fraction length f gives the scaling, 2^(-f). The fraction length or the scaling determines the position of the binary point in the fi object.

For example, create a signed 8-bit fi object with a value of 0.5 and a scaling of 2^(-7).

a = fi(0.5,true,8,7)
a = 
    0.5000

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 8
        FractionLength: 7

Fraction Length Positive and Less than Word Length

When the fraction length f is positive and less than the word length, the binary point lies f places to the left of the least significant bit (LSB) and within the word.

For example, in a signed 3-bit fi with fraction length of 1 and value -0.5, the binary point lies 1 place to the left of the LSB. In this case, each bit is set to 1 and the binary equivalent of the fi with its binary point is 11.1.

The real world value of -0.5 is obtained by multiplying each bit by its scaling factor, starting with the LSB and working up to the signed MSB.

(1*2^-1) + (1*2^0) + (-1*2^1) = -0.5

storedInteger(a) returns the stored signed, unscaled integer value -1.

(1*2^0) + (1*2^1) + (-1*2^2) = -1

a = fi(-0.5,true,3,1)
a = 
   -0.5000

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 3
        FractionLength: 1
bin(a)
ans = 
'111'
storedInteger(a)
ans = int8

-1

Fraction Length Positive and Greater than Word Length

When the fraction length f is positive and greater than the word length, the binary point lies f places to the left of the LSB and outside the word.

For example the binary equivalent of a signed 3-bit word with fraction length of 4 and value of -0.0625 is ._111 Here, _ in the ._111 denotes an unused bit that is not a part of the 3-bit word. The first 1 after the _ is the MSB or the sign bit.

The real world value of -0.0625 is computed as follows (LSB to MSB).

(1*2^-4) + (1*2^-3) + (-1*2^-2) = -0.0625

b = fi(-0.0625,true,3,4)
b = 
   -0.0625

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 3
        FractionLength: 4
bin(b)
ans = 
'111'
storedInteger(b)
ans = int8

-1

Fraction Length Is Negative Integer and Less than Word Length

When the fraction length f is negative, the binary point lies f places to the right of LSB and is outside the physical word.

For instance, in c = fi(-4,true,3,-2) the binary point lies 2 places to the right of the LSB 111__.. Here, the two right most spaces are unused bits that are not part of the 3-bit word. The right most 1 is the LSB and the leading 1 is the sign bit.

The real world value of -4 is obtained by multiplying each bit by its scaling factor 2^(-f), for instance 2(-(-2)) = 2^(2) for the LSB, and then adding the products together.

(1*2^2) + (1*2^3) +(-1*2^4) = -4

c = fi(-4,true,3,-2)
c = 
    -4

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 3
        FractionLength: -2
bin(c)
ans = 
'111'
storedInteger(c)
ans = int8

-1

Fraction Length Set Automatically to the Best Precision Possible and Is Negative

Create a signed 3-bit fi where the fraction length is set automatically depending on the value that the fi is supposed to contain. The resulting fi has a value of 6, with a wordlength of 3 bits and a fraction length of -1. Here the binary point is 1 place to the right of the LSB: 011_.. The _ is again an unused bit and the first 1 before the _ is the LSB. The leading 1 is the sign bit.

The real world value of 6 is obtained as follows:

(1*2^1) + (1*2^2) + (-0*2^3) = 6

d = fi(5,true,3)
d = 
     6

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 3
        FractionLength: -1
bin(d)
ans = 
'011'
storedInteger(d)
ans = int8

3

Interactive fi Binary Point Scaling Example

To run an interactive binary-point scaling example, enter fibinscaling at the MATLAB® Command Window.

This interactive example allows you to change the fraction length of a 3-bit fixed-point number by moving the binary point using a slider. The fraction length can be varied from -3 to 5. You can change the value of the 3 bits to '0' or '1' for either signed or unsigned numbers.

%#ok<*NOPTS,*NASGU>

See Also

| |