[英]how to efficiently convert numpy.array full of 14 bit binary numbers into corresponding two's complement in python?
having more than a million numbers stored in np.array.在 np.array 中存储了超过一百万个数字。 These are straight binary codes and I need to convert them into appropriate two's complement.
这些是直接的二进制代码,我需要将它们转换为适当的二进制补码。
I'm using actually map of a converting function to each element of the array, but it is veeeery slow!我实际上正在使用转换函数的映射到数组的每个元素,但它非常慢!
So, having eg:所以,有例如:
np.array((127,126,2,1,0,255,254,130,129,128))
I'd like to do some manipulation to get array containing我想做一些操作以获得包含的数组
(127,126,2,1,0,-1,-2,-126,-127,-128) (127,126,2,1,0,-1,-2,-126,-127,-128)
Any hint appreciated .d.任何提示表示赞赏。
>>> a = numpy.array((127,126,2,1,0,255,254,130,129,128), dtype=numpy.uint8)
>>> a
array([127, 126, 2, 1, 0, 255, 254, 130, 129, 128], dtype=uint8)
>>> a.astype(numpy.int8)
array([ 127, 126, 2, 1, 0, -1, -2, -126, -127, -128], dtype=int8)
>>> a = numpy.array((127,126,2,1,0,255,254,130,129,128), dtype=numpy.uint16)
>>> a
array([127, 126, 2, 1, 0, 255, 254, 130, 129, 128], dtype=uint16)
>>> a.astype(numpy.int8)
array([ 127, 126, 2, 1, 0, -1, -2, -126, -127, -128], dtype=int8)
一种选择是根据需要将 14 位数字存储在 uint16 或 int16 中,然后向左移动 2 位以捕获符号位,将其视为相反的数据类型并向右移动 2 位,即data_out = numpy.uint16(data_in<<2).astype(numpy.int16)>>2
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.