4
$\begingroup$

I am new to signal processing. I am learning now about filtering and I am trying to implement high pass filter (HPF),and later band-pass filter (BPF). I implement the filters in Python 3.8 with NumPy and SciPy.

For the offline case (filtering a pre-recorded time) I designed a Butterworth high-pass filter, and applied on the signal at the time domain using signal.filtfilt function. However, as the documentation shows, this approach is good only for offline cases and not for real-time, since the filter scans the signal forward and then backwards. I can recover the frequency response from the filter using signal.freqz function.

Now I want that this filter will work also in real-time (so it needs to be causal, and I ask if this possible for this type of filters. Here are my questions:

  1. If I understand correctly, Butterworth filters are IIR (infinite impulse response) filters. Is it true?
  2. Are IIR filter causal? It may be that all IIR filters are not causal and it may be that some are and some are not. What are the cases in which IIR filters are causal?
  3. Are Butterworth filters causal? If not always, in which case they are causal?
  4. If the answer to 3 is true, how to implement a causal Butterworth filter, both at the time domain and the frequency domain? (I can use SciPy functions such as signal.butter, signal.freqz etc.)

Relevant links:

Thank you.

$\endgroup$

2 Answers 2

8
$\begingroup$
  1. Yes, Butterworth are IIR. The decay from an impulse technically lasts forever.
  2. Yes, all [implementable] IIR are causal.
  3. Yes, because of #1 and #2.
  4. Don't use signal.filtfilt. Use signal.lfilter. filtfilt does the same thing as lfilter, except twice, in opposite directions, which changes a causal filter into a zero-phase filter.

However, as the documentation for both of those functions suggests, for most practical use, you should actually use the SOS variants instead:

The function sosfilt (and filter design using output='sos') should be preferred over lfilter for most filtering tasks, as second-order sections have fewer numerical problems.

To summarize the functions:

  • lfilter: Causal single-stage filtering (low orders only)
  • filtfilt: Zero-phase single-stage filtering (low orders only)
  • sosfilt: Causal second-order sections filtering
  • sosfiltfilt: Zero-phase second-order sections filtering

Really the only reason to use lfilter or filtfilt is if your coefficients are already in b, a format, if you're implementing something out of a textbook, etc.

If you're designing the filter yourself, then just use SOS form, which reduces numerical error.

Note that the filtfilt functions apply the filter twice, so it will have double the order of the original filter.

$\endgroup$
8
  • 2
    $\begingroup$ 2.: all implementable IIR are causal. $\endgroup$ Aug 19, 2020 at 13:51
  • $\begingroup$ @MarcusMüller What's a non-implementable filter? $\endgroup$
    – endolith
    Aug 19, 2020 at 13:57
  • 3
    $\begingroup$ Marcus, actually one can implement non-causal IIR filter, but work with it in offline and not in real-time (e.g. filtering a WAV file). This is the case of zero-phase filter. $\endgroup$ Aug 19, 2020 at 15:21
  • 4
    $\begingroup$ @Triceratops: You can TRY to make a non-causal filter causal by applying sufficient amount of bulk delay. That's what non-real processing does. Today you are processing samples which were generated yesterday. You are using some of this delay in your processing. However, for IIR that can only be done approximately since the non-causality is infinite, you technically need infinite amount of bulk delay so you need to truncate at some point. In practice, the truncation error becomes very small quickly, so it's not much of a problem, but it's never error free. $\endgroup$
    – Hilmar
    Aug 20, 2020 at 7:57
  • 1
    $\begingroup$ @endolith I can design a Butterworth filter, obtain its frequency response, taking abs value and obtain a zero-phase filter, which by theory is non-causal. Then I use it as a regular frequency filter. $\endgroup$ Aug 20, 2020 at 15:31
2
$\begingroup$

Are IIR filter causal? It may be that all IIR filters are not causal and it may be that some are and some are not. What are the cases in which IIR filters are causal?

All real IIR filters are causal. All real systems are causal, unless the universe is a lot weirder than it seems.

You can define a filter that acts on future information; i.e. you could say that $y_n = 0.9 y_{n+1} + x_n$. That would (with a lot of words to overcome the reader's skepticism) describe a filter whose impulse response is $$h_\kappa = \begin{cases} 0 & \kappa > 0 \\ 0.9^{-\kappa} & \kappa \le 0 \end{cases}$$

You could not, however, actually implement such a filter in real life.

$\endgroup$
6
  • 1
    $\begingroup$ As Triceratops pointed out, one can run a non-causal filter on a recorded signal, because one can then peek ahead in the recording. $\endgroup$
    – user253751
    Aug 19, 2020 at 21:09
  • $\begingroup$ Yes. But strictly speaking it's still causal, because the signal was recorded to the end before you started running your filter on it. So you can simulate a non-causal filter. But you cannot implement one in really truly real life. Because -- Physics is Everything. $\endgroup$
    – TimWescott
    Aug 20, 2020 at 0:51
  • 1
    $\begingroup$ I don't think those words mean what you think. A filter you run on a recorded signal is a perfectly valid filter. $\endgroup$
    – user253751
    Aug 20, 2020 at 11:29
  • $\begingroup$ @TimWescott A non-causal filter can work on a prerecorded data (e.g. WAV file) but would not, at least according to theory, work in real-time. $\endgroup$ Aug 20, 2020 at 15:48
  • $\begingroup$ Yes, a filter run on pre-recorded data is perfectly valid. And filter that's "non-causal" in the theoretical DSP sense can be run on pre-recorded data. But it's still causal in the physics sense -- for instance you can't run a non-causal filter on the ticker feed from your local stock exchange and make trades based on actual future stock prices. Nor can you put a non-causal filter in the feedback path of a physical control loop. $\endgroup$
    – TimWescott
    Aug 20, 2020 at 17:37

Your Answer

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

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