Matlab~fixed-point guide(1)

matlab支持定点计算,官方提供定点数转换工具箱,并在文档有详细教程,主要流程为:

下面将按步骤一一详细描述:


在matlab中创建定点数据

一般的,可以使用fixed-point designer fi对象创建数据。较常使用的为:

a=fi(v,s,w,f);

其中v为指定生成对象的值,s为有无符号,为1时表示有符号数,w为字长,f为小数长度。

除此之外,fi对象还有:

a=fi(v,T);
a=fi(_,Name,Value);

第一个用法,指定对象数据类型和T一样;第二个指定一些属性值,包括溢出模式OverflowAction、舍入模式RoundingMode、乘法模式ProductMode、求和模式SumMode等(这一部分与fimath有关,在算术部分详细展开)。关于fi对象部分,参考文档Construct fixed-point numeric object - MATLAB

除了fi对象,一些常见的matlab算法也支持定点数生成创建。举个例子:

X = zeros('like',p);
X = zeros(n,'like',p);

X = ones('like',p);
X = ones(n,'like',p);

两个函数的第一个是生成标量,第二个是生成n维矩阵。用到的关键的地方就是这个'like', p,意思是生成的数据类型和p一样。或许创建变量型的函数大多支持生成浮点数,方便先用fi生成想要的定点数模板变量,然后批量生成定点数变量。更多有关定点数的函数可以参考Reference List- MATLAB & Simulink


执行定点算术

在运算这一方面,主要关注精度差异,在默认情况下的计算可以总结为:

fi类型之间:

加减法:同定标数相加,可能有1进位;不同定标数加减,结果和操作数差异多于1位(还是进位多)。

乘法:结果字长和小数长度为操作数对应属性之和。

fi与double之间:

加法:fi。

乘法:fi,double转换为fi,字长符号性相同,小数长度最佳精度。

 matlab用位增长描述默认fimath值下的运算情况:

 位增长导致了在循环中会出现:内存溢出;数据类型改变(导致不支持生成code)。控制位增长的方法有:

下标赋值:通过下标传递值,不更改被赋值的数据类型。

特殊运算函数:accumpos/accumneg。

fimath指定属性:规定数据的数学属性。

 下标赋值类似于C语言的a[i]+=b,得到的结果a[i]数据类型不改变,更多的可以去看示例。

两个运算的东西accumpos和accumneg能够指定属性RoundingMethod和OverflowAction。具体模式有:

Rounding method to use, specified as one of these values:

  • 'Nearest' – Round toward nearest. Ties round toward positive infinity.

  • 'Ceiling' – Round toward positive infinity.

  • 'Convergent' – Round toward nearest. Ties round to the nearest even stored integer (least biased).

  • 'Zero' – Round toward zero.

  • 'Floor' – Round toward negative infinity.

  • 'Round' – Round toward nearest. Ties round toward negative infinity for negative numbers, and toward positive infinity for positive numbers.

Action to take on overflow, specified as one of these values:

  • 'Saturate' – Saturate to the maximum or minimum value of the fixed-point range on overflow.

  • 'Wrap' – Wrap on overflow. This mode is also known as two's complement overflow.

 舍入方式划分比较细,看需要选择,溢出的话一般是饱和。

fimath指定数学属性是比较重要的东西,多花点篇幅说一下。通过fimath可以指定如下属性:

'CastBeforeSum' — 加法之前是否将两个操作数都转换为和数据类型
false or 0 (default) | true or 1

'MaxProductWordLength' — 乘法数据类型的最大允许字长
65535 (default) | positive integer

'MaxSumWordLength' — 和数据类型的最大允许字长
65535 (default) | positive integer

'OverflowAction' — 溢出操作,默认饱和
'Saturate' (default) | 'Wrap'

'ProductBias' —乘法数据类型偏差
0 (default) | floating-point number

'ProductFixedExponent' —乘法数据类型的定点指数(翻译不确定)
-30 (default) | nonzero integer

'ProductFractionLength' — 乘法数据类型的小数长度
30 (default) | nonzero integer

上面两者互为正负。

'ProductMode' — 怎么确定乘法数据类型,在这里使用SpecifyPrecision可以指定精度
'FullPrecision' (default) | 'KeepLSB' | 'KeepMSB' | 'SpecifyPrecision'

'ProductSlope' — 乘法数据类型的斜率
9.3132e-10 (default) | finite, positive floating-point number

'ProductSlopeAdjustmentFactor' — 乘法数据类型的斜率调整系数
1 (default) | floating-point number greater than or equal to 1 and less than 2

'ProductWordLength' — 乘法数据类型的字长
32 (default) | positive integer

'RoundingMethod' — 舍入方法
'Nearest' (default) | 'Ceiling' | 'Convergent' | 'Zero' | 'Floor' | 'Round'

'SumBias' — 求和数据误差
0 (default) | floating-point number

'SumFixedExponent' — 求和数据类型的定点指数
-30 (default) | nonzero integer

'SumFractionLength' — 求和数据类型的小数长度
30 (default) | nonzero integer

上面两者互为正负。

'SumMode' — 求和模式,同样可以指定精度
'FullPrecision' (default) | 'KeepLSB' | 'KeepMSB' | 'SpecifyPrecision'

'SumSlope' — 求和数据类型的斜率
9.3132e-10 (default) | floating-point number

'SumSlopeAdjustmentFactor' — 求和数据类型的斜率偏置系数
1 (default) | floating-point number greater than or equal to 1 and less than 2

'SumWordLength' — 求和数据类型的字长
32 (default) | positive integer

一般来说,想要运算过程中数据的统一性,可以将求和乘法模式等设置好 ,然后指定字长、小数长度。假如对舍入方法和溢出模式有要求,对具体的损失精度有要求,都可以用fimath设置。

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值