From IEEE32 float to 32bit integer numpy array (ex

2019-06-14 12:29发布

问题:

I read some data which is stored as IEEE32 float in the interval [-1, +1] with

A = numpy.fromstring(data, dtype = numpy.float32)

But then I don't want to keep the array as float32, but rather, as 32-bit integers (in [-2^31, +2^31-1]).

I tried by doing simply :

A = numpy.fromstring(data, dtype = numpy.int32)   # that's wrong!

but it provides a bad result. (wrong probably by a multiplicative constant, or another reason?)

How to read an array of IEEE32 float in [-1,1] into a 32bits-integer array in [-2^31, +2^31-1] ?


PS : This would work, but I would like to avoid this naive way to do it :

A = numpy.fromstring(data, dtype = numpy.float32)
A *= 2^31    
A = int(A)   # convert the data type to int

if possible, because I imagine there is a more clever way to do it, without multipliying, but just by doing things bitwise...