Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีพล็อต Signal ใน Matplotlib ด้วย Python

การพล็อตสัญญาณ (Signal) ใน Domain เวลา

แสดงสัญญาณในรูปแบบเวลา-แอมพลิจูด (Time-domain signal)

ขั้นตอนหลัก

  • ตั้งค่าขนาดรูปและ padding
  • ตั้งค่า random seed, dt, Fs, t
  • สร้างสัญญาณ s ประกอบด้วย sine wave + noise
  • สร้าง figure และ subplots
  • ตั้งค่า title, พล็อต t vs s, ตั้ง xlabel, ylabel
  • แสดงผลด้วย 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
Fs = 1 / dt
t = np.arange(0, 10, dt)

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()

คำอธิบาย

สัญญาณ s ประกอบด้วย sine wave 0.1 Hz และ colored noise (exponential decay)