[英]Load an audio file and find the frequency
I have a following code:我有以下代码:
rate, data = wav.read('C.wav')
Fourier = abs(fftpk.fft(data))
max = np.argmax(Fourier, axis=None, out=None)
print(max) # get 787
freq = fftpk.fftfreq(len(Fourier), (1.0/rate))
plt.plot(freq[range(len(Fourier)//2)], Fourier[range(len(Fourier)//2)])
plt.xlabel('Frequency (Hz)')
plt.ylabel('Amplitude')
plt.show()
I want this program to give me the frequency in Hz with the highest amplitude, but instead of getting value around 260, I get 787. I do not know what the problem is.我希望这个程序给我以赫兹为单位的最高振幅的频率,但我得到的不是 260 左右的值,而是 787。我不知道问题是什么。
Plot of the file: Plot 文件:
np.argmax
gives you the index of the maximum element in the Fourier
frequency, not the actual frequency. np.argmax
为您提供Fourier
频率中最大元素的索引,而不是实际频率。 The relation to obtain the frequency from the index is frequency = index*rate/len(Fourier)
.从索引中获取频率的关系是frequency = index*rate/len(Fourier)
。 So, applying this in your case should give you the desired frequency:因此,在您的情况下应用它应该会给您所需的频率:
max = np.argmax(Fourier, axis=None, out=None)
print(max) # get 787
maxfreq = max*rate/len(Fourier) # should give ~260
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.