如何获取特定频率范围的值

时间:2014-04-02 14:08:53

标签: python matplotlib frequency spectrogram sound-recognition

我有一个.wav文件,我加载它然后我得到下一个频谱图,以dB为单位显示频谱

http://i.stack.imgur.com/22TjY.png

现在我想知道这些值,因为我想与其他wav文件进行比较,以识别这4个值是否存在。

http://i.stack.imgur.com/Jun25.png

生成图片的来源(取自其他stackoverflow示例)

## some stuff here

for i in range(0, int(RATE / CHUNK_SIZE * RECORD_SECONDS)):
    # little endian, signed shortdata_chunk
    data_chunk = array('h', stream.read(CHUNK_SIZE))
    if byteorder == 'big':
        data_chunk.byteswap()
    data_all.extend(data_chunk)

## some stuff here

Fs = 16000
f = np.arange(1, 9) * 2000
t = np.arange(RECORD_SECONDS * Fs) / Fs 
x = np.empty(t.shape)
for i in range(8):
x[i*Fs:(i+1)*Fs] = np.cos(2*np.pi * f[i] * t[i*Fs:(i+1)*Fs])

w = np.hamming(512)
Pxx, freqs, bins = mlab.specgram(data_all, NFFT=512, Fs=Fs, window=w, 
                noverlap=464)

#plot the spectrogram in dB
Pxx_dB = np.log10(Pxx)
pyplot.subplots_adjust(hspace=0.4)

pyplot.subplot(211)
ex1 = bins[0], bins[-1], freqs[0], freqs[-1]
pyplot.imshow(np.flipud(Pxx_dB), extent=ex1)
pyplot.axis('auto')
pyplot.axis(ex1)
pyplot.xlabel('time (s)')
pyplot.ylabel('freq (Hz)')

我“认为”信息是在Pxx中,但我不知道如何获得它。

1 个答案:

答案 0 :(得分:0)

documentation开始,我认为Pxx是一个简单的2D numpy数组。

你对1s左右的周期图感兴趣。考虑到Pxx应该有512列,你的样本长约5s,我会在第100列的某处切片:     periodogram_of_interest = Pxx [:,100]

然后找到4个最大值。不幸的是,这4个频率中的每一个都具有有限的宽度,因此简单地查找前4个最大值将非常容易。但是,假设您的信号非常干净,scipy.signal中的一个函数将列出所有本地极值:argrelmax。您可以使用该函数的order参数来减少搜索空间。

使用该函数返回的值,您可以获得如下频率:freqs[those_4_indices]