當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python Matplotlib.pyplot.specgram()用法及代碼示例

Matplotlib是Python中的一個庫,它是數字的-NumPy庫的數學擴展。 Pyplot是Matplotlib模塊的基於狀態的接口,該模塊提供了MATLAB-like接口。

matplotlib.pyplot.specgram()函數

matplotlib庫的pyplot模塊中的specgram()函數用於繪製頻譜圖。

用法: matplotlib.pyplot.specgram(x, NFFT=None, Fs=None, Fc=None, detrend=None, window=None, noverlap=None, cmap=None, xextent=None, pad_to=None, sides=None, scale_by_freq=None, mode=None, scale=None, vmin=None, vmax=None, *, data=None, **kwargs)


參數:此方法接受以下描述的參數:

  • x:此參數是數據序列。
  • Fs:此參數是標量。默認值為2。
  • window:此參數將數據段作為參數,並返回該段的窗口版本。其默認值為window_hanning()
  • sides:此參數指定要返回頻譜的哪一側。它可以具有以下值:‘default’,‘onesided’和‘twosided’。
  • pad_to:此參數包含填充數據段的整數值。
  • Fc:此參數還包含一個整數值,用於抵消曲線圖的x範圍以反映頻率範圍。其默認值為0
  • NFFT:此參數包含每個塊中用於FFT的數據點數。
  • detrend:此參數包含應用於fft-ing之前的每個段的函數,旨在刪除均值或線性趨勢{‘none’,‘mean’,‘linear’}。
  • scale_by_freq:該參數允許對返回的頻率值進行積分。
  • mode:此參數是要使用哪種頻譜{‘default’,‘psd’,‘magnitude’,‘angle’,‘phase’}。
  • noverlap:此參數是塊之間的重疊點數。
  • scale:此參數包含規範{‘default’,‘linear’,'dB'}中值的縮放。
  • Fc:此參數是x的中心頻率。
  • camp:此參數是matplotlib.colors.Colormap實例。

返回值:這將返回以下內容:

  • 頻譜:這將返回以弧度為單位的角度頻譜。
  • 頻率:這將返回與頻譜中的元素相對應的頻率。
  • t:這將返回對應於段中點的時間。
  • 即時通訊:這將返回由imshow創建的包含頻譜圖的圖像。

結果為(頻譜,頻率,t,im)

以下示例說明了matplotlib.pyplot中的matplotlib.pyplot.specgram()函數:

範例1:

# Implementation of matplotlib function 
import matplotlib.pyplot as plt  
import numpy as np  
  
    
dt = 0.005
t = np.arange(0.0, 20.0, dt)  
x = np.sin(np.pi * t) + 1.5 * np.cos(np.pi * 2*t)  
  
plt.specgram(x, Fs = 1) 
plt.title('matplotlib.pyplot.specgram() Example\n',  
          fontsize = 14, fontweight ='bold') 
  
plt.show()

輸出:

範例2:

# Implementation of matplotlib function 
import matplotlib.pyplot as plt  
import numpy as np  
  
  
np.random.seed(9360801)  
    
dt = 0.0005
t = np.arange(0.0, 20.0, dt)  
s1 = np.sin(4 * np.pi * 100 * t)  
s2 = 1.5 * np.sin(1.5 * np.pi * 400 * t)  
    
s2[t <= 10] = s2[12 <= t] = 0
    
nse = 0.2 * np.random.random(size = len(t))  
    
x = s1 + s2 + nse    
NFFT = 512 
Fs = int(1.0 / dt)   
  
plt.specgram(x, Fs = Fs, cmap = plt.cm.bone)  
plt.title('matplotlib.pyplot.specgram() Example\n', 
          fontsize = 14, fontweight ='bold') 
  
plt.show()

輸出:




相關用法


注:本文由純淨天空篩選整理自SHUBHAMSINGH10大神的英文原創作品 Matplotlib.pyplot.specgram() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。