What's the fastest way to convert an interleaved NumPy integer array to complex64?

 ̄綄美尐妖づ 提交于 2019-12-01 15:08:02

问题


I have a stream of incoming data that has interleaved real and imaginary integers. Converting these to complex64 values is the slowest operation in my program. This is my current approach:

import numpy as np

a = np.zeros(1000000, dtype=np.int16)
b = np.complex64(a[::2]) + np.complex64(1j) * np.complex64(a[1::2])

Can I do better without making a C extension or using something like cython? If I can't do better, what's my easiest approach using a technology like one of these?


回答1:


[~]
|1> import numpy as np

[~]
|2> a = np.zeros(1000000, dtype=np.int16)

[~]
|3> b = a.astype(np.float32).view(np.complex64)

[~]
|4> b.shape
(500000,)

[~]
|5> b.dtype
dtype('complex64')


来源:https://stackoverflow.com/questions/5658047/whats-the-fastest-way-to-convert-an-interleaved-numpy-integer-array-to-complex6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!