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

  • $\begingroup$ @PeterK. Thank you for looking into it. I've tried the difference equation posted on that related question, but I did not get expected result. I've updated the question with additional information. $\endgroup$ Nov 23, 2021 at 12:50
  • 1
    $\begingroup$ OK! I've reopened it. Thanks for adding the extra detail. It looks like your implementation has an extra difference in there that shouldn't be there. I'll have a quick look now. $\endgroup$
    – Peter K.
    Nov 23, 2021 at 13:56
  • 1
    $\begingroup$ this HPF of yours is a simple DC-blocking filter. try an $\alpha$ closer to 1. but keep it strictly less than 1. $\endgroup$ Nov 23, 2021 at 14:41
  • $\begingroup$ @Quazi_Irfan If you desire a linear-phase DC cancellation filter have a look at the following web page: dsprelated.com/showarticle/58.php $\endgroup$ Nov 23, 2021 at 16:15
  • $\begingroup$ Related: High Pass filter $\endgroup$ Nov 24, 2021 at 9:15

1 Answer 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)

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)
  • 2
    $\begingroup$ 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 $\endgroup$
    – Hilmar
    Nov 23, 2021 at 14:46
  • $\begingroup$ @Hilmar Sure! But that's a whole over level of effort. I'm only in it for the quick points. ;-) $\endgroup$
    – Peter K.
    Nov 23, 2021 at 14:54
  • $\begingroup$ @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? $\endgroup$ Nov 24, 2021 at 6:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.