Main Content

MATLAB 中创建定点数据

以下示例说明如何使用 Fixed-Point Designer™ fi 对象创建定点数据。

例 1. 使用默认属性创建定点数

对数字调用 fi 会生成具有默认符号性、默认字长和小数长度的定点数。

 fi(pi)
ans =
 
    3.1416

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

例 2. 创建具有指定符号性、字长和小数长度的定点数

您可以指定符号性(1 表示有符号,0 表示无符号)以及字长和小数长度。

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

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

 finumerictype 对象

例 3. 创建定点整数值

要创建定点整数值,请指定小数长度为 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

例 4. 创建随机定点值的数组

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

例 5. 创建全零数组

编写代码时,您有时需要为变量测试不同数据类型。将变量的数据类型与算法分离使测试变得更加简单。通过创建数据类型定义表,您可以编程方式使函数在浮点数据类型和定点数据类型之间切换。以下示例说明如何使用此方法以及如何创建全零数组。

 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

注意

有关说明此方法的实现的完整示例,请参阅Implement FIR Filter Algorithm for Floating-Point and Fixed-Point Types Using cast and zeros