# Removal of gradually-increasing dc component (offline, zero-phase)

A program I'm writing produces a signal with a dc component that increases over time. I need some kind of post-process which can remove the dc content, without affecting the phase or magnitude of the interesting high-frequency content.

So far, I've tried running high-pass flat-passband IIR filters forwards then backwards over the signal. This works fine for the most part, but introduces large low-frequency oscillations at the end of the output.

I've also tried taking the FFT of the signal, and gradually reducing the magnitude of the lowest bins. The problem with this approach is that this is essentially circular convolution, so I get artefacts 'wrapping round' at the beginning and end of the signal.

Zero-padding the input doesn't seem to help in either of these approaches, because the signal goes from large-offset to zero in the space of a single sample. IIR filters are obviously going to have a hard time with this, and the FFT approach does too.

Some examples: This is the kind of raw output signal I'm generating.

This is the content I wish to extract (zoomed in at the beginning of the 'raw' signal).

Any advice would be appreciated. Thanks!

• This looks not so normal. Are you sure there is no form of accumulating error? Why does is the increase in dc component over time? – msm Sep 17 '16 at 10:10
• It's the output of a waveguide mesh excited by a Dirac delta. Using a Dirac delta produces a an output with mostly flat frequency response, at the cost of accumulated DC offset. Using other excitation signals can reduce the offset, at the cost of reducing the output bandwidth, and unfortunately bandwidth/flat-response is very important to my application. – Reuben Thomas Sep 17 '16 at 10:20
• have you considered notch filtering at dc frequency? – msm Sep 17 '16 at 10:29
• I hadn't, but I think this will cause the same problems as using the high pass iir filter. I'll try it anyway just in case. – Reuben Thomas Sep 17 '16 at 11:05
• Can I ask how confident are you that this DC is not a "bug" or other error? If the digital waveguide is modelling a membrane then an increasing DC means a mounting tension (or displacement) of the membrane (?) – A_A Sep 18 '16 at 8:28

Perhaps the DC cancellation filter on the following web page would be useful.

https://www.dsprelated.com/showarticle/58.php

• I had tried this approach previously. Unfortunately it doesn't perform as well as I'd like (very slow rolloff). – Reuben Thomas Sep 17 '16 at 13:01
• Just shorten the delay if the rolloff is too slow. – Dan Boschen Sep 17 '16 at 15:52

I wouldn't bother with FILTFILT since doing the filtering backwards has a large transient at the end (which becomes the beginning when filtering backwards). the high-pass filter should not mess up the phases of the high frequency content. only the frequency components close to DC would be affected and your HPF is intended to filter them out.

if you insist on FILTFILT (because you want strictly linear phase), then i would make your data twice as long where the latter half is a mirror image of the first half. then your peak DC is in the middle and does not have a discontinuity. run the whole thing though the HPF forward and then run the entire result of that (including the IIR tail) through backwards through the same HPF. then you should not have a transient from the large DC at the end being appended to 0.

Since you are doing this offline and the DC accumulation seems to increase exponentially, you could also try fitting an exponential, possibly with the addition of an offset, to the data and then simply subtract its contribution.

This will yield the variation of the signal around the exponential component and is close to what is actually happening in reality.

So, given some $x(n)$, use the $n,x(n)$ pairs to fit some $y(n)= a + b^n$ and recover $a,b$. With a given fit, you can then form your desired signal as some $z(n) = x(n) - y_{a,b}(n)$.

Hope this helps.