Python specgram matplotlib中未使用频率的切割

Python specgram matplotlib中未使用频率的切割,python,matplotlib,frequency,spectrogram,Python,Matplotlib,Frequency,Spectrogram,我有一个采样率为16e3的信号,它的频率范围是125到1000赫兹。 因此,如果我绘制一个光谱图,我会得到一个非常小的颜色范围,因为所有未使用的频率 我试图通过设置ax限制来解决这个问题,但这不起作用 有没有办法切断未使用的频率或用NAN代替它们 将数据重新采样到2e3将不起作用,因为仍有一些未使用的频率低于125 Hz 感谢您的帮助。specgram()正在为您完成所有工作。如果查看axis.py中的specgram函数,可以看到它是如何工作的。原始函数位于我的计算机上的Python27\Li

我有一个采样率为16e3的信号,它的频率范围是125到1000赫兹。 因此,如果我绘制一个光谱图,我会得到一个非常小的颜色范围,因为所有未使用的频率

我试图通过设置ax限制来解决这个问题,但这不起作用

有没有办法切断未使用的频率或用NAN代替它们

将数据重新采样到2e3将不起作用,因为仍有一些未使用的频率低于125 Hz

感谢您的帮助。

specgram()正在为您完成所有工作。如果查看axis.py中的specgram函数,可以看到它是如何工作的。原始函数位于我的计算机上的
Python27\Lib\site packages\matplotlib\axes.py

    <snip>  
    Pxx, freqs, bins = mlab.specgram(x, NFFT, Fs, detrend,
         window, noverlap, pad_to, sides, scale_by_freq)

    Z = 10. * np.log10(Pxx)
    Z = np.flipud(Z)

    if xextent is None: xextent = 0, np.amax(bins)
    xmin, xmax = xextent
    freqs += Fc
    extent = xmin, xmax, freqs[0], freqs[-1]
    im = self.imshow(Z, cmap, extent=extent, **kwargs)
    self.axis('auto')

    return Pxx, freqs, bins, im
Pxx是一个二维阵列,形状为(len(freqs),len(bin)

这将限制Pxx和频率

Pxx = Pxx[(freqs >= 125) & (freqs <= 1000)]
freqs = freqs[(freqs >= 125) & (freqs <= 1000)]
Pxx=Pxx[(freqs>=125)和(freqs=125)和(freqs 10,tspecgram()返回(Pxx,freqs,bins,im),其中im是AxesImage
实例[1]。您可以使用它设置绘图的限制:

Pxx, freqs, bins, im = plt.specgram(signal, Fs=fs)
im.set_ylim((125,1000))

[1]

这是一个经过改编的版本: 这会更改绘制的频率范围

#!/usr/bin/env python
#### from the example
#### 
from pylab import *

dt = 0.0005
t = arange(0.0, 20.0, dt)
s1 = sin(2*pi*100*t)
s2 = 2*sin(2*pi*400*t)
mask = where(logical_and(t>10, t<12), 1.0, 0.0)
s2 = s2 * mask
nse = 0.01*randn(len(t))
x = s1 + s2 + nse # the signal

NFFT = 1024       # the length of the windowing segments
Fs = int(1.0/dt)  # the sampling frequency

ax1 = subplot(211)
plot(t, x)
subplot(212, sharex=ax1)
Pxx, freqs, bins, im = specgram(x, NFFT=NFFT, Fs=Fs, noverlap=900,
                                cmap=cm.gist_heat)

#### edited from the example
#### 
# here we get access to the axes
x1,x2,y1,y2 = axis()
# leave x range the same, change y (frequency) range
axis((x1,x2,25,500))

show()
!/usr/bin/env python
####从这个例子
#### 
从派拉布进口*
dt=0.0005
t=arange(0.0,20.0,dt)
s1=sin(2*pi*100*t)
s2=2*sin(2*pi*400*t)

mask=where(logical_)和(t>10,t如今,有一种比提问时更简单的方法:可以使用
matplotlib.pyplot.axis
ymin
ymax
设置为所需的频率。这很简单;下面是我的程序片段:

plt.specgram(xmit, NFFT=65536, Fs=Fs)
plt.axis(ymin=Fc-Fa*10, ymax=Fc+Fa*10)
plt.show()

您是否尝试将scale\u by\u freq参数设置为False?是的,这没有任何改变。我只是在查看文档…可能与cmap参数一起使用。我认为如果我有静态数据,这可能会起作用,不幸的是,我必须绘制多个specgram。是否有某种方法可以直接编辑绘图细节?并删除未使用的数据?或者没有o获取最高值以动态设置cmap值?非常感谢。如果我有足够的声誉,我会向上投票。我添加了我的_specgram(),我认为它符合您的需要。现在您应该有足够的声誉来向上投票;-)@wwii,您好,为什么要做Z=10。*np.log10(Pxx)到spectrum?@moeseth,这是原始函数的一部分-我用一行散列将我的更改(上下)括起来。
Pxx, freqs, bins, im = plt.specgram(signal, Fs=fs)
im.set_ylim((125,1000))
#!/usr/bin/env python
#### from the example
#### 
from pylab import *

dt = 0.0005
t = arange(0.0, 20.0, dt)
s1 = sin(2*pi*100*t)
s2 = 2*sin(2*pi*400*t)
mask = where(logical_and(t>10, t<12), 1.0, 0.0)
s2 = s2 * mask
nse = 0.01*randn(len(t))
x = s1 + s2 + nse # the signal

NFFT = 1024       # the length of the windowing segments
Fs = int(1.0/dt)  # the sampling frequency

ax1 = subplot(211)
plot(t, x)
subplot(212, sharex=ax1)
Pxx, freqs, bins, im = specgram(x, NFFT=NFFT, Fs=Fs, noverlap=900,
                                cmap=cm.gist_heat)

#### edited from the example
#### 
# here we get access to the axes
x1,x2,y1,y2 = axis()
# leave x range the same, change y (frequency) range
axis((x1,x2,25,500))

show()
plt.specgram(xmit, NFFT=65536, Fs=Fs)
plt.axis(ymin=Fc-Fa*10, ymax=Fc+Fa*10)
plt.show()