matplotlib.pyplot.psd函数学习及例程
matplotlib.pyplot.psd(x, NFFT=None, Fs=None, Fc=None, detrend=None, window=None, noverlap=None, pad_to=None,
sides=None, scale_by_freq=None, return_line=None, *, data=None, **kwargs)
Plot the power spectral density.绘制功率谱密度。
The power spectral density PxxPxx by Welch’s average periodogram method. The vector x is divided into NFFT length segments. Each segment is detrended by function detrend and windowed by function window. noverlap gives the length of the overlap between segments. The |fft(i)|^2 of each segment ii are averaged to compute PxxPxx, with a scaling to correct for power loss due to windowing.
⽤韦尔奇平均周期图法计算功率谱密度。向量x被划分为nfft长度段。每个段都被函数detend和函数window所删除。Noverlap给出了段之间重叠的长度。每个II段的fft(i)^2平均计算PxPxx,并缩放以纠正窗⼝化造成的功率损失。
If len(x) < NFFT, it will be zero padded to NFFT.
如果len(x)<nfft,它将被零填充到nfft。
Parameters:
x : 1-D array or sequence
Array or sequence containing the data
包含数据的数组或序列
Fs : scalar
The sampling frequency (samples per time unit). It is used to calculate the Fourier frequencies, freqs, in cycles per time unit. The default value is 2.
采样频率(每个时间单位的采样点数)。它⽤于计算傅⽴叶频率,频率,以周期/时间单位。默认值为2。
window : callable or ndarray
A function or a vector of length NFFT. To create window vectors see window_hanning, window_none, numpy.blackman, numpy.hamming, numpy.bartlett, scipy.signal, _window, etc. The de
fault is window_hanning. If a function is passed as the argument, it must take a data segment as an argument and return the windowed version of the segment.
长度为nfft的函数或向量。要创建窗⼝向量,请参见window_hanning、window_none、numpy.blackman、numpy.hamming、numpy.bartlett、scipy.signal、_window等。默认值为window_hanning。如果函数作为参数传递,则必须将数据段作为参数,并返回段的窗⼝版本。
sides : {‘default’, ‘onesided’, ‘twosided’}
Specifies which sides of the spectrum to return. Default gives the default behavior, which returns one-sided for real data and both for complex data. ‘onesided’ forces the return of a one-sided spectrum, while ‘twosided’ forces two-sided.
指定要返回频谱的哪⼀侧。默认值提供默认⾏为,对于实际数据和复杂数据都返回单⾯。单侧的⼒是单侧谱的返回,⽽“双侧的”⼒是双侧谱的返回。
pad_to : int
The number of points to which the data segment is padded when performing the FFT. This can be different from NFFT, which specifies the number of data points used. While not increasing the actual
resolution of the spectrum (the minimum distance between resolvable peaks), this can give more points in the plot, allowing for more detail. This corresponds to the n parameter in the call to fft(). The default is None, which sets pad_to equal to NFFT
执⾏FFT时填充数据段的点数。这可能不同于NFFT,它指定使⽤的数据点数量。虽然不能增加光谱的实际分辨率(可分辨峰之间的最⼩距离),但这可以在图中给出更多的点,从⽽提供更多的细节。这对应于对fft()的调⽤中的n参数。默认值为⽆,它将pad_设置为等于nfft
NFFT : int
The number of data points used in each block for the FFT. A power 2 is most efficient. The default value is 256. This should NOT be used to get zero padding, or the scaling of the result will be incorrect. Use pad_to for this instead.
在每个块中⽤于FFT的数据点的数⽬。功率2是最有效的。默认值为256。这不应该⽤于获得零填充,否则结果的缩放将不正确。⽤pad_o 代替。
detrend : {‘none’, ‘mean’, ‘linear’} or callable, default ‘none’
The function applied to each segment before fft-ing, designed to remove the mean or linear trend. Un
like in MATLAB, where the detrend parameter is a vector, in Matplotlib is it a function. The mlab module defines detrend_none, detrend_mean, and detrend_linear, but you can use a custom function as well. You can also use a string to choose one of the functions:
‘none’ calls detrend_none. ‘mean’ calls detrend_mean. ‘linear’ calls detrend_linear.
去除线性分量/去趋势化
在快速傅⽴叶变换前应⽤于每⼀段的函数,⽤来消除平均或线性趋势。与matlab不同,detrend参数是⼀个向量,matplotlib中是⼀个函数。MLAB模块定义了Detrend ou none、Detrend ou mean和Detrend ou linear,但也可以使⽤⾃定义函数。您还可以使⽤字符串来选择函数之⼀:“none”调⽤detrend“none”。 ‘mean’ 调⽤“detrend_mean”。linear’调⽤decretend_linear。
scale_by_freq : bool, optional
Specifies whether the resulting density values should be scaled by the scaling frequency, which gives density in units of
Hz^-1. This allows for integration over the returned frequency values. The default is True for MATLAB compatibility.
指定是否应按⽐例频率缩放⽣成的密度值,⽐例频率以hz ^-1为单位提供密度。这样可以集成返回的频率值。对于matlab兼容性,默认值为true。
noverlap : int
The number of points of overlap between segments. The default value is 0 (no overlap).
段之间重叠点的数⽬。默认值为0(⽆重叠)。
Fc : int
The center frequency of x (defaults to 0), which offsets the x extents of the plot to reflect the frequency range used when a signal is acquired and then filtered and downsampled to baseband.
x的中⼼频率(默认值为0),它偏移绘图的x范围,以反映获取信号时使⽤的频率范围,然后将其过滤和减采样到基带。
return_line : bool
Whether to include the line object plotted in the returned values. Default is False.
是否在返回值中包含绘制的线条对象。默认值为假。
Returns:
Pxx : 1-D array
The values for the power spectrum P_{xx} before scaling (real valued).
缩放前功率谱p_xx的值(实值)。
freqs : 1-D array
The frequencies corresponding to the elements in Pxx.
对应于pxx中元素的频率。
line : a Line2D instance
The line created by this function. Only returned if return_line is True.
此函数创建的⾏。仅当return_line为真时返回。
Other Parameters:
**kwargs
Keyword arguments control the Line2D properties:
关键字参数控制Line2d属性:
| Property | Description |
agg_filter a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array alpha float or None
animated bool
antialiased or aa bool
clip_box Bbox
clip_on bool
clip_path [(Path, Transform) | Patch | None]
color or c color
contains callable
dash_capstyle {‘butt’, ‘round’, ‘projecting’}
dash_joinstyle {‘miter’, ‘round’, ‘bevel’}
dashes sequence of floats (on/off ink in points) or (None, None)
drawstyle or ds {‘default’, ‘steps’, ‘steps-pre’, ‘steps-mid’, ‘steps-post’}, default: ‘default’figure Figure
fillstyle {‘full’, ‘left’, ‘right’, ‘bottom’, ‘top’, ‘none’}
gid str
in_layout bool
label object
linestyle or ls {’-’, ‘–’, ‘-.’, ‘:’, ‘’, (offset, on-off-seq), …}
linewidth or lw float
marker marker style
markeredgecolor or mec color
markeredgewidth or mew float
markerfacecolor or mfc color
markerfacecoloralt or mfcalt color
matplotlib中subplot
markersize or ms float
markevery None or int or (int, int) or slice or List[int] or float or (float, float)
path_effects AbstractPathEffect
picker float or callable[[Artist, Event], Tuple[bool, dict]]
pickradius float
rasterized bool or None
sketch_params (scale: float, length: float, randomness: float)
snap bool or None
solid_capstyle {‘butt’, ‘round’, ‘projecting’}
solid_joinstyle {‘miter’, ‘round’, ‘bevel’}
ansforms.Transform
url str
visible bool
xdata 1D array
ydata 1D array
zorder float
Notes
For plotting, the power is plotted as 10log10(Pxx)10log10(Pxx) for decibels, though Pxx itself is returned.对于绘图,功率以10log10(pxx)表⽰分贝,但pxx本⾝返回。
References
Bendat & Piersol – Random Data: Analysis and Measurement Procedures, John Wiley & Sons (1986) Bendat&Piersol——随机数据:分析和测量程序,John Wiley&Sons(1986)
Note
In addition to the above described arguments, this function can take a data keyword argument. If such a data argument is given, the following arguments are replaced by data[]:
除了上述参数外,此函数还可以采⽤data关键字参数。如果给定了这样的数据参数,则以下参数将被数据[]替换:
All arguments with the following names: ‘x’.
Objects passed as data must support item access (data[]) and membership test ( in data).
具有以下名称的所有参数:“x”。
作为数据传递的对象必须⽀持项访问(data[])和成员资格测试(data中的)。
Examples using matplotlib.pyplot.psd
Plotting Power Spectral Density (PSD) in Matplotlib.
The PSD is a common plot in the field of signal processing. NumPy has many useful libraries for computing a PSD. Below we demo a few examples of how this can be accomplished and visualized with Matplotlib.
绘制Matplotlib中的功率谱密度(PSD)。
PSD是信号处理领域的⼀种常⽤图形。numpy有许多⽤于计算PSD的有⽤库。下⾯我们演⽰⼀些如何⽤matplotlib实现和可视化的⽰例。
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.mlab as mlab
idspec as gridspec
# Fixing random state for reproducibility
np.random.seed(19680801)
dt = 0.01
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
plt.subplot(211)
plt.plot(t, s)
plt.subplot(212)
plt.psd(s, 512, 1 / dt)
plt.show()
`
Compare this with the equivalent Matlab code to accomplish the same thing:
将其与等效的matlab代码进⾏⽐较,以完成相同的任务:
dt = 0.01;
t = [0:dt:10];
nse = randn(size(t));
r = exp(-t/0.05);
cnse = conv(nse, r)*dt;
cnse = cnse(1:length(t));
s = 0.1*sin(2*pi*t) + cnse;
subplot(211)
plot(t,s)
subplot(212)
psd(s, 512, 1/dt)
Below we’ll show a slightly more complex example that demonstrates how padding affects the resulting PSD.下⾯我们将展⽰⼀个稍微复杂⼀些的⽰例,演⽰填充如何影响⽣成的PSD。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。