aubio库已被SWIG封装,因此可以被Python使用。其众多功能包括音调检测/估计的几种方法,包括YIN算法和一些谐波梳理算法。然而,如果你想要简单些,我前段时间为音高估计编写了一些代码,你可以拿它或者离开它。它不会像在aubio中使用算法一样准确,但它可能足够满足您的需求。我基本上只是把数据的FFT乘以一个窗口(在这种情况下是一个Blackman窗口),FFT值的平方,找到具有最高值的bin,并且使用最大值的对数在峰值附近使用二次插值和它的两个相邻值来找到基频。我从我发现的一些论文中得到的二次插值。
它在测试音调上运行得很好,但它不会像上面提到的其他方法那样稳健或不精确。通过增加块大小(或通过减小块大小)可以提高精度。块大小应该是2的倍数以充分利用FFT。另外,我只确定每个块的基本音高,没有重叠。我用PyAudio在写出估计音高时播放声音。
源代码:
# Read in a WAV and find the freq's
import pyaudio
import wave
import numpy as np
chunk = 2048
# open up a wave
wf = wave.open('test-tones/440hz.wav', 'rb')
swidth = wf.getsampwidth()
RATE = wf.getframerate()
# use a Blackman window
window = np.blackman(chunk)
# open stream
p = pyaudio.PyAudio()
stream = p.open(format =
p.get_format_from_width(wf.getsampwidth()),
channels = wf.getnchannels(),
rate = RATE,
output = True)
# read some data
data = wf.readframes(chunk)
# play stream and find the frequency of each chunk
while len(data) == chunk*swidth:
# write data out to the audio stream
stream.write(data)
# unpack the data and times by the hamming window
indata = np.array(wave.struct.unpack("%dh"%(len(data)/swidth),\
data))*window
# Take the fft and square each value
fftData=abs(np.fft.rfft(indata))**2
# find the maximum
which = fftData[1:].argmax() + 1
# use quadratic interpolation around the max
if which != len(fftData)-1:
y0,y1,y2 = np.log(fftData[which-1:which+2:])
x1 = (y2 - y0) * .5/(2 * y1 - y2 - y0)
# find the frequency and output it
thefreq = (which+x1)*RATE/chunk
print "The freq is %f Hz." % (thefreq)
else:
thefreq = which*RATE/chunk
print "The freq is %f Hz." % (thefreq)
# read some more data
data = wf.readframes(chunk)
if data:
stream.write(data)
stream.close()
p.terminate()
这可能有帮助(请务必阅读回复):http://www.keyongtech.com/5003865-frequency-analysis-without-numpy – ChristopheD 2010-04-15 19:07:49