pythonscipyfilteringsignal-processingsmoothing

Savitzky-Golay filtering giving incorrect derivative in 1D


I have an x and y dataset, with x as the independent variable and y as the dependent variable.

y=2x


I add some noise to 'y' and apply the scipy Savitzky Golay filter. When I attempt to get the first derivative of y, I get the derivative as zero. I understand this is because of the filter takes only 'y' as the input. I would want to have a filter that considers both x and y, and also provide me with a derivative value.

Here I show my implementation with the plots indicating incorrect data.

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()

Result:enter image description here

The derivative result: dy/dx = 2.
I need the Savitzky-Golay filter to provide me this result. Please help me with a python implementation that considers two variables.


Solution

  • Edit: Correction made for English language usage.

    In the given case, the derivative of y with respect to x is not dy/dx = 2, but rather dy/1.0 = 0.12 because y is defined as y = 2x = np.linspace(-3,3,100).

    The variable dx was not defined as delta, and the default value delta=1.0 was used instead.

    So, using delta which is equal to dx indeed resolves the issue.

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