应用傅立叶变换后scipy.io wave文件处理问题 [英] Issues with scipy.io wave file processing after applying fourier transforms

查看:98
本文介绍了应用傅立叶变换后scipy.io wave文件处理问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用scipy.fftpack和scipy.io包在Python中处理一些音频文件.这意味着我有导入的wave文件,使用傅里叶变换对其进行处理,然后输出到新的wave文件.但是,我遇到了这样的问题:运行这些转换后,波形文件将无法播放,并且大约是原始文件大小的4倍.

I'm working with some audio files in Python using scipy.fftpack and scipy.io packages. What this implies is I have wave files that I am importing, playing around with them using Fourier transforms, and then outputting to a new wave file. I am however having running into issues where in after running these transforms, the wave file will not play and is roughly 4 times the size of the original file.

目前,我只是在导入歌曲,从导入中获取速率,数据片段,然后执行ifft(fft(data)),然后将其输出.我尝试将这些浮点值转换为整数,然后计算初始导入的数据与ifft(fft(data))之间的差,发现它恰好为零.

Currently I'm just importing the song, taking the rate, data pieces from the import, doing ifft(fft(data)) and then outputting this. I have tried taking these float values and converting it to integers and calculating the difference between the initial imported data and the ifft(fft(data)) and saw that it was exactly zero.

以下是我的问题:

  1. 有人会知道为什么在进行傅立叶变换后我要播放的wave文件无法播放吗?

  1. Would anyone know why the wave files I am attempting to play after doing fourier transforms won't play?

对Wave文件是否有任何限制,这些限制通常会引起问题.还是数据必须是整数?

Are there any restrictions on wave files that coud be causing a issues in general. Or does the data have to be in integers?

已经很晚了,我觉得我的帖子可能有点头脑混乱,所以如果您需要更多信息,请询问,我会尽力提供更好的描述.

It's late and I feel like my post may be scatter-brained, so if you need more information please ask and I will do my best to give better description.

推荐答案

您需要在处理后将数据转换为适当的位深的整数类型.以此文件为例:

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')

即使data中的值确实与data_bis中的值非常接近,但它们却是非常不同的野兽,如下所示:

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

但是,如果您只是简单地将处理后的结果转换回原始的dtype,则一切将再次正常运行:

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

这篇关于应用傅立叶变换后scipy.io wave文件处理问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆