我试图用Python来检索现场音频输入的主频。目前我正在尝试使用笔记本电脑内置麦克风的音频流,但在测试下面的代码时,我得到的结果非常糟糕。Python中的频率分析
# Read from Mic Input and find the freq's
import pyaudio
import numpy as np
import bge
import wave
chunk = 2048
# use a Blackman window
window = np.blackman(chunk)
# open stream
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 1920
p = pyaudio.PyAudio()
myStream = p.open(format = FORMAT, channels = CHANNELS, rate = RATE, input = True, frames_per_buffer = chunk)
def AnalyseStream(cont):
data = myStream.read(chunk)
# unpack the data and times by the hamming window
indata = np.array(wave.struct.unpack("%dh"%(chunk), 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))
# stream.close()
# p.terminate()
的代码是从this question,与波形文件的傅立叶分析涉及蚕食。它是在当前的模块化结构中,因为我正在使用Blender Game Environment(因此在顶部导入bge)来实现它,但是我非常确定我的问题在于AnalyseStream模块。
任何意见,你可以提供将不胜感激。
更新:我得到正确的值,每隔一次又一次,但他们很少发现不正确的值(< 10Hz)。那个和程序真的很慢。
1920的采样率看起来很腥。更典型的音频采样率是8000或44100.你用什么样的声音来进行正确性测试?如果它不是来自正弦波发生器,那么您听到的音高和频率峰值可能会非常不同。 – hotpaw2 2012-01-31 16:31:51