首页 > 解决方案 > 如何找到wav文件的基频

问题描述

我正在分析很多短的 .wav 文件,并且对于部分分析,我只想绘制文件的基频。我的信号处理有点生疏,但我现在得到的图看起来应该是正确的。我只是不明白为什么 y 轴刻度是关闭的(F0 大约是 300Hz,而它应该在 8000Hz 左右)。所以我想在 .wav 文件的持续时间内绘制 F0,就像没有强度信息的频谱图一样。有人可以帮帮我吗?很高兴提供更多信息!

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

标签: pythonsignal-processingtime-frequency

解决方案


numpy.fft.fftfreq文档将一个参数称为“窗口长度”,所以我建议替换

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

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

或者

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

推荐阅读