# Are IIR filters (and specifically Butterworth filter) causal?

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.)

Thank you.

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.

• 2.: all implementable IIR are causal. Aug 19 '20 at 13:51
• @MarcusMüller What's a non-implementable filter? Aug 19 '20 at 13:57
• 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. Aug 19 '20 at 15:21
• @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. Aug 20 '20 at 7:57
• @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. Aug 20 '20 at 15:31

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.

• As Triceratops pointed out, one can run a non-causal filter on a recorded signal, because one can then peek ahead in the recording. Aug 19 '20 at 21:09
• 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. Aug 20 '20 at 0:51
• I don't think those words mean what you think. A filter you run on a recorded signal is a perfectly valid filter. Aug 20 '20 at 11:29
• @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. Aug 20 '20 at 15:48
• 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. Aug 20 '20 at 17:37