简体   繁体   English

如何使用`matplotlib.pyplot.specgram`绘制频带

[英]How to plot frequency band using `matplotlib.pyplot.specgram`

I can plot a spectrogram (in a Jupyter notebook) thus:我可以绘制频谱图(在 Jupyter 笔记本中),因此:

fs = 48000
noverlap = (fftFrameSamps*3) // 4
spectrum2d, freqs, timePoints, image = \
    plt.specgram( wav, NFFT=fftFrameSamps, Fs=fs, noverlap=noverlap )

plt.show()

在此处输入图片说明

However, I am only interested in the 15-20 kHz range.但是,我只对 15-20 kHz 范围感兴趣。 How can I plot only this range?我怎样才能只绘制这个范围?

I can see that the function returns image , so maybe I could convert the image to a matrix and take an appropriate slice from the matrix...?我可以看到该函数返回image ,所以也许我可以将图像转换为矩阵并从矩阵中取出适当的切片......?

I can see that the function accepts vmin and vmax but these appear to be undocumented and playing with them doesn't yield a valid result.我可以看到该函数接受vminvmax但这些似乎没有记录并且使用它们不会产生有效的结果。

You can modify the limits of the axis as you would normally with set_ylim() and set_xlim() .您可以像通常使用set_ylim()set_xlim()一样修改轴的限制。 In this case在这种情况下

plt.ylim([15000, 20000])

should restrict your plot to the 15-20 kHz range.应该将您的绘图限制在 15-20 kHz 范围内。 For a complete example drawing from the Spectrogram Demo :对于来自Spectrogram Demo的完整示例图:

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(19680801)

dt = 0.0005
t = np.arange(0.0, 20.0, dt)
s1 = np.sin(2 * np.pi * 100 * t)
s2 = 2 * np.sin(2 * np.pi * 400 * t)

# create a transient "chirp"
s2[t <= 10] = s2[12 <= t] = 0

# add some noise into the mix
nse = 0.01 * np.random.random(size=len(t))

x = s1 + s2 + nse  # the signal
NFFT = 1024  # the length of the windowing segments
Fs = int(1.0 / dt)  # the sampling frequency

fig, (ax1, ax2) = plt.subplots(nrows=2, figsize=(14, 7))
ax1.specgram(x, NFFT=NFFT, Fs=Fs, noverlap=900)
ax2.specgram(x, NFFT=NFFT, Fs=Fs, noverlap=900)
ax2.set_ylim([50, 500])
plt.show()

在此处输入图片说明

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

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