The EEGLAB News #9
How Does Causal Filtering Work?
Questions: Cedric Cannard, Ph.D. Candidate, CerCo, Paul Sabatier University, Toulouse France
Answers: Andreas Widmann, Ph.D., Scientific Lab Manager, University of Leipzig
The original thread is available at https://sccn.ucsd.edu/pipermail/eeglablist/2021/016141.html (click on next message on bottom to move through the discussion). Below is a summary of the discussion by Cedric Cannard.
What is a phase shift?
Let’s consider the simplest device possible, a piece of wire of length L where the signal can travel at the speed of light. Clearly, it takes L/c seconds for the signal to get through this device. The simple fact that time is required for a signal to pass from a device’s input to its output means that it generates a phase shift (i.e., ordinary time delay). Compared to other electrical devices, filters have significant amounts of phase shift, and the longer the filter (i.e., the more poles or taps it has), the more delay it has. This delay is usually discussed in terms of samples (or seconds).
What is a group delay?
The group delay is the negative derivative of the phase with respect to frequency. Consider a square wave (a large group of frequency components that are in proper phase alignment with one another). If we pass a square wave through a device and expect it to remain square, we need to ensure that it doesn’t distort the signal and misalign these frequency components. A group delay measurement shows us how much a device causes these frequency components to become misaligned. Misaligning the phase between a group of frequency components can distort a signal. In short, we want all frequency components to experience the same amount of delay as they pass through a device.
EEGLAB’s default filter (pop_eegfiltnew) is a linear (zero-phase) non-causal FIR filter. Its delay is constant irrespective of the frequency, and the delay is automatically corrected/subtracted. The difference between the linear phase non-causal filter and a linear phase causal filter is exclusively the delay correction. The output of the non-causal filter equals the delay-corrected output of the causal filter. If your signal of interest is further away from stimulus onset than the group delay, you can simply use the default linear phase non-causal filter.
A causal filter (linear or non-linear) cannot be delay-corrected. This would make it automatically non-causal and break causality, and there is no way around this. So if the (group) delay is too large for your purpose and you need to preserve causality, you must reduce the delay and thus use a non-linear causal filter (e.g. minimum phase filter). How to use EEGLAB non-linear minimum-phase causal filter (example for high-pass 1 Hz): EEG = pop_eegfiltnew(EEG, ‘locutoff',1,'minphase',1); Note that you cannot correct the delay of an IIR filter (the impulse response is infinite and the phase response non-linear). You can only apply backward filtering, but this will result in a non-causal zero-phase filter (back-filtering of linear FIR filter will result in delay correction). The advantage of non-linear filters is that they are short (a few samples) compared to linear filters (sometimes hundreds or thousands of samples).
Non-linear causal filters typically should be used if the application explicitly requires it. For example, if causality matters as in the detection of onset latencies (even if the problem is overestimated as it mainly affects ultra-sharp transients typically not observed in EEG/ERP), the analysis of small fast components before large slow components (e.g. if higher high-pass cutoff frequencies are required), or in the analysis of pre-stimulus activity (to avoid post-stimulus oscillations smearing into the pre-stimulus interval leading to spurious interpretations of pre-stimulus phase). Non-linear filters delay different frequencies by a different amount. Therefore, you must not interpret the shape of the waveform in ERPs as it is distorted to some extent and also not interpret cross-frequency relationships of amplitude or phase in ERSPs. But on the other hand, you can be confident that your pre-stimulus data are not contaminated by post-stimulus activity.
For frequency analysis, filtering is not necessarily required. Most time frequency analysis methods are actually filters by itself (and most can indeed be implemented by convolution, that is, also here you may have to watch out for non-causal effects in certain scenarios). Typically DC removal (mean subtraction or possibly detrending in problematic cases) may be sufficient. For ERPs, I would personally try non-linear phase filters. I would try to systematically compare non-linear and linear phase filtered data to try to disentangle what are distortions due to non-linearity or what are potentially non-causal effects. Particularly helpful is to systematically evaluate and compare the signal removed by different filters (unfiltered minus filtered data).
How to calculate the filter order and the group delay (in seconds) before applying a filter:
the firwsord function can be used to estimate the filter order, requiring only the transition bandwidth from user. E.g., order_highpass = firwsord('hamming', EEG.srate, 0.5) % 0.5 being the transition bandwidth in Hz
Then, the group delay (in seconds) can be calculated with: Filter_order / 2 / sample rate
“Recommended” transition bandwidths:
For low-pass filters for EEG/ERP, 12.5 Hz transition bandwidth is fine. For high-pass filters, for ERPs it is too low. Your high-pass cutoff would be limited to 6.25 Hz (without compromising filter quality).
Causal Filtering and ICA
Using a causal as opposed to a non-causal filter shouldn’t change the independent component topographies. As the output of non-causal vs. causal linear phase filters is identical (just delayed) there shouldn’t be a difference with respect to causality. See e.g., the introduction of https://onlinelibrary.wiley.com/doi/full/10.1111/ejn.14992 for more detailed considerations on ICA and filtering.