Fixed-Point and Floating-Point Basics

本文介绍了数字在硬件中的定点和浮点表示,重点讨论了如何选择合适的测量尺度以优化精度和资源利用。通过固定斜率和偏置缩放,可以实现更精确的转换,同时考虑量化误差。在Matlab中,`fixdt`函数用于创建和配置定点数据类型,其参数如slope、bias等用于控制实际世界值的表示。
摘要由CSDN通过智能技术生成

1 数字表示

定点和浮点数表示
在数字硬件中,二进制数字以定点或浮点数据类型的形式来表示。了解不同数据类型是如何在硬件中定义和表示的,可以帮助您选择适合您应用程序的数据类型。

1.1 Physical Quantities and Measurement Scales

1.1.1 Introduction

Measurement Scales只是以特定的方式表示数字,这种表示通常在功耗、大小、内存使用、速度和最终产品成本方面具有优势。
选择合适的Measurement Scales,目的在于充分利用处理器,以获得最大精度。
比如,对于1cm,一般来说,你可以选择用10mm,0.01m表示. 但你也可以打破传统,选择100甚至更大的表示1cm,以提高精度。

1.1.1 Selecting a Measurement Scale

假设你想要测量液态水的温度,并用uint8进行表示测量值,幸运的是,水的温度有限0-100℃,且恰好落在了0-255的范围内。
第一种方法,是使用standard scale。即温度单位为Celsius, 0和100分别表示冰点和沸点(0℃和100℃),此时单位约为1℃ / bit,虽然此刻度给出了一个从整数到摄氏度的简单转换。但数字101-255没有使用。通过使用这个标准量表,超过60%的数字范围被浪费了。

第二种方法,是使用nonstandard scale。以0和255分别表示冰点和沸点(0℃和100℃)。此时,单位约为0.3921568℃ / bit,因此转换为摄氏度需要除以2.55,这在大多数定点处理器上是一个相对昂贵的操作。

第三种方法,是使用semistandard。以0和200分别表示冰点和沸点(0℃和100℃),此时,单位约为0.5℃ / bit,虽然201-255之间的数字没有使用,浪费超过21%,但这种比例尺允许相对容易地转换为标准比例尺。转换为摄氏度需要除以2,这在大多数处理器上是一个简单的移位操作。
在这里插入图片描述
Measurement Scales: Beyond Multiplication
从图形上看,转换是一条斜率为S的直线,且该直线通过原点。通过原点的直线称为纯线性转换。将自己局限于纯线性转换可能会造成资源的浪费,最好使用直线的一般方程:
V = S Q + B {\color{Blue}V = SQ + B } V=SQ+B
通过加偏移B,可以基于有限的bit获得更高的精度。
但所有的quantization methods,精度都是有限的,并且转换会引入误差。具有量化误差的直线的一般方程:
V = S Q + B ± E r r o r {\color{Blue}V = SQ +B ± Error } V=SQ+B±Error
如果量化值Q四舍五入到最近的可表示数字,则
− S 2 ≤ E r r o r ≤ S 2 {\color{Blue}-\frac{S}{2}\leq Error\leq \frac{S}{2}} 2SError2S
也就是说,误差是由bit个数和scale两者确定。上述是误差最小的情况。对于其他方案,误差可能是原来的两倍。

1.1.2 Select a Measurement Scale for Temperature

例程详见Mathwork官网
https://ww2.mathworks.cn/help/fixedpoint/ug/physical-quantities-and-measurement-scales.html#f21994

2.1 Slope Bias Scaling

在Simulink定标中,必须指定slope和bias,基于slope和bias scale后的real-world value可有如下公式表示:
r e a l − w o r l d    v a l u e = ( s l o p e × i n t e g e r ) + b i a s {\color{Blue}real-world\,\,value = (slope \times integer) + bias } realworldvalue=(slope×integer)+bias
s l o p e = s l o p e    a d j u s t m e n t    f a c t o r × 2 f i x e d    e x p o n e n n t {\color{Blue}slope = slope\,\,adjustment\,\,factor \times 2^{fixed\,\,exponennt}} slope=slopeadjustmentfactor×2fixedexponennt
f i x e d    e x p o n e n n t = − F r a c t i o n    l e n g t h {\color{Blue}fixed\,\,exponennt = - Fraction\,\,length} fixedexponennt=Fractionlength
Binary Point的概念如下:

在这里插入图片描述
where

  • b i b_{i} bi is the I t h I^{th} Ith binary digit.
  • w l wl wl is the word length in bits.
  • b w l − 1 b_{wl - 1} bwl1 is the location of the most significant, or highest, bit (MSB).
  • b 0 b_{0} b0 is the location of the least significant, or lowest, bit (LSB).
  • The binary point is shown four places to the left of the LSB. In this example, the number is said to have four fractional bits, or a fraction length of four.

2.2 fixdt的使用

用于创建描述定点或浮点数据类型的 Simulink.NumericType 对象
语法中涉及到的设置项见2.1,
a = fixdt(Signed,WordLength)
a = fixdt(Signed,WordLength,FractionLength)
a = fixdt(Signed,WordLength,TotalSlope,Bias)
a = fixdt(Signed,WordLength,SlopeAdjustmentFactor,FixedExponent,Bias)
a = fixdt(DataTypeNameString)
a = fixdt(___,‘DataTypeOverride’,‘Off’)
[a,IsScaledDouble] = fixdt(___)
https://ww2.mathworks.cn/help/fixedpoint/ug/scaling.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值