定点开发
这些天我正在研究一些定点编码。
如果我有一堆来自 ADC 的 16 位样本,并且我与 16 位滤波器系数相乘,结果可能是 32 位定点数,对吗?现在没关系,因为我的目标是 32 位定点 DSP。但是,如果我想将其乘以另一个 16 位定点系数或其他系数,那么我会溢出,对吗?那么这是否意味着我需要进行中间截断?最终我还是会进行截断,因为我需要将结果发送到 16 位 DAC。
有谁有在 MATLAB 中执行此操作的经验吗?
编辑我确实有定点工具箱。我不明白的是,现在如果我设置一个 16 位字长的数字,然后将最大乘积长度设置为 16,然后将其乘以另一个 16 位字,会出现错误吗?如果我必须执行所有截断以防止错误,那么定点工具箱如何真正帮助我?我想我正在寻找一个关于如何使用定点工具箱来确保最佳舍入/溢出条件的示例,因为我的输入是 16 位并且我有 32 位寄存器。
谢谢
I'm working on some fixed point coding these days.
If I have a bunch of 16 bit samples from an ADC and I do multiplication with a 16 bit filter coefficient, the result could be a 32 bit fixed point number right? Now that's fine because I'm targeting a 32 bit fixed point DSP. However, if I want to multiply that by another 16 bit fixed point coefficient or something then I get overflow right? So does that mean I need to do intermediate truncation? Eventually I'll be truncating anyway because I need to send the result to a 16 bit DAC.
Does anyone have experience with doing this in MATLAB?
EDIT I do have fixed point toolbox. What I don't understand is that right now if I set up a number with a 16 bit word length, then set the max product length to 16, then multiply it by another 16 bit word it gives me an error? If I have to perform all the truncations to prevent an error how does the fixed point toolbox even really help me? I guess I'm looking for an example on how to use the fixed point toolbox to ensure best possible rounding/overflow conditions given that my inputs are 16 bits and I have 32 bit registers.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所指出的,16 位乘法可以得到 32 位结果。接下来,我假设您的定点表示法是 16.16。
为了执行第二次乘法,您应该首先将初始乘法结果向下移动 16 位。由于结果现在回到所需的 16.16 格式,您可以继续进行第二个 mul(“...如果我想将其乘以另一个 16 位定点系数...”)。在第二次乘法之后,将结果下移 16 位以恢复 16.16 表示法。
在将值输出 DAC 之前,我希望您需要保留定点表示法并恢复为整数形式。为此,只需将该值下移 16 位即可。在离开定点表示法之前,您可能会考虑对结果进行舍入。假设一个正定点数,这可以通过在最终右移之前向结果添加 0.5f 来实现。 (在 16.16 中,0.5f 是 2^15。)
一如既往,应该仔细研究顺序定点算术运算,以避免溢出左侧。可以对操作进行重新排序或分解以防止溢出。网络上有许多很好的教程可以帮助教程。
至于在 matlab 中执行定点数学,bitshift 函数很容易使用:参考。当然,定点工具箱使这一切变得更加容易。
As you noted, a 16-bit multiply can result in a 32-bit result. In continuing, I'm assuming you're fixed-point notation is 16.16.
In order to perform your second multiplication, you should first shift the initial mul's result back down by 16 bits. Since the result is now back into the desired 16.16 format, you may proceed with the second mul ("...if I want to multiply that by another 16 bit fixed point coefficient..."). After this second multiplication, shift the result down by 16 bits to restore the 16.16 notation.
Before shipping the value out the DAC, I would expect that you need to leave fixed-point notation and revert to integer form. To do this, simply shift the value down by 16 bits. Before leaving fixed-point notation, you might consider rounding the result. Assuming a positive fixed-point number, this can be accomplished by adding 0.5f to the result prior to the final right shift. (In 16.16, 0.5f is 2^15.)
As always, sequential fixed-point arithmetic operations should be studied closely to avoid overflowing the left hand side. The operations may be re-ordered or factored to prevent overflow. There are a number of good tutorials on the web that can help tutorial.
As for performing fixed-point math in matlab, the bitshift functions are easy enough to use: reference. Of course, the fixed-point toolbox makes this all the more easy.