Savitzky-Golay 过滤在一维中给出不正确的导数

问题描述 投票:0回答:2

我有一个 x 和 y 数据集,其中 x 为自变量,y 为因变量。

y=2x


我向“y”添加一些噪音并应用 scipy Savitzky Golay 过滤器。当我尝试获得 y 的一阶导数时,我得到的导数为零。 我知道这是因为过滤器只接受“y”作为输入。我想要一个同时考虑 x 和 y 的过滤器,并为我提供导数值。

在这里,我展示了我的实现,其中的图表指示了不正确的数据。

import numpy as np
from scipy import signal
import matplotlib.pyplot as plt

# create some sample twoD data
x = np.linspace(-3,3,100)
y = 2*x
y = y + np.random.normal(0, 0.2, y.shape)

# filter it
Zn = signal.savgol_filter(y, window_length=29, polyorder=4, deriv=0)
Zf = signal.savgol_filter(y, window_length=29, polyorder=4, deriv=1)
# do some plotting
plt.plot(x,y, label = 'Input')
plt.plot(x,Zn, label= 'Savitzky-Golay filtered')
plt.plot(x,Zf, label= 'Savitzky-Golay filtered - 1st derivative')
plt.legend()
plt.show()

结果:

求导结果: dy/dx = 2。
我需要 Savitzky-Golay 过滤器来为我提供这个结果。请帮助我实现一个考虑两个变量的Python实现。

python scipy filtering signal-processing smoothing
2个回答
9
投票

要在

deriv
 中使用 
savgol_filter
> 0,还必须给出 x 坐标的间距。解决方法很简单:在通话中的
delta=x[1] - x[0]
之后添加
deriv=1

Zf = signal.savgol_filter(y, window_length=29, polyorder=4, deriv=1, delta=x[1] - x[0])

7
投票

编辑:针对英语语言用法进行更正

在给定的情况下,

y
相对于
x
的导数不是
dy/dx = 2
,而是
dy/1.0 = 0.12
,因为
y
被定义为
y = 2x = np.linspace(-3,3,100)

变量

dx
未定义为
delta
,而是使用默认值
delta=1.0

因此,使用

delta
等于
dx
确实可以解决问题。

dx = x[0] - x[1]
Zf = signal.savgol_filter(y, window_length=29, polyorder=4, deriv=1, delta=dx)
© www.soinside.com 2019 - 2024. All rights reserved.