# Weird filtering result

Im filtering some ecg data for mainly just powerline noise (50 Hz), but get some weird result between the R-peaks (main spike). I have just done some tried with some basic filtering like this:

import scipy.signal as ss
Fs = 201
HPC = 0.5
LPC = 40
Q = 30
b_lp, a_lp = ss.butter(5, HPC/(0.5*Fs), btype='low', analog=False, output='ba')
b_hp, a_hp = ss.butter(5, LPC/(0.5*Fs), btype='high', analog=False, output='ba')
b_n, a_notch = ss.iirnotch(50/(0.5*Fs), Q)
hp_f = ss.lfilter(b_hp,a_hp,signal)
LoHi_f = ss.lfilter(b_lp,a_lp,hp_filtered)
cleaned = ss.lfilter(b_notch,a_notch,LoHi_filtered)


(I know I could use sos filter as it is more stable, but at this order it shouldn't be a problem and it is slower than 'ba'. ) This is how the result looks, the top one is the filtered and the bottom is just unfiltered. As you can see there is a drift an upward drift in the top one. THis is what I want to avoid.

Any tips for avoiding this?

• Could you be more specific about what you're trying to avoid? Those plots look pretty similar to the uninitiated. Commented Jun 13, 2022 at 14:54
• There is a weird "drift" in between the R peaks. This is what i want to avoid. Commented Jun 13, 2022 at 15:04
• Assuming that the top is "before" and the bottom is "after" this looks pretty normal. It's hard to make though, since you can't see where 0 is. Properly labelling and annotating your graphs would really help here. It's also unclear why you use a notch at 50Hz when your upper bandlimited is only 40 Hz. Q=30 is a lot, so it's going to take the notch filter quite some time to kick into gear. Commented Jun 13, 2022 at 18:34
• I can't help you with your filtering problem but perhaps the material at the following web page could be of some value to you: dsprelated.com/showarticle/1383.php Commented Jun 14, 2022 at 10:49
• @Hilmar as mentioned the top is after filtering. The reason the graphs is is not annotated is that it is taken from a prototype ecg monotoring device, and this should matter with the problem. Can you explain how it would take more time for the notch filter to kick into gear, as I understood it, it defines the "sharpness" of the dip/filtered out area. Commented Jun 14, 2022 at 12:50