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