[英]Python: performing FFT on music file
I am trying to perform a FFT on a song (audio file in wav format, about 3 minutes long) which I created as follows, just in case it is relevant. 我正在尝试对一首歌曲(wav格式的音频文件,大约3分钟)执行FFT,我创建如下,以防它是相关的。
ffmpeg -i "$1" -vn -ab 128k -ar 44100 -y -ac 1 "${1%.webm}.wav"
Where $1
is the name of a webm file. 其中
$1
是webm文件的名称。
This is the code which is supposed to display a FFT of the given file: 这是应该显示给定文件的FFT的代码:
import numpy as np
import matplotlib.pyplot as plt
# presume file already converted to wav.
file = os.path.join(temp_folder, file_name)
rate, aud_data = scipy.io.wavfile.read(file)
# wav file is mono.
channel_1 = aud_data[:]
fourier = np.fft.fft(channel_1)
plt.figure(1)
plt.plot(fourier)
plt.xlabel('n')
plt.ylabel('amplitude')
plt.show()
The problem is, it takes for ever. 问题是,这需要永远。 It takes so long that I cannot even show the output, since I've had plenty of time to research and write this post and it still has not finished.
我需要很长时间才能显示输出,因为我有足够的时间研究和撰写这篇文章但它还没有完成。
I presume that the file is too long, since 我认为文件太长了,因为
print (aud_data.shape)
outputs (9218368,)
, but this looks like a real world problem, so I hope there is a way to obtain an FFT of an audio file somehow. 输出
(9218368,)
,但这看起来像一个真实世界的问题,所以我希望有办法以某种方式获得音频文件的FFT。
What am I doing wrong? 我究竟做错了什么? Thank you.
谢谢。
edit 编辑
A better formulation of the question would be: is the FFT of any good in music processing? 问题的一个更好的表述是:音乐处理的任何好处的FFT? For example similarity of 2 pieces.
例如2件的相似性。
As pointed out in the comments, my plain approach is way too slow. 正如评论中指出的那样,我的简单方法太慢了。
Thank you. 谢谢。
To considerably speed up the fft
portion of your analysis, you can zero-pad out your data to a power of 2: 为了大大加快分析的
fft
部分,您可以将数据填充到2的幂:
import numpy as np
import matplotlib.pyplot as plt
# rate, aud_data = scipy.io.wavfile.read(file)
rate, aud_data = 44000, np.random.random((9218368,))
len_data = len(aud_data)
channel_1 = np.zeros(2**(int(np.ceil(np.log2(len_data)))))
channel_1[0:len_data] = aud_data
fourier = np.fft.fft(channel_1)
Here is an example of plotting the real component of the fourier transform of a few sine waves using the above method: 下面是使用上述方法绘制几个正弦波的傅立叶变换的实部的示例:
import numpy as np
import matplotlib.pyplot as plt
# rate, aud_data = scipy.io.wavfile.read(file)
rate = 44000
ii = np.arange(0, 9218368)
t = ii / rate
aud_data = np.zeros(len(t))
for w in [1000, 5000, 10000, 15000]:
aud_data += np.cos(2 * np.pi * w * t)
# From here down, everything else can be the same
len_data = len(aud_data)
channel_1 = np.zeros(2**(int(np.ceil(np.log2(len_data)))))
channel_1[0:len_data] = aud_data
fourier = np.fft.fft(channel_1)
w = np.linspace(0, 44000, len(fourier))
# First half is the real component, second half is imaginary
fourier_to_plot = fourier[0:len(fourier)//2]
w = w[0:len(fourier)//2]
plt.figure(1)
plt.plot(w, fourier_to_plot)
plt.xlabel('frequency')
plt.ylabel('amplitude')
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.