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

Vẽ mật độ phổ công suất (Power Spectral Density) trong Matplotlib


Các bước

  1. Thiết lập kích thước hình và padding.
  2. Khởi tạo biến dt.
  3. Tạo dữ liệu t, nse, r, cnse, s bằng numpy.
  4. Tạo figure và subplots (2 hàng, 1 cột).
  5. Vẽ tín hiệu t, s bằng plot().
  6. Vẽ PSD bằng psd().
  7. Gọi 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

dt = 0.01
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(2 * np.pi * t) + cnse

fig, (ax0, ax1) = plt.subplots(2, 1)
ax0.plot(t, s)
ax1.psd(s, 512, 1 / dt)
plt.show()

Kết quả

Vẽ mật độ phổ công suất (Power Spectral Density) trong Matplotlib