Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách vẽ tín hiệu (signal) trong Matplotlib Python?

Để vẽ đồ thị tín hiệu (signal plot):

  • Thiết lập kích thước hình và điều chỉnh khoảng đệm subplot.
  • Đặt seed ngẫu nhiên.
  • Khởi tạo dt khoảng lấy mẫu, tính tần số lấy mẫu.
  • Tạo điểm dữ liệu ngẫu nhiên cho t.
  • Tạo nhiễu: lấy nse, r, cnse, s bằng numpy.
  • Tạo figure và subplots bằng subplots().
  • Đặt tiêu đề.
  • Vẽ t và s.
  • Đặt nhãn x, y.
  • Hiển thị hình bằng show().

Ví dụ

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

Kết quả

Cách vẽ tín hiệu (signal) trong Matplotlib Python?