Matplotlib是Python中的一個庫,它是數字的-NumPy庫的數學擴展。 Pyplot是Matplotlib模塊的基於狀態的接口,該模塊提供了MATLAB-like接口。在Pyplot中可以使用各種圖,例如線圖,輪廓圖,直方圖,散點圖,3D圖等。
matplotlib.pyplot.magnitude_spectrum()函數:
matplotlib庫的pyplot模塊中的magnitude_spectrum()函數用於繪製幅度譜。通常,它會計算序列的幅度譜並繪製出圖。
用法: magnitude_spectrum(x, Fs=2, Fc=0, window=mlab.window_hanning, pad_to=None, sides=’default’, **kwargs)
參數:此方法接受以下描述的參數:
- x:此參數是數據序列。
- Fs:此參數是標量。默認值為2。
- window:此參數將數據段作為參數,並返回該段的窗口版本。其默認值為window_hanning()
- sides:此參數指定要返回光譜的哪一側。它可以具有以下值:‘default’,‘onesided’和‘twosided’。
- pad_to:此參數包含填充數據段的整數值。
- Fc:此參數還包含一個整數值,用於抵消曲線圖的x範圍以反映頻率範圍。其默認值為0
返回值:這將返回以下內容:
- 光譜:這將返回以弧度為單位的角度光譜。
- 頻率:這將返回與頻譜中的元素相對應的頻率。
- 線:這將返回此函數創建的行。
結果是(頻譜,頻率,線)
以下示例說明了matplotlib.pyplot中的matplotlib.pyplot.magnitude_spectrum()函數:
範例1:
# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(10**5)
dt = 0.0001
Fs = 1 / dt
geeks = np.array([24.40, 110.25, 20.05, 22.00, 61.90,
7.80, 15.00, 22.80, 34.90, 57.30])
nse = np.random.randn(len(geeks))
r = np.exp(-geeks / 0.05)
s = 0.1 * np.sin(2 * np.pi * geeks) + nse
# plot magnitude_spectrum
plt.magnitude_spectrum(s, Fs = Fs)
plt.title('matplotlib.pyplot.magnitude_spectrum() function Example',
fontweight ="bold")
plt.show()
輸出:
範例2:
# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
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(2 * np.pi * t) + cnse
# plot simple spectrum
plt.subplot(2, 1, 1)
plt.plot(t, s)
plt.title('matplotlib.pyplot.magnitude_spectrum() function Example',
fontweight ="bold")
plt.subplot(2, 1, 2)
plt.magnitude_spectrum(s, Fs = Fs)
plt.show()
輸出:
相關用法
注:本文由純淨天空篩選整理自SHUBHAMSINGH10大神的英文原創作品 matplotlib.pyplot.magnitude_spectrum() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。