spkit.filter_smooth_sGolay

spkit.filter_smooth_sGolay(X, window_length=127, polyorder=3, deriv=0, delta=1.0, mode='interp', cval=0.0)

Smoothing filter using Savitzky-Golay filter

Smoothing filter using Savitzky-Golay filter

Savitzky-Golay filter for multi-channels signal: From Scipy library

Parameters:
Xarray,
  • input signal single channel (n,) or multi-channel, channel axis should be 1 shape ~ (n,ch)

window_length: int, odd number,
  • length of window, larger the value, more flat drift is removed.

  • use smaller size to retain small duration of fluctuations and curves.

polyorder: int, non-negetive
  • order of polynomial to be fitted. Higher the order, more curvey smooth is captured.

others: deriv=0, delta=1.0, mode=’interp’, cval=0.0
  • parameters as same as in scipy.signal.savgol_filter

  • (deriv=0, delta=1.0, mode=’interp’, cval=0.0)

Returns:
Ysame shape as X,
  • corrected signal

See also

filter_smooth_gauss

Smoothing signal using Gaussian kernel

filter_smooth_mollifier

Smoothing signal using Mollifier

filter_with_kernel

filtering signal using custom kernel

filter_X

Spectral filtering

Examples

#sp.filter_smooth_sGolay
import numpy as np
import matplotlib.pyplot as plt
import spkit as sp
x,fs = sp.data.ppg_sample(sample=1)
x = x[:int(fs*5)]
x = x - x.mean()
t = np.arange(len(x))/fs
xf1 = sp.filter_smooth_sGolay(x.copy(),window_length=31, polyorder=2)
xf2 = sp.filter_smooth_sGolay(x.copy(),window_length=31, polyorder=5)
xf3 = sp.filter_smooth_sGolay(x.copy(),window_length=51, polyorder=3)
plt.figure(figsize=(12,3))
plt.plot(t,x,label='x: signal')
plt.plot(t,xf1,label='xf1: (wL=31, order=2)')
plt.plot(t,xf2,label='xf2: (wL=31, order=5)')
plt.plot(t,xf3,label='xf3: (wL=51, order=3)')
plt.xlim([t[0],t[-1]])
plt.xlabel('time (s)')
plt.ylabel('PPG Signal')
plt.grid()
plt.legend(bbox_to_anchor=(1,1))
plt.title('Savitzky-Golay Smoothing')
plt.tight_layout()
plt.show()
../../_images/spkit-filter_smooth_sGolay-1_00_00.png
plt.figure(figsize=(12,3))
plt.plot(t,x-xf1,label='x-xf1')
plt.plot(t,x-xf2-40,label='x-xf2')
plt.plot(t,x-xf3-80,label='x-xf3')
plt.xlim([t[0],t[-1]])
plt.xlabel('time (s)')
plt.ylabel('Residual')
plt.legend(bbox_to_anchor=(1,1))
plt.grid()
plt.tight_layout()
plt.show()
../../_images/spkit-filter_smooth_sGolay-1_01_00.png