[英]How to plot frequency band using `matplotlib.pyplot.specgram`
我可以繪制頻譜圖(在 Jupyter 筆記本中),因此:
fs = 48000
noverlap = (fftFrameSamps*3) // 4
spectrum2d, freqs, timePoints, image = \
plt.specgram( wav, NFFT=fftFrameSamps, Fs=fs, noverlap=noverlap )
plt.show()
但是,我只對 15-20 kHz 范圍感興趣。 我怎樣才能只繪制這個范圍?
我可以看到該函數返回image
,所以也許我可以將圖像轉換為矩陣並從矩陣中取出適當的切片......?
我可以看到該函數接受vmin
和vmax
但這些似乎沒有記錄並且使用它們不會產生有效的結果。
您可以像通常使用set_ylim()
和set_xlim()
一樣修改軸的限制。 在這種情況下
plt.ylim([15000, 20000])
應該將您的繪圖限制在 15-20 kHz 范圍內。 對於來自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.