Is there a faster way of reading a buffer of integers to an array of complex numbers?
This works good (if buffer is with floats):
import numpy, struct
binary_string = struct.pack('2f', 1,2)
print numpy.frombuffer(binary_string, dtype=numpy.complex64)
# [ 1. + 2.j]
But, if readed buffer is with integers, there is a problem:
import numpy, struct
binary_string = struct.pack('2i', 1,2)
print numpy.frombuffer(binary_string, dtype=numpy.complex64)
# [ 1.40129846e-45 +2.80259693e-45j]
So, I can't find any faster way to convert it except with a slicing:
import numpy, struct
#for int32
binary_string = struct.pack('2i', 1,2)
ints = numpy.frombuffer(binary_string, dtype=numpy.int32)
print ints[::2] + 1j*ints[1::2]
# [ 1. + 2.j]
#for int16
binary_string = struct.pack('2H', 1,2)
ints = numpy.frombuffer(binary_string, dtype=numpy.int16)
print ints[::2] + 1j*ints[1::2]
# [ 1. + 2.j]
Also, is there any of a "complex number with integers" datatype, so a result could look like:
[1 + 2j]
Thanks.