aubio 라이브러리는 SWIG로 싸여 있으므로 Python에서 사용할 수 있습니다. 많은 기능 중에는 YIN 알고리즘과 일부 하모닉 콤 알고리즘을 포함하여 피치 검출/추정을위한 여러 가지 방법이 있습니다.
그러나 좀 더 간단한 것을 원한다면 얼마 전에 피치 추정을위한 몇 가지 코드를 작성 했으므로 가져 가거나 그대로 둘 수 있습니다. aubio 알고리즘을 사용하는 것만 큼 정확하지는 않지만 필요에 따라 충분할 수 있습니다. 기본적으로 데이터의 FFT에 창 (이 경우 블랙맨 창)을 곱하고 FFT 값을 제곱하며 가장 높은 값을 갖는 빈을 찾은 후 최대 값의 로그를 사용하여 피크 주위에서 2 차 보간을 사용했습니다. 기본 주파수를 찾기 위해 이웃하는 두 값을 비교합니다. 나가 찾아낸 몇몇 종이에서 가지고 간 2 차 보간법.
테스트 톤에서 상당히 잘 작동하지만 위에 언급 된 다른 방법보다 강력하거나 정확하지는 않습니다. 청크 크기를 늘리거나 정확도를 높이면 정확도를 높일 수 있습니다. 청크 크기는 FFT를 최대한 활용하기 위해 2의 배수 여야합니다. 또한, 나는 겹치지 않는 각 덩어리의 기본 피치만을 결정하고있다. 예상 된 피치를 쓰는 동안 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