1

For the following signal, I am trying to remove the gradual positive drift but retain the spikes using a high pass filter.

enter image description here

I am aware that I can use implement a high pass filter using difference equation. I am familiar with convolution, and I have implemented simple low pass filter using moving average and first order recursive filter in the past. But I am not sure how to use difference equation to implement a high order filter that fits this particular problem. I am looking for a simple starting point using difference equation that I can tweak around, and then maybe move toward something a bit more complicated.

Any starting point would be appreciated.


I've tried difference equation $y[n] = \alpha y[n-1] + x[n] - x[n-1]$ posted on How to remove or filter the drift problem in measured Strain signal?, but I do not get the expected result. The signal can be found here as a Python list, and I chose $\alpha$ value to be 0.8.

enter image description here

Expected result, enter image description here

robert bristow-johnson
  • 20,661
  • 4
  • 38
  • 76
Quazi Irfan
  • 113
  • 5

1 Answers1

1

I get the same thing you do. Another possibility is to just do a detrend on the data.

My take

The top plot is your data, and the DC blocker approach.

The bottom plot is your data, and removing the straight line fit through your data.


Python code

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

alpha = 0.9 b = [1,-1] a = [1,-alpha] dc_blocked_data = signal.lfilter(b,a,data) plt.subplot(211) plt.plot(data) plt.plot(dc_blocked_data)

gradient,bias = np.polyfit(np.arange(0,len(data)),data,1) estimate = gradient*np.arange(0,len(data))+bias linear_subtraction = np.subtract(data,estimate) plt.subplot(212) plt.plot(data) plt.plot(linear_subtraction)

Peter K.
  • 25,714
  • 9
  • 46
  • 91
  • 2
    Good idea. I think it would be even better if you detrend in sections. The slope after a positive and negative peak is quite different, so detrending between the peaks will look better. Of course, you've have to figure how to splice the detrended sections together – Hilmar Nov 23 '21 at 14:46
  • @Hilmar Sure! But that's a whole over level of effort. I'm only in it for the quick points. ;-) – Peter K. Nov 23 '21 at 14:54
  • @PeterK. Thanks. I came up with the exact approach after right after posting this question. But I was wondering if there is any class of difference equation that I can use for this particular problem? – Quazi Irfan Nov 24 '21 at 06:08