0
$\begingroup$

I'm trying to implement a high pass filter on sensor data to remove the dc offset. I'm aware I can subtract the mean of the data but I'm looking for a frequency domain approach. I've used the matlab's filterDesigner tool but I'm facing this problem: enter image description here

You can see in the time domain output (last plot in blue) that the filter takes some time to settle. I want to minimize that and get similar results as mean subtraction (the plot in orange). Here are the filter specifications.

filter sepcifications

Can anyone help with me with what parameters to tweek to have no "settling time"? I'm going to implement this on esp32 so I need the order to be low.

I'm pretty new to dsp so please be kind.

$\endgroup$
1
  • $\begingroup$ Both the answers provided are correct. Looking at your plot, the mean looks awful close to 2048, so maybe a 12 bit ADC w/ a bias to 50% the rail? You could just subtract that from the measured value, which would be immediate and may be ‘good enough’. You’d probably still have some DC, and a HPF is probably better, but that really depends on what you need to do with the result. $\endgroup$
    – Dan Szabo
    Apr 3 at 15:36
3
$\begingroup$

The filter you desire is often called a “DC removal” or a “DC cancelation” filter. Unfortunately there’s no way to design a useful IIR DC removal filter that has no “settling time”. Your designed filter’s “transition region” is the lower passband frequency (50 Hz) minus the upper stopband frequency (10 Hz). So your designed filter’s transition region is 40 Hz.

If you design a “sharper” DC removal filter having a transition region of 20 Hz then that filter will have a longer settling time. If you design a DC removal filter having a wider transition region of 100 Hz (a lower “performance” filter) then that filter will have a shorter settling time than the filter you designed. So tryingengineer, you’re faced with a trade-off; the higher the performance of a filter (narrower transition region) the longer will be its settling time.

By the way, IIR filters have nonlinear phase responses. If you seek a computationally-efficient DC removal filter that has a linear phase response, see the material at: https://www.dsprelated.com/showarticle/58.php

$\endgroup$
2
  • $\begingroup$ Oh, I overlooked they've selected IIR in the filter design tool, true! $\endgroup$ Apr 3 at 10:41
  • $\begingroup$ "Unfortunately there’s no way to design a useful IIR DC removal filter that has no 'settling time'”. Nor an FIR -- DC removal means observing the average over a period of time and subtracting it out -- FIR or IIR, that time period of observing the average translates to "settling". $\endgroup$
    – TimWescott
    Apr 4 at 16:56
1
$\begingroup$

I'm aware I can subtract the mean of the data

That's actually a high-pass filter!

You can see in the time domain output that the filter takes some time to settle.

That is true for any causal system, i.e. any system that can't look into the future; you'll want to read up on "group delay"

Can anyone help with me with what parameters to tweek to have no "settling time"?

Mathematically impossible.

I want to minimize that and get similar results as mean subtraction (the plot in orange).

You'll find that if your filter is a linear phase filter, then your group delay is half its length and is constant for all frequencies. That means you can shift its output by half the filter length to the left (i.e. ignore the first filter length/2 output samples), and your two lines will overlap very nicely.


You, however, selected "IIR" as as filter type, and these can't be linear phase filters. There might be some trade-off here, too: non-linear phase filter can have lower delay, but it's not the same delay for all frequency components. I find that whenever you actually want to analyze a signal, that becomes a problem, but that can very well be managed if you can design your filter such that the phase response is appropriate for the frequencies you care about, but that leads to the need for a rather involved filter design. I'm not quite convinced you'd win much by this!

$\endgroup$
2
  • $\begingroup$ Thank you for the answer. What I understand from it, is that I should design a linear phase delay filer (FIR filters?) and ignore the first half of the filer output. But I then I would have some dead time where I loose the signal. Is there any alternate approach that you would recommend? $\endgroup$ Apr 3 at 11:00
  • $\begingroup$ no, not the first half of output. The first "filter length"/2 outputs, that's way less, it's exactly the shift between your blue and orange curve. You don't lose any useful signal there. No, you cannot work around that. Math says causal systems have delay, as I said. $\endgroup$ Apr 3 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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