# Implementing basic high pass filter using difference equation

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

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.

Expected result,

• @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. Nov 23, 2021 at 12:50
• 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.
– Peter K.
Nov 23, 2021 at 13:56
• this HPF of yours is a simple DC-blocking filter. try an $\alpha$ closer to 1. but keep it strictly less than 1. Nov 23, 2021 at 14:41
• @Quazi_Irfan If you desire a linear-phase DC cancellation filter have a look at the following web page: dsprelated.com/showarticle/58.php Nov 23, 2021 at 16:15
• Related: High Pass filter Nov 24, 2021 at 9:15

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

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)