# Correcting phase response of a signal

I have a sensor that applies a frequency dependant phase alteration to my signals. I'm trying to find a way to correct these phase responses using an FIR filter. Essentially, I'm looking for methods to apply an all-pass filter with arbitrary frequency response to correct this.

For magnitude calibrations of my sensor, I can achieve this by drawing out my calibration spectrum, applying an ifft to it, then applying a hamming window. Then I simply convolve the resulting impulse response with my signal. This is essentially MATLAB's fir2(). Is there an analogous procedure I can do to correct arbitrary phase responses in my signals?

## 2 Answers

You can equalize magnitude and phase simultaneously by defining a desired complex frequency response

$$D(\omega)=M(\omega)e^{j\phi(\omega)}\tag{1}$$

with magnitude $$M(\omega)$$ and phase $$\phi(\omega)$$ chosen such that they compensate for the given magnitude and phase distortions.

An FIR filter approximating $$(1)$$ can be designed by using the following error function:

$$E=\sum_kW_k\big|D(\omega_k)-H(\omega_k)\big|^2\tag{2}$$

where $$\omega_k$$ are frequencies on a dense grid, $$W_k$$ are some non-negative weights allowing to emphasize certain frequency regions compared to others, and $$H(\omega)$$ is the frequency response of the FIR filter to be designed:

$$H(\omega)=\sum_{n=0}^{N-1}h[n]e^{-jn\omega}\tag{3}$$

Minimizing $$(2)$$ with respect to the filter coefficients $$h[n]$$ can be achieved by solving a system of linear equations. I've written a Matlab/Octave function that does just that: lslevin.m

Note that it may be necessary to add a linear phase (i.e., a delay) to the required $$\phi(\omega)$$ in order for a causal filter to better approximate the desired complex frequency response. As a guideline, the average delay implied by the desired phase should be about half the chosen filter length $$N$$. It is worthwhile experimenting with different additional delays because they may have a significant influence on the resulting approximation error.

• This is really great! I had looked over least squares methods, but never saw an implementation addressing arbitrary phase responses. I will try to implement this, the least squares code is very helpful! I'm curious, are there any pre-made implementations of this in scipy or Matlab/octave? I'm wondering why I've never come across this before. – RTbecard Dec 24 '20 at 10:14
• @RTbecard: There are some functions for approximating a complex-valued frequency response (i.e., magnitude and phase), but at least at the time when I wrote that function there was no simple least squares algorithm. – Matt L. Dec 24 '20 at 10:42

Is there an analogous procedure I can do to correct arbitrary phase responses in my signals?

Arbitrary all-pass design is tricky since there are some extra constraints to be taken into account.

1. FIR filters cannot be "ideal" all pass filters, since all pass filters have poles and zeroes which inverse of each others. FIR filters have all the poles at zero.
2. For a real valued filter, the transfer function must have Hermitian symmetry which implies the transfer function at 0 and at the Nyquist frequency must be real, i.e. the phase must be $$0$$ or $$\pi$$
3. IIR allpass filters always have a phase of $$0$$ at 0 Hz and a phase of $$N \cdot \pi$$ at Nyquist, where N is the filter order. So overall shape is fairly constrain and it's tricky to do something "arbitrary" that looks substantially different.

So the best you can do is with FIR filter, is an "approximate" all pass. How good or bad this works, depends a lot on your specific example and requirements.

I can achieve this by drawing out my calibration spectrum, applying an ifft to it, then applying a hamming window

That's not a bad method for FIR allpass designs as well. Define your phase correction phase starting with a large FFT grid, make sure it's conjugate symmetric, do an IFFT and see what get. Typically you will see an impulse response that's more on the long side and non-causal. You can then start playing around with windows to shorten it up.

Things that help are

1. Make sure you avoid very steep phase gradients, if possible
2. Carefully manage the transition into DC and Nyquist. Ideally your target moves smoothly to the nearest "real" phase.
3. If you have "don't care" areas in the frequency, make sure you optimize the frequency response target for the best time domain properties of the filter. That may often require an iterative approach
• Ah, I did not realise I could just apply the same method for phase responses! Does that mean that the window function also applies some sort of smoothing to the resulting phase values? (analogous to reducing the ripple in the frequency response magnitude?). My point of hesitation was not understanding how windowing the impulse response would affect the phase. – RTbecard Dec 24 '20 at 9:52
• Yes. Windowing will create BOTH phase smoothing and amplitude ripple – Hilmar Dec 25 '20 at 13:50