用 SciPy 的 signal.savgol_filter 计算的 Savitzky-Golay 导数需要缩放吗?
Savitzky-Golay derivatives, computed with SciPy's signal.savgol_filter need to be scaled?
我正在计算信号的一阶和二阶导数,然后绘图。我选择了 SciPy(信号模块)中实现的 Savitzky-Golay 滤波器。我想知道输出是否需要缩放 - 在同一个过滤器的 Matlab 实现中,指定过滤器的输出需要缩放:
savitzkyGolayFilt(X,N,DN,F) filters the signal X using a
Savitzky-Golay (polynomial) filter. The polynomial order, N, must be
less than the frame size, F, and F must be odd. DN specifies the
differentiation order (DN=0 is smoothing). For a DN higher than zero,
you'll have to scale the output by 1/T^DN to acquire the DNth smoothed
derivative of input X, where T is the sampling interval.
但是,我在 SciPy 的文档中没有找到任何类似的内容。有没有人试过并知道 Python 中的输出是否正确并且不需要进一步缩放?我 运行 一阶导数的代码行是: first_deriv = signal.savgol_filter(spectra_signal,sigma=7,2, deriv=1, delta=3.1966)
spectra_signal 是我的 "y" 变量,delta 是 "x" 的变体多变的。
此外,我尝试在不使用 savgol_filter 的情况下计算一阶导数,而是在平滑信号上使用 np.diff
(基于公式导数 = dy/dx)。first_deriv_alternative = np.diff(signal.savgol_filter(spectra_signal, sigma=7,2))/3.1966
。而且结果也不一样
工作代码示例:
import numpy as np
from scipy import signal
x =[405.369888, 408.561553, 411.753217, 414.944882, 418.136547, 421.328212, 424.519877, 427.711541, 430.903206]
y =[5.001440644264221191e-01,
4.990128874778747559e-01,
4.994551539421081543e-01,
5.002806782722473145e-01,
5.027571320533752441e-01,
5.053851008415222168e-01,
5.082427263259887695e-01,
5.122825503349304199e-01,
5.167465806007385254e-01]
#variation of x variable, constant step
sampling_step = x[1]-x[0]
#Method 1: using savgol_filter
deriv1_method1 = signal.savgol_filter(y,5,2,deriv=1, delta=sampling_step)
#Method 2: using np.diff to compute the derivative of the filtered original data
dy=np.diff(signal.savgol_filter(y, 5,2))
dx=np.diff(x)
deriv1_method2=dy/dx
#Method 3: filtering the first derivative of the original data
deriv1_method3=signal.savgol_filter((np.diff(y)/np.diff(x)), 5,2)
在引擎盖下 signal.savgol_filter
使用 signal.savgol_coeffs
如果你看 the source code 它说 "The coefficient assigned to y[deriv] scales the result to take into account the order of the derivative and the sample spacing"。结果在执行拟合和 convolve1d
之前被缩放。所以默认情况下,结果似乎已经根据导数的顺序进行了缩放。
我认为在计算 Savitzky-Golay 滤波器后执行导数不会给你相同的结果,因为在这种情况下,你正在计算已经过滤的频谱的导数,而在第一种情况下你正在执行执行拟合和缩放之前的导数。
我正在计算信号的一阶和二阶导数,然后绘图。我选择了 SciPy(信号模块)中实现的 Savitzky-Golay 滤波器。我想知道输出是否需要缩放 - 在同一个过滤器的 Matlab 实现中,指定过滤器的输出需要缩放:
savitzkyGolayFilt(X,N,DN,F) filters the signal X using a Savitzky-Golay (polynomial) filter. The polynomial order, N, must be less than the frame size, F, and F must be odd. DN specifies the differentiation order (DN=0 is smoothing). For a DN higher than zero, you'll have to scale the output by 1/T^DN to acquire the DNth smoothed derivative of input X, where T is the sampling interval.
但是,我在 SciPy 的文档中没有找到任何类似的内容。有没有人试过并知道 Python 中的输出是否正确并且不需要进一步缩放?我 运行 一阶导数的代码行是: first_deriv = signal.savgol_filter(spectra_signal,sigma=7,2, deriv=1, delta=3.1966)
spectra_signal 是我的 "y" 变量,delta 是 "x" 的变体多变的。
此外,我尝试在不使用 savgol_filter 的情况下计算一阶导数,而是在平滑信号上使用 np.diff
(基于公式导数 = dy/dx)。first_deriv_alternative = np.diff(signal.savgol_filter(spectra_signal, sigma=7,2))/3.1966
。而且结果也不一样
工作代码示例:
import numpy as np
from scipy import signal
x =[405.369888, 408.561553, 411.753217, 414.944882, 418.136547, 421.328212, 424.519877, 427.711541, 430.903206]
y =[5.001440644264221191e-01,
4.990128874778747559e-01,
4.994551539421081543e-01,
5.002806782722473145e-01,
5.027571320533752441e-01,
5.053851008415222168e-01,
5.082427263259887695e-01,
5.122825503349304199e-01,
5.167465806007385254e-01]
#variation of x variable, constant step
sampling_step = x[1]-x[0]
#Method 1: using savgol_filter
deriv1_method1 = signal.savgol_filter(y,5,2,deriv=1, delta=sampling_step)
#Method 2: using np.diff to compute the derivative of the filtered original data
dy=np.diff(signal.savgol_filter(y, 5,2))
dx=np.diff(x)
deriv1_method2=dy/dx
#Method 3: filtering the first derivative of the original data
deriv1_method3=signal.savgol_filter((np.diff(y)/np.diff(x)), 5,2)
在引擎盖下 signal.savgol_filter
使用 signal.savgol_coeffs
如果你看 the source code 它说 "The coefficient assigned to y[deriv] scales the result to take into account the order of the derivative and the sample spacing"。结果在执行拟合和 convolve1d
之前被缩放。所以默认情况下,结果似乎已经根据导数的顺序进行了缩放。
我认为在计算 Savitzky-Golay 滤波器后执行导数不会给你相同的结果,因为在这种情况下,你正在计算已经过滤的频谱的导数,而在第一种情况下你正在执行执行拟合和缩放之前的导数。