# Design of all pass filter with desired phase response

I want to design an all pass filter with a desired phase response in MATLAB. But in MATLAB, all pass filters are designed either by specifying the all pass filter coefficients or from a transfer function of the desired all pass filter. What if we do not know the transfer function of the filter (i.e. we have to estimate the filter), and only want the phase delay at each particular frequencies to be a specific value, say for example at 1kHz, I want a phase shift of 12 degree and at 2kHz ,I want a phase delay of 4 degrees and so on. Assuming sampling frequency to be 40kHz, how to design such type of digital filters.

• Can you be more specific? In radians, you want a phase shift of $\phi(\omega)=-\frac{12}{180}\pi$ at 1 kHz ($\omega = \frac{1}{40}2\pi$) and an apparently lesser phase shift of $\phi(\omega)=-\frac{4}{180}\pi$ at 2 kHz ($\omega = \frac{2}{40}2\pi$)? These causal APFs have more negative phase shift at higher frequencies, not less. So unless you're willing to put in nearly an entire phase wrap around of $2\pi$ (360°), between the two frequencies, you cannot meet your spec with a causal filter. Aug 27 at 13:42

This is a tricky problem.

First you need to decide whether you need a "true" all pass filter or can do with an approximate one. The difference being

$$|H_\text{trueAP}(\omega) = 1| \\ | |H_\text{apprAP}(\omega)| -1 | < \epsilon, \omega_1 < \omega < \omega_2$$

i.e. do you need the magnitude to be exactly one for all frequencies or do you just need a magnitude "sufficiently close to one" over a certain frequency range?

True all pass filter have some mathematical properties that severely limits the degrees of freedom.

1. They are IIR filters
2. The zeros are inverses of the poles which is equivalent to the transfer function polynomials being mirror of each other.
3. The phase is monotonic decreasing with frequency.
4. The phase DC is 0 and at Nyquist is $$N\pi$$ where $$N$$ is the filter order

That means you need an $$N^\text{th}$$ order all pass to accommodate $$N$$ target points. However, you need to make sure first that the target is decreasing but subtracting multiples of $$2\pi$$. For example

at 1kHz, I want a phase shift of 12 degree and at 2kHz ,I want a phase delay of 4 degrees

You would have to turn this into -348 degrees at 1Khz and -716 degrees at 2kHz. The downside of this approach is that the phase response at the points you don't specify could look rather ugly and will be NOT smooth if the phase doesn't go down with frequency. In this example, the group delay between 1kHz and 2kHz will be very high.

If you only need an approximate all pass, then you can use a least square FIR or IIR filter design method, just as you would with any other filter. The thing to watch here is that these will converge poorly with the target is inherently non-casual. If that's the case, it helps to cascade with a bulk delay.

• I wonder if this spec could be made with a pure delay element of just the right amount of delay? There will still be the needed wrap around phase. Aug 27 at 13:46
• I don't think so. Example: let's say you want -15 degrees at fs/4 (say 12 kHz @fs = 48 kHz). A one sample delay gives you -90 degrees and any large (integer) delay will give you integer multiples of -90. No way to get to -N*360-15 Aug 27 at 13:54
• yeah, i think you're right. even if there was some phase wrap around preceding the 1 kHz point and some more after, but it would have to be less, not more. Aug 27 at 13:59

The Audio EQ Cookbook has the general formulae for all-pass filters (APF). You can maybe cascade four of these APFs together and get 12° at 1 kHz and 360°+4° at 2 kHz. But it's a wild APF and will definitely have some "resonance" (of a sort) around 1.5 kHz.