Main Content

Create Fixed-Point Data in MATLAB

The following examples show how to create fixed-point data using the Fixed-Point Designer™ fi object.

Example 1. Create a fixed-point number with default properties

Calling fi on a number produces a fixed-point number with default signedness and default word and fraction lengths.

 fi(pi)
ans =
 
    3.1416

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

Example 2. Create a fixed-point number with specified signedness, word length, and fraction length

You can specify the signedness (1 for signed, 0 for unsigned) and the word and fraction lengths.

fi(pi,1,15,12)
ans =
 
    3.1416

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

 The fi and numerictype Objects

Example 3. Create fixed-point integer values

To create fixed-point integer values, specify a fraction length of 0.

fi(1:25,0,8,0)
ans =
 
  Columns 1 through 13
     1   2   3   4   5   6   7   8   9  10  11  12  13
  Columns 14 through 25
    14  15  16  17  18  19  20  21  22  23  24  25

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

Example 4. Create an array of random fixed-point values

fi(rand(4),0,12,8)
ans =
 
    0.1484    0.8125    0.1953    0.3516
    0.2578    0.2422    0.2500    0.8320
    0.8398    0.9297    0.6172    0.5859
    0.2539    0.3516    0.4727    0.5508

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

Example 5. Create an array of zeros

When writing code, you sometimes want to test different data types for your variables. Separating the data types of your variables from your algorithm makes testing much simpler. By creating a table of data type definitions, you can programmatically toggle your function between floating point and fixed point data types. The following example shows how to use this technique and create an array of zeros.

 T.z = fi([],1,16,0);

z = zeros(2,3,'like',T.z)
z = 

     0     0     0
     0     0     0

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

Note

For a full example showing this technique’s implementation, see Implement FIR Filter Algorithm for Floating-Point and Fixed-Point Types Using cast and zeros.