当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python SciPy signal.savgol_filter用法及代码示例


本文简要介绍 python 语言中 scipy.signal.savgol_filter 的用法。

用法:

scipy.signal.savgol_filter(x, window_length, polyorder, deriv=0, delta=1.0, axis=-1, mode='interp', cval=0.0)#

将 Savitzky-Golay 过滤器应用于数组。

这是一个一维滤波器。如果 x 的维度大于 1,则 axis 确定应用过滤器的轴。

参数

x array_like

要过滤的数据。如果x不是单精度或双精度浮点数组,它将被转换为类型numpy.float64过滤前。

window_length int

滤波器窗口的长度(即系数的数量)。如果mode为‘interp’,window_length必须小于或等于x的大小。

polyorder int

用于拟合样本的多项式的阶数。多阶必须小于window_length。

deriv 整数,可选

要计算的导数的阶数。这必须是一个非负整数。默认为0,表示不区分过滤数据。

delta 浮点数,可选

将应用过滤器的样本的间距。这仅在 deriv > 0 时使用。默认值为 1.0。

axis 整数,可选

沿其应用过滤器的数组 x 的轴。默认值为 -1。

mode str,可选

必须是‘mirror’, ‘constant’, ‘nearest’, ‘wrap’或‘interp’。这决定了应用滤波器的填充信号的扩展类型。当模式为‘constant’时,填充值由cval给定。有关 ‘mirror’, ‘constant’, ‘wrap’ 和 ‘nearest’ 的更多详细信息,请参阅注释。当选择‘interp’模式(默认)时,不使用扩展名。相反,度多阶多项式适合边的最后 window_length 值,并且该多项式用于评估最后 window_length //2 个输出值。

cval 标量,可选

如果模式为‘constant’,则要填充超过输入边的值。默认值为 0.0。

返回

y ndarray,形状与x

过滤后的数据。

注意

模式选项的详细信息:

‘mirror’:

Repeats the values at the edges in reverse order. The value closest to the edge is not included.

‘nearest’:

The extension contains the nearest input value.

‘constant’:

The extension contains the value given by the cval argument.

‘wrap’:

The extension contains the values from the other end of the array.

例如,如果输入为 [1, 2, 3, 4, 5, 6, 7, 8],window_length 为 7,则以下显示了各种模式选项的扩展数据(假设 cval 为 0):

mode       |   Ext   |         Input          |   Ext
-----------+---------+------------------------+---------
'mirror'   | 4  3  2 | 1  2  3  4  5  6  7  8 | 7  6  5
'nearest'  | 1  1  1 | 1  2  3  4  5  6  7  8 | 8  8  8
'constant' | 0  0  0 | 1  2  3  4  5  6  7  8 | 0  0  0
'wrap'     | 6  7  8 | 1  2  3  4  5  6  7  8 | 1  2  3

例子

>>> import numpy as np
>>> from scipy.signal import savgol_filter
>>> np.set_printoptions(precision=2)  # For compact display.
>>> x = np.array([2, 2, 5, 2, 1, 0, 1, 4, 9])

使用窗口长度为 5 和 2 次多项式的过滤器。对所有其他参数使用默认值。

>>> savgol_filter(x, 5, 2)
array([1.66, 3.17, 3.54, 2.86, 0.66, 0.17, 1.  , 4.  , 9.  ])

请注意,x 中的最后五个值是抛物线的样本,因此当 mode='interp'(默认值)与 polyorder=2 一起使用时,最后三个值保持不变。比较一下,例如,模式='最近'

>>> savgol_filter(x, 5, 2, mode='nearest')
array([1.74, 3.03, 3.54, 2.86, 0.66, 0.17, 1.  , 4.6 , 7.97])

相关用法


注:本文由纯净天空筛选整理自scipy.org大神的英文原创作品 scipy.signal.savgol_filter。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。