如何使用 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("Longitudinal Magnitude Spectrum")
axs.magnitude_spectrum(s, Fs=Fs, scale='dB', color='C1')
plt.show()

輸出

更新日期: 2021 年 6 月 16 日

301 次瀏覽

開啟你的 職業生涯

完成課程以獲取認證

開始
廣告
© . All rights reserved.