Python中的频率分析 [英] Frequency Analysis in Python

查看:421
本文介绍了Python中的频率分析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python检索实时音频输入的主要频率.目前,我正在尝试使用笔记本电脑内置麦克风的音频流,但是在测试以下代码时,结果却很糟糕.

I'm trying to use Python to retrieve the dominant frequencies of a live audio input. For the moment I am experimenting using the audio stream my Laptop's built in microphone, but when testing the following code, I am getting very poor results.

    # 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()

该代码可从此问题中删除,该问题处理wave文件的傅立叶分析.当我使用Blender Game Environment实现它时,它处于当前的模块化结构中(因此,导入bge位于顶部),但是我可以肯定我的问题出在AnalyseStream模块之内.

The code is cannibalized from this question, which deals with Fourier Analysis of a wave file. It's in the current modular structure as I'm implementing it with the Blender Game Environment (hence the import bge at the top), but I'm pretty certain my problem lies within the AnalyseStream module.

您能提供的任何建议将不胜感激.

Any advice you can offer would be much appreciated.

更新:我一次又一次获得正确的值,但是在不正确的值(< 10Hz)中很少发现它们.这样,程序运行起来就很慢.

UPDATE: I'm getting the correct values every now and again, but they're found infrequently amongst incorrect values (<10Hz). That and the program runs REALLY slowly.

推荐答案

您好,找到用于实时分析的FFT计算最大值变得有点慢.

Hello find the maximum computing the FFT for real-time analysis becomes a little slow.

如果您不使用复杂的波形来查找频率,则可以使用基于时域的任何方法,例如过零,这样性能会更好.

If you will not work with complex waveforms to find the frequencies you can use any method based on Time-domain such as zero-crossing where the performance will be better.

去年,我做了一个简单的函数,可以通过零交叉来计算频率.

In the last year i make one simple function to calc the frequency by Zero-crossing.

#Eng Eder de Souza 01/12/2011
#ederwander
from matplotlib.mlab import find
import pyaudio
import numpy as np
import math


chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 20


def Pitch(signal):
    signal = np.fromstring(signal, 'Int16');
    crossing = [math.copysign(1.0, s) for s in signal]
    index = find(np.diff(crossing));
    f0=round(len(index) *RATE /(2*np.prod(len(signal))))
    return f0;


p = pyaudio.PyAudio()

stream = p.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
output = True,
frames_per_buffer = chunk)

for i in range(0, RATE / chunk * RECORD_SECONDS):
    data = stream.read(chunk)
    Frequency=Pitch(data)
    print "%f Frequency" %Frequency

ederwander

ederwander

这篇关于Python中的频率分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆