您需要在处理后将数据转换为适当位深度的整数类型。以这个文件为例:
>>> 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')
尽管 中的值data
非常接近 中的值data_bis
,但它们是非常不同的野兽,如下所示:
>>> scipy.io.wavfile.write('test.wav', rate, data_bis)
>>> scipy.io.wavfile.read('test.wav')
TypeError: data type not understood
但是,如果您只是将处理后的结果转换回原始结果dtype
,一切都会再次正常运行:
>>> 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))