如何在 Python 中的 Matplotlib 中繪製相位頻譜?
要繪製相位頻譜,我們可以採取以下步驟 -
- 設定圖表的大小並將圖表間及周圍的填充調整好。
- 獲取隨機種子值。
- 初始化取樣間隔的**dt**並找出取樣頻率。
- 為**t**建立隨機資料點。
- 使用 numpy 生成噪聲,**獲得 nse、r、cnse**和**s**。
- 使用**subplots()**方法建立一個圖表和一組子圖表。
- 設定圖表的標題。
- 繪製相位頻譜。
- 使用**show()**方法顯示圖表。
示例
import matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True np.random.seed(0) dt = 0.01 # sampling interval Fs = 1 / dt # sampling frequency t = np.arange(0, 10, dt) # generate noise: nse = np.random.randn(len(t)) r = np.exp(-t / 0.05) cnse = np.convolve(nse, r) * dt cnse = cnse[:len(t)] s = 0.1 * np.sin(4 * np.pi * t) + cnse fig, axs = plt.subplots() axs.set_title("Phase Spectrum ") axs.phase_spectrum(s, Fs=Fs, color='C2') plt.show()
輸出
廣告