Issues with scipy.io wave file processing after applying fourier transforms

大兔子大兔子 提交于 2019-12-01 11:01:23

You need to convert your data after processing to an integer type of appropriate bit depth. Using this file as an example:

>>> import scipy.io.wavfile
>>> rate, data = scipy.io.wavfile.read('Happy Tree Friends.wav')
>>> rate
8000
>>> data
array([ 5, -5,  5, ...,  0, -1,  0], dtype=int16)
>>> data_bis = np.fft.ifft(np.fft.fft(data))
>>> data_bis
array([  5.00000000e+00 -1.55406753e-11j,
        -5.00000000e+00 +1.95349676e-11j,
         5.00000000e+00 +1.41131140e-11j, ...,
         8.06674092e-12 -7.58643463e-13j,
        -1.00000000e+00 -2.21611283e-12j,  -2.04999489e-11 +4.55890751e-12j])
>>> data_bis.dtype
dtype('complex128')

Even though the values in data are really close to the ones in data_bis, they are very different beasts, as the following shows:

>>> scipy.io.wavfile.write('test.wav', rate, data_bis)
>>> scipy.io.wavfile.read('test.wav')
TypeError: data type not understood

But if you simply convert your processed results back to the original dtype, everything works nicely again:

>>> scipy.io.wavfile.write('test.wav', rate, data_bis.astype(data.dtype))
__main__:1: ComplexWarning: Casting complex values to real discards the imaginary part
>>> scipy.io.wavfile.read('test.wav')
(8000, array([ 4, -5,  4, ...,  0, -1,  0], dtype=int16))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!