简体   繁体   English

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

[英]How do I get the values of a specific frequency range

I have a .wav file, I load it and I get the next spectrogram showing the spectrum in dB 我有一个.wav文件,将其加载后得到下一个频谱图,以dB为单位显示频谱

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

Now I would like to know these values exactly because I want to compare with other wav file, for recognizing if these 4 values are there. 现在,我想确切地知道这些值,因为我想与其他wav文件进行比较,以识别这4个值是否存在。

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

The source to generate that pictures (taken from other stackoverflow example) 生成该图片的源(取自其他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)')

I "think" that the information is in Pxx but I don't know how to get it. 我“认为”该信息包含在Pxx中,但我不知道如何获取。

From the documentation , I gather that Pxx is a simple 2D numpy array. 文档中 ,我收集到Pxx是一个简单的2D numpy数组。

You're interested in periodograms around 1s. 您对1s左右的周期图感兴趣。 Considering Pxx should have 512 columns and your sample is about 5s long, I'd take a slice somewhere around column 100: periodogram_of_interest = Pxx[:, 100] 考虑到Pxx应该有512列并且您的样本大约有5s长,所以我会在第100列的周围切一个切片:periodogram_of_interest = Pxx [:, 100]

Then find the 4 maxima. 然后找到4个最大值。 Unfortunately, each of those 4 frequencies has a finite width, so simply looking for the top 4 maxima will nog be as easy. 不幸的是,这4个频率中的每个频率都具有有限的宽度,因此简单地查找前4个最大值将非常容易。 However, assuming your signal is quite clean, there's a function in scipy.signal that will list all local extrema: argrelmax . 但是,假设您的信号非常干净, scipy.signal中有一个函数将列出所有局部极值: argrelmax You could play with the order argument of that function to reduce your search space. 您可以使用该函数的order参数来减少搜索空间。

With the values returned from that function, you could get the frequencies like this: freqs[those_4_indices] . 使用从该函数返回的值,您可以获得如下频率: freqs[those_4_indices]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM