From IEEE32 float to 32bit integer numpy array (example : audio .wav files)

微笑、不失礼 提交于 2019-12-08 10:19:49

问题


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...

来源:https://stackoverflow.com/questions/22467556/from-ieee32-float-to-32bit-integer-numpy-array-example-audio-wav-files

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