简体   繁体   English

如何找到wav文件的基频

[英]How to find the fundamental frequency of a wav file

I'm analysing a lot of short .wav files and for a part of the analysis I want to plot the fundamental frequency only of the file.我正在分析很多短的 .wav 文件,并且对于部分分析,我只想绘制文件的基频。 My signal processing is a bit rusty, but I'm now getting plots that look like they should be correct.我的信号处理有点生疏,但我现在得到的图看起来应该是正确的。 I'm just not getting why the y axis scale is off (the F0 is around 300Hz when it should be around 8000Hz).我只是不明白为什么 y 轴刻度是关闭的(F0 大约是 300Hz,而它应该在 8000Hz 左右)。 So I want to plot the F0 across the duration of the .wav file like a spectrogram without the intensity information.所以我想在 .wav 文件的持续时间内绘制 F0,就像没有强度信息的频谱图一样。 Can anybody help me out?有人可以帮帮我吗? happy to provide additional info!很高兴提供更多信息!

from scipy import signal
import numpy as np
import soundfile as sf

y, samplerate = sf.read('audiofile.wav') 
chunks = np.array_split(y,int(samplerate/2000))
peaks = []

for chunk in chunks:
    # simulated pure signal
    t = np.linspace(0, 1, samplerate)
    wave = chunk
    # compute the magnitude of the Fourier Transform and its corresponding frequency values
    freq_magnitudes = np.abs(np.fft.fft(wave))
    freq_values = np.fft.fftfreq(samplerate, 1/samplerate)
    # find the max. magnitude
    max_positive_freq_idx = np.argmax(freq_magnitudes[:samplerate//2 + 1])
    peaks.append(freq_values[max_positive_freq_idx])

The numpy.fft.fftfreq documentation refers to the first argument as "window length", so I would recommend replacing numpy.fft.fftfreq文档将第一个参数称为“窗口长度”,所以我建议替换

freq_values = np.fft.fftfreq(samplerate, 1/samplerate)

with

freq_values = np.fft.fftfreq(len(wave), 1/samplerate)

or或者

freq_values = np.fft.fftfreq(wave.shape[0], 1/samplerate)

Hard to jugde since you have not provided your plot code but if your wav file is in stereo you will get y as a two dimensional array and that could be a potential problem.很难判断,因为您没有提供绘图代码,但如果您的wav文件是立体声的,您将得到y作为二维数组,这可能是一个潜在的问题。 As @Andris suggests, the window length also needs to be corrected.正如@Andris 建议的那样,窗口长度也需要更正。

If you make your own spectrogram plot, please check your output from fft and fftfreq , the order is like [0 .. Fs/2 -Fs/2 ..] .如果您制作自己的频谱图,请检查fftfftfreq的输出,顺序类似于[0 .. Fs/2 -Fs/2 ..] You can reorder them using fftshift .您可以使用fftshift重新排序它们。

Otherwise there is also a spectrogram plot ready to use in signal package否则,还有一个频谱图可以在信号包中使用

f,t,Sxx = signal.spectrogram(y[:,0],samplerate)

plt.pcolormesh(t, f, np.log10(Sxx))
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
plt.show()

频谱图准备使用以下抛出的错误你可以帮助纠正 f, t, Sxx = signal.spectrogram(y[:, 0], samplerate) IndexError: too many indices for array: array is 1-dimensional, but 2 were索引

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM