python频率_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游戲環境中實現它的(因此在頂部是import bge),但是我很確定我的問題在AnalyseStream模塊中。
如果您能提供任何建議,我們將不勝感激。
更新:我時不時地得到正確的值,但在不正確的值中很少發現這些值(<;10Hz)。這個程序運行得很慢。
總結
以上是生活随笔為你收集整理的python频率_Python中的频率分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java实现dex转jar_dex转ja
- 下一篇: 谈谈对三大框架的理解