Python的频率检测 [英] Python frequency detection

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

问题描述

确定什么即时试图做的是一种音频处理软件,如果频率是打了足够长的时间(几毫秒)我知道我得到了一个积极的比赛,可以检测prevalent频率的。我知道我需要使用FFT或东西simiral但在数学这个领域我吸,我也上网搜索,但没找不到code,可以做到只有这个。

Ok what im trying to do is a kind of audio processing software that can detect a prevalent frequency an if the frequency is played for long enough (few ms) i know i got a positive match. i know i would need to use FFT or something simiral but in this field of math i suck, i did search the internet but didn not find a code that could do only this.

目标即时试图accieve是让自己自定义的协议发送数据低谷的声音,需要每秒非常低比特率(5-10bps),但IM也对结束信号传递非常有限,所以recieving软件将需要能够自定义(不能使用一个实际的硬件/软件调制解调器)我也想这是唯一的软件(除了声卡无需额外的硬件)

the goal im trying to accieve is to make myself a custom protocol to send data trough sound, need very low bitrate per sec (5-10bps) but im also very limited on the transmiting end so the recieving software will need to be able custom (cant use an actual hardware/software modem) also i want this to be software only (no additional hardware except soundcard)

非常感谢您的帮助。

推荐答案

借助 aubio 库已经包裹着痛饮,因此可以通过使用蟒蛇。在他们的许多功能,包括几种方法基音检测/估计包括算法和一些谐波梳子算法。

The aubio libraries have been wrapped with SWIG and can thus be used by Python. Among their many features include several methods for pitch detection/estimation including the YIN algorithm and some harmonic comb algorithms.

不过,如果你想要简单的东西,我写了一些code为基音估计前一段时间,你可以把它或离开它。它不会像在aubio使用的算法准确,但它可能不够好您的需求。我基本上只是拿了FFT数据时代的窗口(在这种情况下Blackman窗)的平方FFT值,发现有最高值的bin,并使用最大值的日志中使用周围的峰值二次插值和它的两个相邻值寻找基本频率。在二次插值我从一些纸,我发现了。

However, if you want something simpler, I wrote some code for pitch estimation some time ago and you can take it or leave it. It won't be as accurate as using the algorithms in aubio, but it might be good enough for your needs. I basically just took the FFT of the data times a window (a Blackman window in this case), squared the FFT values, found the bin that had the highest value, and used a quadratic interpolation around the peak using the log of the max value and its two neighboring values to find the fundamental frequency. The quadratic interpolation I took from some paper that I found.

它工作得相当好于测试音调,但它不会像健壮或精确与上述其他方法。的精度可通过增加块大小(或者通过减小它减小)。块的大小应该是2的倍数,以充分利用FFT的。另外,我只确定没有重叠每个块的基本音调。我用PyAudio通过,而写出来的估计的音调来播放声音。

It works fairly well on test tones, but it will not be as robust or as accurate as the other methods mentioned above. The accuracy can be increased by increasing the chunk size (or reduced by decreasing it). The chunk size should be a multiple of 2 to make full use of the FFT. Also, I am only determining the fundamental pitch for each chunk with no overlap. I used PyAudio to play the sound through while writing out the estimated pitch.

来源$ C ​​$ C:

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

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

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