Practical use of Phase Delay for IIR Filter on Pure Sinusoid

I recently asked a question about Finding Dominant Frequency and Phase using FFT. My follow-up question has to do with the phase shift induced by an IIR filter. For example, consider a situation where I want to forecast a signal into the future. The original, noisy signal gets filtered (Row 1). The dominant frequency is determined (Row 2). The phase angle of the dominant frequency is determined (Row 3). Finally, the forecasted signal is aligned with the filtered signal based on the phase angle to the dominant frequency (Row 4).
However, the true goal is to align the forecasted signal with the original signal, but I don't understand how—other than empirically—to determine the phase shift. Is this information in phasedelay() (below)?
This essentially results in two phase shifts of the forecasted signal: one based on the original signal's phase (gathered from fft()), one to correct for the shift due to the IIR (unsure about this).

3 Comments

Hello
remember that sine wave are represented in frequency domain as complex numbers. when you filter a signal the output signal phase is the sum of all rotations induced by applied filters.
you shall compute the gain and phase of the applied filter (at the sine wave frequency)
if you wish to align the raw and filtered data, you can either delay the raw data by the phase amount of the filter , or compensate the delay of the filter on the output signal (remove the first samples according to your filter response delay )
Mathieu, assuming a 2Hz sine wave is the raw data, where would I gather information on the delay of the filter? For example, if I'm forecasting using a sine wave, my delay should be bounded by -π and π.
Matt
i am not 100% sure of what you intend to do , but if you want to smooth some noisy sine waves without adding delay , you should try to use bandpass filters and not only low pass filters.
If you know or measure first the sine wave frequency (f0) set the bandpass filter low and high pass frequency like this :
f_low = f0/a
f_high = f0*a
a defines the bandwith (selectivity) of you filter (start with a = 2 for example and see the diffeence when increasing / decreasing a)
the benefit of doing this way is that your bandpass filter will have zero delay at the f0 frequency, so if you implement it on a DSP , you can observe both raw and filtered signals without delay.
to furtehr comment on phase behaviour of filters, there is NO filter that has not a phase roll with frequency - even Bessel filters. This applies for analog and digital filters as well. Only if there is need for it , there are "linear" phase filters and "non linear" phase filters - but there is no "phase neutral" filter on earth - not sure what neutral means here...

Sign in to comment.

 Accepted Answer

To avoid the phase shift, use the filtfilt function instead of filter.

4 Comments

Unfortunately, MALAB is only to prototype this filter, I need to use an IIR in practice. Note, the suggestion for an empirical estimation here: How to get the phase sift of the filtering?
If you want to design a phase-neutral filter in hardware, use the besself function to prototype it. All other filter designs — to the best of my knowledge at least — exhibit phase delays and phase distortion. That’s the reason Bessel filters are used as anti-aliasing filters in front of ADCs.
Calculating the component values is not trivial, and requires polynomial continued fraction expansion of the transfer function. It would likely be best to go online or use an offline textbook to design and calculate the component values. (Bessel filters cannot be rendered as discrete filters, so there is no way to test it otherwise. You simply need to build it. I’ve not built hardware filters in a while, so there could be ICs that make the Bessel filter implementation easier. You will have to look for them.)
(As I was writing this a few minutes ago, Windows 10 crashed — again for the second time in about as many hours — or I’d have posted this a few minutes earlier.)
Thanks, Star. The IIR selection might be a bit outside the scope of this thread: A Fast EEG Forecasting Algorithm for Phase-Locked Transcranial Electrical Stimulation of the Human Brain
In any case, my best attempt at characterizing the filter is as follows. I swept from the low to high cutoff frequencies of my filter (0.5–4Hz) using each frequency to construct a pure sine wave, which was then passed through my filter. I used the FFT to extract the phase at each frequency, ultimately building a phase delay curve (or 'lookup table') based on the dominant frequency of the filtered data (this is the phase shift I need to apply to the forecasted signal to re-align it to the raw data).
Those data can be unwrapped and fit with a third order polynomial (adequate for my application) which makes the phase shift lookup basically one line of code (or use feval() in MATLAB).
Together: the raw data is a 2.3Hz sine wave arbitrarily shifted (here, 0.65π, Row 1). That phase delay is found by the FFT (Rows 2,3) and forecasted, then corrected by the phase delay from my polynomial lookup equation (Row 4).
This feels very convoluted, but arriving at a simple one-liner phase correction is something I'm happy with. Here's another example where the raw data is modulated at 3.15Hz with an arbitrary shift of 1.6π.
I’m not certain what you want to do. If you’re doing anything in real-time, the Bessel filter (or several cascaded if necessary to get a shorter transition region) is likely the way to go for the reasons I stated. For real-time applications, it would be necessary to minimise the phase delay and phase distortion inherent in other filter designs.
I’ve not done anything with EEG for a while, and nothing about using (what appears to me to be) a feedback system for transcranial stimulation. That’s beyond the scope of my current expertise.
In any event, while you can use Bessel filters for real-time applications, to prototype them in MATLAB it is necessary to use filtfilt, since it provides phase-neutral filtering for all discrete filter designs.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!