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

Cách vẽ phổ biên độ (magnitude spectrum) trong Matplotlib Python?

Để vẽ phổ biên độ (magnitude spectrum):

  • 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ẽ phổ biên độ bằng magnitude_spectrum().
  • 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("Magnitude Spectrum")
axs.magnitude_spectrum(s, Fs=Fs, color='C1')
plt.show()

Kết quả

Cách vẽ phổ biên độ (magnitude spectrum) trong Matplotlib Python?