我正在开发一个Python模块的概念,它可以检测特定频率(人类语音频率80-300hz),并通过从数据库中检查来显示句子的语调。我使用SciPy绘制声音文件的频率,但我无法设置任何特定的频率以分析音高。我该怎么做?
更多信息:我想能够设置一种定义好的语音模式(例如上升、下降),并且程序会检测声音文件是否遵循特定的模式。
我正在开发一个Python模块的概念,它可以检测特定频率(人类语音频率80-300hz),并通过从数据库中检查来显示句子的语调。我使用SciPy绘制声音文件的频率,但我无法设置任何特定的频率以分析音高。我该怎么做?
更多信息:我想能够设置一种定义好的语音模式(例如上升、下降),并且程序会检测声音文件是否遵循特定的模式。
更新于2019年,现在有基于神经网络的非常准确的音高跟踪器。而且它们可以直接在Python中使用。请查看https://pypi.org/project/crepe/
来自2015年的回答。音高检测是一个复杂的问题,谷歌最新的软件包为这个非常棘手的任务提供了高度智能的解决方案:
https://github.com/google/REAPER
你可以将其封装在Python中,如果你想从Python中访问它。
您可以尝试以下方法。我相信您知道人类声音也有超过300赫兹的谐波。尽管如此,您可以在音频文件上移动窗口,并尝试查看最大功率(如下所示)或窗口中一组频率的功率变化。以下代码旨在提供直觉:
import scipy.fftpack as sf
import numpy as np
def maxFrequency(X, F_sample, Low_cutoff=80, High_cutoff= 300):
""" Searching presence of frequencies on a real signal using FFT
Inputs
=======
X: 1-D numpy array, the real time domain audio signal (single channel time series)
Low_cutoff: float, frequency components below this frequency will not pass the filter (physical frequency in unit of Hz)
High_cutoff: float, frequency components above this frequency will not pass the filter (physical frequency in unit of Hz)
F_sample: float, the sampling frequency of the signal (physical frequency in unit of Hz)
"""
M = X.size # let M be the length of the time series
Spectrum = sf.rfft(X, n=M)
[Low_cutoff, High_cutoff, F_sample] = map(float, [Low_cutoff, High_cutoff, F_sample])
#Convert cutoff frequencies into points on spectrum
[Low_point, High_point] = map(lambda F: F/F_sample * M, [Low_cutoff, High_cutoff])
maximumFrequency = np.where(Spectrum == np.max(Spectrum[Low_point : High_point])) # Calculating which frequency has max power.
return maximumFrequency
voiceVector = []
for window in fullAudio: # Run a window of appropriate length across the audio file
voiceVector.append (maxFrequency( window, samplingRate))
现在根据语音的语调,最大功率频率可能会发生偏移,您可以注册并映射到给定的语调。这不一定总是正确的,您可能需要同时监测许多频率的变化,但这应该可以帮助您入门。
有许多不同的算法可以用来估计音高,但研究发现Praat的算法是最准确的[1]。最近,Parselmouth库使得从Python中调用Praat函数变得更加容易[2]。
[1]: Strömbergsson, Sofia. "Today's Most Frequently Used F0 Estimation Methods, and Their Accuracy in Estimating Male and Female Pitch in Clean Speech." INTERSPEECH. 2016. https://pdfs.semanticscholar.org/ff04/0316f44eab5c0497cec280bfb1fd0e7c0e85.pdf [2]: https://github.com/YannickJadoul/Parselmouth