蟒蛇 - 删除范围的特定频率 [英] Python - Removing specific frequencies between a range

查看:184
本文介绍了蟒蛇 - 删除范围的特定频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个频谱图:

欲清洁谱图,所以我只捕获在特定范围内的频率(即,在本例中,2627之间 - 3939),并删除所有都低于该频率块。我的总体目标是仅留下与4段是该频率范围内的,并且,可以鉴别。

I want to clean the spectrogram up, so I only capture the frequencies within a specific range (i.e. in this example, between 2627 - 3939) and remove all of the blocks that are below this frequency. My overall aim is to only be left with the 4 segments that are within this frequency range, and, can be identified.

下面是我的code迄今:

Here is my code so far:

import wave, struct, numpy as np, matplotlib.mlab as mlab, pylab as pl
def wavToArr(wavefile):
    w = wave.open(wavefile,"rb")
    p = w.getparams()
    s = w.readframes(p[3])
    w.close()
    sd = np.fromstring(s, np.int16)
    return sd,p

def wavToSpec(wavefile,log=False,norm=False):
    wavArr,wavParams = wavToArr(wavefile)
    print wavParams
    return  mlab.specgram(wavArr, NFFT=256,Fs=wavParams[2],window=mlab.window_hanning,noverlap=128,sides='onesided',scale_by_freq=True)

wavArr,wavParams = wavToArr("4bats.wav")
Pxx, freqs, bins = wavToSpec("4bats.wav")
Pxx += 0.0001

freqs += (len(wavArr) / wavParams[2]) / 2.
hf=pl.figure(figsize=(12,12));
ax = hf.add_subplot(2,1,1);
#plot spectrogram as decibals
hm = ax.imshow(10*np.log10(Pxx),interpolation='nearest',origin='lower',aspect='auto')
hf.colorbar(hm)
ylcnt = len(ax.get_yticklabels())
ycnt = len(freqs)
ylstep = int(ycnt / ylcnt)
ax.set_yticklabels([ int(freqs[f]) for f in xrange(0,ycnt,ylstep) ])
pl.show()

问题是,我不知道如何做到这一点使用Python。我所知道的范围(2627 - 3939),但是,我会在整个二维数组迭代,并总结了所有的块,或者每个块的频谱范围内,计算频率,如果它高于阈值,保持它,否则,值将成为0.0?

The problem is, I don't know how to do this using Python. I know the ranges (2627 - 3939) but, would I iterate through the entire 2D-array and sum up all the blocks, or, for each block within the Spectrogram, calculate the frequency and if it's higher than the threshold, keep it, otherwise the values become 0.0?

如果我总结了每个仓的,我得到以下内容:

If I sum up each of the bins, I get the following:

我需要保持这些块,但是,希望从这些分离除去隔挡。

I need to keep these blocks, but, want to remove every other block apart from these.

我希望有人能帮帮我!

推荐答案

也许你想是这样的:

Pxx[np.greater(np.sum(Pxx, axis=1), 2955), :] = 0.

或者,我可能有你切换轴,所以也尝试:

or, I might have your axes switched, so also try:

Pxx[:, np.greater(np.sum(Pxx, axis=0), 2955)] = 0.

或者,也许你想要别的东西......我觉得这个问题有点不清楚。

Or maybe you want something else... I find the question a bit unclear.

这篇关于蟒蛇 - 删除范围的特定频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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