python - 如何找到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])
解决方案
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)
推荐阅读
- http - HTTP 客户端:因错误断开连接:-114 Lua
- google-api - Google 地方信息自动填充以错误的语言返回结果
- reactjs - 图像随着警报消失
- javascript - 在每 x 个单词上使用 .split()
- c++ - FILETIME 到/从 ISO 8601 与 Win32 API。正确设置夏令时?
- javascript - 谷歌表格应用脚本不隐藏标签
- javascript - 在复选框上使用 JavaScript 时遇到问题
- python - 如何在机器学习中使用 K-fold 验证进行欠采样
- python - 无论我做什么,都无法使用预签名 URL 将文件上传到 S3。AWS 命令行有效。CURL 和其他任何东西 = 403
- sql - 在 BigQuery 中结合聚合和分析功能以减小表大小