如何使用 Python 在 Matplotlib 中繪製縱向幅頻譜?
要繪製幅頻譜,我們可以採取以下步驟 −
- 設定圖形大小並調整子圖之間的留白和周圍的留白。
- 獲取隨機種子值。
- 初始化取樣間隔的 **dt**,然後查詢取樣頻率。
- 建立 **t** 的隨機資料點。
- 要生成噪聲,請使用 numpy 獲取 nse、r、cnse 和**s**。
- 使用 subplots() 方法建立一個圖形和一組子圖。
- 設定圖形標題。
- 繪製縱向幅頻譜。
- 要顯示圖形,請使用 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 # 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("Longitudinal Magnitude Spectrum")
axs.magnitude_spectrum(s, Fs=Fs, scale='dB', color='C1')
plt.show()輸出

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP