gpt4 book ai didi

python - Savitzky-Golay 过滤在 1D 中给出不正确的导数

转载 作者:行者123 更新时间:2023-12-03 20:28:01 43 4
gpt4 key购买 nike

我有一个 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()

结果: enter image description here

衍生结果:
dy/dx = 2。

我需要 Savitzky-Golay 过滤器来为我提供这个结果。请帮助我考虑两个变量的python实现。

最佳答案

使用 deriv > 0 在 savgol_filter ,您还必须给出 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])

关于python - Savitzky-Golay 过滤在 1D 中给出不正确的导数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56486999/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com