将 16 位整数转换为 32 位浮点数
我正在尝试将用另一种语言(一种由 Wavemetrics 开发的名为 Igor Pro 的晦涩难懂的语言,你们可能听说过它)编写的代码的一部分移植到 Python 中。
在此代码中,数据类型从 16 位整数(从 16 位大端二进制文件读取)转换为单精度(32 位)浮点。在该程序中,转换如下:
有符号 16 位整数:
print tmp
tmp[0]={-24160,18597,-24160,18597,-24160}
转换为 32 位浮点数:
Redimension/S/E=1 tmp
print tmp
tmp[0]={339213,339213,5.79801e-41,0,0}
/S
标志/选项表示 tmp 应该是 float32 而不是 int16。但是,我相信重要的标志/选项是
/E=1
,据说“强制重塑而不转换或移动数据”。
在Python中,转换如下:
>>> tmp[:5]
array([-24160, 18597, -24160, 18597, -24160], dtype=int16)
>>> tmp.astype('float32')
array([-24160., 18597., -24160., ..., 18597., -24160., 18597.], dtype=float32)
这是我所期望的,但我需要找到一个模拟上面原始代码中的/E=1
选项的函数/操作。有没有一种明显的方法可以将 -24160 和 18597 都转换为 339213 ?这与 byteswap 或 newbyteorder 或其他什么有关系吗?
I am trying to port a portion of a code written in a different language (an obscure one called Igor Pro by Wavemetrics for those of you have heard of it) to Python.
In this code, there is a conversion of a data type from a 16-bit integer (read in from a 16-bit, big endian binary file) to single-precision (32-bit) floating-point. In this program, the conversion is as follows:
Signed 16-bit integer:
print tmp
tmp[0]={-24160,18597,-24160,18597,-24160}
converted to 32-bit floating-point:
Redimension/S/E=1 tmp
print tmp
tmp[0]={339213,339213,5.79801e-41,0,0}
The /S
flag/option indicates that the data type of tmp
should be float32 instead of int16. However, I believe the important flag/option is /E=1
, which is said to "Force reshape without converting or moving data."
In Python, the conversion is as follows:
>>> tmp[:5]
array([-24160, 18597, -24160, 18597, -24160], dtype=int16)
>>> tmp.astype('float32')
array([-24160., 18597., -24160., ..., 18597., -24160., 18597.], dtype=float32)
Which is what I expect, but I need to find a function/operation that emulates the /E=1
option in the original code above. Is there an obvious way in which -24160 and 18597 would both be converted to 339213? Does this have anything to do with byteswap
or newbyteorder
or something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
结果:
我必须在值列表中添加一个零,因为值的数量为奇数。它无法将这些解释为 32 位浮点数,因为有 5 个 16 位值。
Result:
I had to add a zero to the list of value because there are an odd number of values. It cannot interpret those as 32 bit floats since there 5 16 bit values.
使用 view 而不是 astype:
.astype
创建一个副本数组的转换为新的dtype.view
返回数组的视图(具有相同的基础数据),根据新的数据类型解释数据。
Use view instead of astype:
.astype
creates a copy of the array cast to the new dtype.view
returns a view of the array (with the same underlying data),with the data interpreted according to the new dtype.
不,但也没有任何明显的方法可以将 -24160 转换为 339213 和 5.79801e-41 和 0。
看起来更像是转换需要两个 输入数字来创建一个 输出(可能通过将原始 2×16 位连接到 32 位并将结果称为浮点数)。在这种情况下,
-24160,18597
对始终变为 339213,而 5.79801e-41 可能是由-24160,0
产生的,其中 0 是因为我们用完输入而发明的。由于 5.79801e-41 看起来可能是单精度非正规,这意味着两个 16 位块可能以小端顺序连接。是否需要对每个 16 位输入进行字节交换还有待观察,但您可以自己检查。
No, but neither is there any obvious way in which -24160 would convert to 339213 and 5.79801e-41 and 0.
It looks more like the conversion takes two input numbers to create one output (probably by concatenating the raw 2×16 bits to 32 bits and calling the result a float). In that case the pair
-24160,18597
consistently becomes 339213, and 5.79801e-41 probably results from-24160,0
where the 0 is invented because we run out of inputs. Since 5.79801e-41 looks like it might be a single-precision denormal, this implies that the two 16-bit blocks are probably concatenated in little-endian order.It remains to see whether you need to byte-swap each of the 16-bit inputs, but you can check that for yourself.