如何使用Python在Matplotlib中繪製訊號?


要獲取訊號圖,我們可以按照以下步驟操作 −

  • 設定圖片大小並在子圖周圍和之間調整填充。
  • 獲取隨機種子值。
  • 初始化dt用於取樣間隔並找到取樣頻率。
  • 建立t的隨機資料點。
  • 使用numpy生成噪聲,獲取nse、r、cnses
  • 使用subplots()方法建立圖片和一組子圖。
  • 設定圖表的標題。
  • 繪製ts資料點。
  • 設定xy標籤。
  • 使用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("Signal")
axs.plot(t, s, color='C0')
axs.set_xlabel("Time")
axs.set_ylabel("Amplitude")

plt.show()

輸出

更新於: 16-6-2021

7K+ 瀏覽

開啟你的 事業

完成課程即可獲得認證

開始
廣告
© . All rights reserved.