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

วิธีพล็อต Longitudinal Magnitude Spectrum ใน Matplotlib

การพล็อต Magnitude Spectrum แบบ Longitudinal (dB Scale)

ใช้ magnitude_spectrum() พร้อม scale='dB'

ขั้นตอนหลัก

  • ตั้งค่าขนาดรูปและ padding
  • ตั้งค่า random seed, dt, Fs, t
  • สร้างสัญญาณรบกวนและสัญญาณหลัก s
  • สร้าง figure และ subplots
  • ตั้งค่า title และพล็อต magnitude_spectrum(s, Fs=Fs, scale='dB', color='C1')
  • แสดงผลด้วย 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("Longitudinal Magnitude Spectrum")
axs.magnitude_spectrum(s, Fs=Fs, scale='dB', color='C1')
plt.show()

คำอธิบาย

scale='dB' แสดง magnitude ในหน่วยเดซิเบล (logarithmic scale)