quantize
量化定点数
语法
说明
使用 numerictype 对象进行量化
使用默认设置量化输入 y = quantize(x)x 值。
numerictype、舍入方法和溢出操作仅在量化期间适用。输出 y 没有关联的 fimath。
通过指定数值类型属性来进行量化
示例
定义要量化的输入 fi 值。
x_BP = fi(pi)
x_BP =
3.1416
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 16
FractionLength: 13
使用 numerictype 对象
创建 numerictype 对象,该对象指定一个具有 8 位字长和 4 位小数长度的有符号定点数据类型。
ntBP = numerictype(1,8,4);
使用定义的 numerictype 对象 ntBP 将输入 x_BP 量化为二进制小数点定标的定点数据类型。
yBP1 = quantize(x_BP,ntBP)
yBP1 =
3.1250
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 8
FractionLength: 4
在输入端指定数值类型属性
yBP2 = quantize(x_BP,1,8,4)
yBP2 =
3.1250
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 8
FractionLength: 4
创建一个 numerictype 对象,该对象指定一个斜率偏置定标定点数据类型。
ntSB = numerictype('Scaling','SlopeBias',... 'SlopeAdjustmentFactor',1.8,... 'Bias',1,... 'FixedExponent',-12);
定义要量化的输入 fi 值。
x_BP = fi(pi)
x_BP =
3.1416
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 16
FractionLength: 13
使用定义的 numerictype ntSB 将输入 x_BP 量化为一个斜率偏置定标的定点数据类型。
ySB1 = quantize(x_BP, ntSB)
ySB1 =
3.1415
DataTypeMode: Fixed-point: slope and bias scaling
Signedness: Signed
WordLength: 16
Slope: 0.000439453125
Bias: 1
定义要量化的输入 fi 值。
x_SB = fi(rand(5,3),numerictype('Scaling','SlopeBias','Bias',-0.125))
x_SB =
0.8147 0.0975 0.1576
0.8750 0.2785 0.8750
0.1270 0.5469 0.8750
0.8750 0.8750 0.4854
0.6324 0.8750 0.8003
DataTypeMode: Fixed-point: slope and bias scaling
Signedness: Signed
WordLength: 16
Slope: 3.0517578125e-5
Bias: -0.125
使用 numerictype 对象
创建一个 numerictype 对象 ntBP,该对象指定一个具有 8 位字长和 4 位小数长度的有符号二进制小数点定标的定点数据类型。
ntBP = numerictype(1,8,4);
使用定义的 numerictype ntBP 将输入 x_SB 量化为一个二进制小数点定标的定点数据类型。此外,舍入到最接近的值并在溢出时进行饱和处理。
yBP1 = quantize(x_SB,ntBP,'Nearest','Saturate')
yBP1 =
0.8125 0.1250 0.1875
0.8750 0.2500 0.8750
0.1250 0.5625 0.8750
0.8750 0.8750 0.5000
0.6250 0.8750 0.8125
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 8
FractionLength: 4
在输入端指定数值类型属性
yBP2 = quantize(x_SB,1,8,4,'Nearest','Saturate')
yBP2 =
0.8125 0.1250 0.1875
0.8750 0.2500 0.8750
0.1250 0.5625 0.8750
0.8750 0.8750 0.5000
0.6250 0.8750 0.8125
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 8
FractionLength: 4
定义要量化的输入 fi 值。
x_SB = fi(rand(5,3),numerictype('Scaling','SlopeBias','Bias',-0.125))
x_SB =
0.8147 0.0975 0.1576
0.8750 0.2785 0.8750
0.1270 0.5469 0.8750
0.8750 0.8750 0.4854
0.6324 0.8750 0.8003
DataTypeMode: Fixed-point: slope and bias scaling
Signedness: Signed
WordLength: 16
Slope: 3.0517578125e-5
Bias: -0.125
创建一个 numerictype 对象,该对象指定一个斜率偏置定标的定点数据类型。
ntSB = numerictype('Scaling','SlopeBias', ... 'SlopeAdjustmentFactor',1.8,'Bias',... 1,'FixedExponent',-12);
使用定义的 numerictype ntSB 将输入 x_SB 量化为一个斜率偏置定标的定点数据类型。此外,采用向上舍入。
ySB2 = quantize(x_SB,ntSB,'Ceiling')ySB2 =
0.8150 0.0978 0.1580
0.8752 0.2789 0.8752
0.1272 0.5469 0.8752
0.8752 0.8752 0.4854
0.6326 0.8752 0.8005
DataTypeMode: Fixed-point: slope and bias scaling
Signedness: Signed
WordLength: 16
Slope: 0.000439453125
Bias: 1
定义要量化的输入值。
xInt = int8(-16:4:16)
xInt = 1×9 int8 row vector
-16 -12 -8 -4 0 4 8 12 16
使用 numerictype 对象
创建一个 numerictype 对象,该对象指定一个具有 8 位字长和 4 位小数长度的有符号二进制小数点定标的定点数据类型。
ntBP = numerictype(1,8,4);
使用定义的 numerictype ntBP 将输入 xInt 量化为一个二进制小数点定标的定点数据类型。
yBP1 = quantize(xInt,ntBP,'Zero')yBP1 =
0 4 -8 -4 0 4 -8 -4 0
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 8
FractionLength: 4
显示量化输出的范围。
range(yBP1)
ans =
-8.0000 7.9375
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 8
FractionLength: 4
前两个值和最后三个值发生绕回,因为它们超出输出类型的可表示范围。
在输入端指定数值类型属性
yBP2 = quantize(xInt,1,8,4,'Zero')yBP2 =
0 4 -8 -4 0 4 -8 -4 0
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 8
FractionLength: 4
定义要量化的输入值。
xInt = int8(-16:4:16)
xInt = 1×9 int8 row vector
-16 -12 -8 -4 0 4 8 12 16
创建一个 numerictype 对象,该对象指定一个斜率偏置定标定点数据类型。
ntSB = numerictype('Scaling','SlopeBias', ... 'SlopeAdjustmentFactor',1.8,'Bias',... 1,'FixedExponent',-12);
使用定义的 numerictype ntSB 将输入 xInt 量化为一个斜率偏置定标的定点数据类型。
ySB = quantize(xInt,ntSB,'Round','Saturate')
ySB =
-13.4000 -11.9814 -7.9877 -3.9939 -0.0002 3.9936 7.9873 11.9811 15.3996
DataTypeMode: Fixed-point: slope and bias scaling
Signedness: Signed
WordLength: 16
Slope: 0.000439453125
Bias: 1
显示量化输出的范围。
range(ySB)
ans =
-13.4000 15.3996
DataTypeMode: Fixed-point: slope and bias scaling
Signedness: Signed
WordLength: 16
Slope: 0.000439453125
Bias: 1
第一个值和最后一个值进行了饱和处理,因为它们在输出类型的可表示范围的限值上。
输入参数
要量化的输入数据,指定为:
内置有符号或无符号整数
二进制小数点定标的定点
fi斜率偏置定标的定点
fi
虽然允许使用 fi 双精度值和 fi 单精度值作为输入,但它们会通过 quantize 函数而不被量化。
数据类型: int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | fi
复数支持: 是
numerictype 对象,用于描述一个定点数据类型。
用于量化的舍入方法,指定为以下值之一:
'Ceiling'- 向上舍入到下一个允许的量化值。'Convergent'- 舍入到最接近的允许的量化值。仅当舍入后的最低有效位将设置为 0 时,恰好在两个最接近的允许的量化值中间的数字才向上舍入。'Floor'- 向下舍入到下一个允许的量化值。'Nearest'- 舍入到最接近的允许的量化值。位于两个最接近的允许量化值中间的数字会向上舍入。'Round'- 舍入到最接近的允许的量化值。位于两个最接近的允许量化值中间的数字在绝对值上向上舍入。'Zero'- 将负数向上舍入到下一个允许的量化值或将正数向下舍入到下一个允许的量化值。
数据类型: char
对溢出采取的操作,指定为以下值之一:
'Saturate'- 将溢出饱和处理。当要量化数据的值超出由数值类型属性指定的最大和最小可表示数的范围时,这些值将被量化为最接近的最大或最小可表示值。
'Wrap'- 溢出将绕回。当要量化的数据的值超出由数值类型属性指定的最大和最小可表示数的范围时,使用相对于最小可表示数的模块化算术将这些值绕回到该范围内。
数据类型: char
量化的定点数的符号性,指定为 1(有符号)或 0(无符号)。
数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical
输出数据的存储整数值的字长,以位为单位。
量化值的小数长度,指定为整数标量。
数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
扩展功能
C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。
版本历史记录
在 R2006a 之前推出不推荐使用 quantize。请改用 cast、zeros、ones、eye 或 subsasgn。目前没有删除 quantize 的计划。
从 R2013a 开始,改用 cast、zeros、ones、eye 或 subsasgn。cast、zeros、ones、eye 和 subsasgn 函数除了可以量化 fi 对象外,还可以量化其他数据类型,并将量化的类型信息封装在一个对象中,而不是作为单独的输入参量。
不推荐 | 推荐 |
|---|---|
x_BP = fi(pi); ntBP = numerictype(1,8,4); yBP = quantize(x_BP,ntBP) yBP =
3.1250
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 8
FractionLength: 4 |
x_BP = fi(pi);
ntBP = fi([],1,8,4);
yBP = cast(x_BP,'like',ntBP)yBP =
3.1250
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 8
FractionLength: 4 |
另请参阅
fi | numerictype | cast | zeros
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)