spkit.filtering_pipeline

spkit.filtering_pipeline(X, fs, trim=[0, 0], iir_alpha=0, sg_window=1001, sg_polyorder=1, sg_itr=1, filter_lpf=None, filter_pwrline=None, last_filter_keywords={}, verbose=1)

Filtering Pipeline

Applying sequence of filters

  • Useful to set onces and apply all many samples

  • Any filter.]/operation can be excluded or included using parameters

Sequence of Operations:

  1. Trim/crop the signal
    • if sum(trim)>0: X[int(fs*trim[0]):-int(fs*trim[1])

  2. DC Removal:
    • 2.1) IIR DC Removal : if iir_alpha>0, apply filterDC

    • 2.2) SGolay DC Removal: if sg_window>0, apply filterDC_sGolay

  3. Powerline intereference removal:
  4. Lowpass filter:
    • if filter_lpf is not None, apply filter_X

  5. Last custom filter
    • if len(last_filter_keywords)>0, apply filter_X

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

fs: int,
  • sampling frequency

trim: list, default=[0,0]
  • applied only if sum(trim)>0

  • duration of signal to be removed (triming the signal) from start and end.

  • trim =[1,1] means, removing 1sec from start and 1sec from end

  • X = X[int(fs*trim[0]):-int(fs*trim[1])]

iir_alpha: scalar, default=0
  • applied only if iir_alpha>0

  • alpha for IIR DC Removal filter

(sg_window,sg_polyorder,sg_itr): scalars,
  • parameters for DC removal using sGolay filter, applied only is sg_window>0

  • defaults values sg_window=1001,sg_polyorder=1,sg_itr=1

  • sg_window: window length,sg_polyorder: polynomial order,sg_itr: number of iterations to apply

filter_lpf: scalar, default=None
  • applied only if it not None

  • cut-off frequency for lowpass filter

filter_pwrline: {50,60},default=None
  • applied only if it not None

  • powerline interefence type 50Hz of 60Hz,

last_filter_keywords: dict, deafult ={}
  • applied only if dictionary is not empty

  • parameters to be passed for filter_X, other than X, fs, and verbose.

verbose=1,

Returns:
YFiltered signal

See also

filter_smooth_sGolay

Smoothing signal using Savitzky-Golay filter

filter_smooth_gauss

Smoothing signal using Gaussian kernel

filter_with_kernel

filtering signal using custom kernel

filter_X

Spectral filtering

Notes

#TODO

References

  • wikipedia -

Examples

#sp.filtering_pipeline
import numpy as np
import matplotlib.pyplot as plt
import spkit as sp
x,fs,lead_names = sp.data.ecg_sample_12leads(sample=1)
x = x[:,1]
t = np.arange(len(x))/fs
t1,t2 = 0.5, 0.5
y = sp.filtering_pipeline(x,fs,trim=[t1,t2],sg_window=fs+1,sg_polyorder=1,filter_lpf=150,filter_pwrline=50)
plt.figure(figsize=(12,5))
plt.subplot(311)
plt.plot(t,x)
plt.grid()
plt.xticks(np.arange(0,t[-1],0.5))
plt.xlim([t[0],t[-1]])
plt.ylabel('x: signal')
plt.subplot(312)
plt.plot(t[:len(y)]+t1,y,label='x: signal')
plt.xticks(np.arange(0,t[-1],0.5))
plt.xlim([t[0],t[-1]])
plt.ylabel('y: processed')
plt.grid()
plt.subplot(313)
plt.plot(t[:len(y)]+t1,x[:len(y)]-y)
plt.xticks(np.arange(0,t[-1],0.5))
plt.xlim([t[0],t[-1]])
plt.grid()
plt.ylabel('x-y: residual')
plt.tight_layout()
plt.show()
../../_images/spkit-filtering_pipeline-1.png