📜  使用 Matplotlib - Python绘制功率谱密度

📅  最后修改于: 2022-05-13 01:55:30.842000             🧑  作者: Mango

使用 Matplotlib - Python绘制功率谱密度

matplotlib.pyplot.psd()函数用于绘制功率谱密度。在用于评估功率谱密度(例如,P xx )的 Welch 平均周期图方法中,矢量“x”被均分为 NFFT 段。每个段由函数窗口窗口化并由函数detrend 去趋势。段之间的重叠由“noverlap”给出。每段“i”的|fft(i)|*2 一起平均以计算 P,并使用缓冲缩放来校正由于开窗导致的功率损失。

注意:如果 len(x) < NFFT,则使用 NFFT 填充将为零。

其他参数:
**kwargs关键字参数用于控制 Line2D 属性

PropertyDescription
agg_filtera filter function that takes a (m, n, 3) float array and a dpi value that returns a (m, n, 3) array
alphafloat
animatedbool
antialiased or aabool
clip_boxBbox
clip_onbool
clip_path[(Path, Transform)|Patch|None]
color or ccolor
containscallable
dash_capstyle{‘butt’, ’round’, ‘projecting’}
dash_joinstyle{‘miter’, ’round’, ‘bevel’}
dashessequence of floats(on/off ink in points) or (None, None)
drawstyle or ds{‘default’, ‘steps’, ‘steps-pre’, ‘steps-mid’, ‘steps-post’}, default: ‘default’
figurefigure
fillstyle{‘full’, ‘left’, ‘right’, ‘bottom’, ‘top’, ‘none’}
gidstr
in_layoutbool
labelobject
linestyle or lsl{‘-‘, ‘–‘, ‘-.’, ‘:’, ”, (offset, on-off-seq), …}
linewidth or lwfloat
markermarker style
markeredgecolor or meccolor
Cmarkeredgewidth or mewfloat
markerfacecolor or mfccolor
markerfacecoloralt or mfcaltcolor
markersize or msfloat
markeveryNone or int or (int, int) or slice or List[int] or float or (float, float)
path_effectsAbstractPathEffect
pickerfloat or callable[[Artist, Event], Tuple[bool, dict]]
pickradiusfloat
rasterizedbool or None
sketch_params(scale: float, length: float, randomness: float) 
 
snapbool or None
solid_capstyle{‘butt’, ’round’, ‘projecting’} 
 
solid_joinstyle{‘miter’, ’round’, ‘bevel’}
transformmatplotlib.transforms.Transform
urlstr
visiblebool
xdata1D array
ydata1D array
zorderfloat

示例 1:

Python3
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.gridspec as gridspec
 
# set random state for reproducibility
np.random.seed(19695601)
 
diff = 0.01
ax = np.arange(0, 10, diff)
n = np.random.randn(len(ax))
by = np.exp(-ax / 0.05)
 
cn = np.convolve(n, by) * diff
cn = cn[:len(ax)]
s = 0.1 * np.sin(2 * np.pi * ax) + cn
 
plt.subplot(211)
plt.plot(ax, s)
plt.subplot(212)
plt.psd(s, 512, 1 / diff)
 
plt.show()


Python3
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.gridspec as gridspec
 
 
# set random valueto ensure reproducibility
random_rep = np.random.RandomState(19680801) 
 
frame_per_second = 1000
a = np.linspace(0, 0.3, 301)
b = np.array([2, 8]).reshape(-1, 1)
c = np.array([150, 140]).reshape(-1, 1)
d = (b * np.exp(2j * np.pi * c * a)).sum(axis = 0) + 5 * random_rep.randn(*a.shape)
 
figure, (a0, a1) = plt.subplots(ncols = 2,
                                constrained_layout = True)
 
e = np.arange(-50, 30, 10)
f = (e[0], e[-1])
g = np.arange(-500, 550, 200)
 
a0.psd(d, NFFT = 301,
       Fs = frame_per_second,
       window = mlab.window_none,
       pad_to = 1024,
       scale_by_freq = True)
 
a0.set_title('Periodo-gram')
a0.set_yticks(e)
a0.set_xticks(g)
a0.grid(True)
a0.set_ylim(f)
 
a1.psd(d, NFFT = 150,
       Fs = frame_per_second,
       window = mlab.window_none,
       pad_to = 512,
       noverlap = 75,
       scale_by_freq = True)
 
a1.set_title('Welch')
a1.set_xticks(g)
a1.set_yticks(e)
 
# overwriting the y-label added by `psd`
a1.set_ylabel('')
a1.grid(True)
a1.set_ylim(f)
 
plt.show()


输出:

使用 Matplotlib 在 Python 中的光谱密度

示例 2:

Python3

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.gridspec as gridspec
 
 
# set random valueto ensure reproducibility
random_rep = np.random.RandomState(19680801) 
 
frame_per_second = 1000
a = np.linspace(0, 0.3, 301)
b = np.array([2, 8]).reshape(-1, 1)
c = np.array([150, 140]).reshape(-1, 1)
d = (b * np.exp(2j * np.pi * c * a)).sum(axis = 0) + 5 * random_rep.randn(*a.shape)
 
figure, (a0, a1) = plt.subplots(ncols = 2,
                                constrained_layout = True)
 
e = np.arange(-50, 30, 10)
f = (e[0], e[-1])
g = np.arange(-500, 550, 200)
 
a0.psd(d, NFFT = 301,
       Fs = frame_per_second,
       window = mlab.window_none,
       pad_to = 1024,
       scale_by_freq = True)
 
a0.set_title('Periodo-gram')
a0.set_yticks(e)
a0.set_xticks(g)
a0.grid(True)
a0.set_ylim(f)
 
a1.psd(d, NFFT = 150,
       Fs = frame_per_second,
       window = mlab.window_none,
       pad_to = 512,
       noverlap = 75,
       scale_by_freq = True)
 
a1.set_title('Welch')
a1.set_xticks(g)
a1.set_yticks(e)
 
# overwriting the y-label added by `psd`
a1.set_ylabel('')
a1.grid(True)
a1.set_ylim(f)
 
plt.show()

输出:

使用 Matplotlib 在 Python 中的光谱密度