# Allpass Filter with Sign Switch

I want to design a digital allpass IIR filter with the following transfer function. $$H(\omega) = 1 \textrm{ for } \omega < \omega_1 \textrm{ or } \omega > \omega_2$$ and $$H(\omega) = -1 \textrm{ for } \omega > \omega_1 \textrm{ and } \omega < \omega_2$$ I assume that higher-order filters are needed for a steeper transition band. Can you help me design a filter that approximates this specification? I prefer a solution that strictly uses allpass digital filters.

• I have two questions. What's the purpose of this filter? And what do you mean by "a solution that strictly uses allpass digital filters"? Nov 11, 2023 at 16:43
• @MattL. 1) I have a symmetric filter F with a real-valued frequency response, including zero-crossings at $\omega_1$ and $\omega_2$. The magnitude of F is then $|F| = F * H$. 2) So, the allpass H(z) = b(z)/a(z), where $z^{-m} conj(b(1/z)) = a(z)$. The filter can be acausal.
– Jiro
Nov 11, 2023 at 21:08

In this answer I'll just focus on the given design problem, even though I'm not sure if there might be a better overall solution to your underlying problem.

First of all it's instructive to realize that the given filter specification is just the difference of two (ideal) lowpass filters plus a constant. Let $$h_1[n]$$ be an ideal lowpass filter with cut-off frequency $$\omega_1$$, and $$h_2[n]$$ is an ideal lowpass with cut-off $$\omega_2$$, then the desired filter can be expressed as

$$h_d[n]=2\big(h_1[n]-h_2[n]\big) + \delta[n]\tag{1}$$

Eq. $$(1)$$ could be used to design an FIR filter approximating the given desired response. We would just need two odd-length linear-phase lowpass filters designed by any of the many well-known methods. We would need odd-length filters because they have an integer delay, and we could add the constant by simply replacing $$\delta[n]$$ in Eq. $$(1)$$ by $$\delta[n-M]$$, where $$M$$ is the delay of the linear phase filters. Note that the resulting FIR filter would implement the desired phase response exactly (plus a delay), but the magnitude response would only be an approximation to the desired unity gain response.

Eq. $$(1)$$ wouldn't help for designing an IIR filter because of the different non-linear phase responses of the individual lowpass filters. Unlike FIR filters, IIR filters can have an exactly constant magnitude response. However, the desired phase can only be approximated. If we require the filter to be IIR, we need to resort to numerical design methods.

One simple and effective method is the equation error method, which I explain in this blog post. There's also a link to a simple Matlab/Octave implementation: iir_ap.m.

The designed IIR filter needs to be causal and stable, otherwise there's no way to implement it. For this reason we need to add a linear phase term to the desired phase response. This just introduces some extra delay. The amount of delay necessary to make the resulting filter causal and stable must be determined experimentally.

I've designed two filters, one $$10$$th order and one $$20$$th order IIR allpass:

L = 500;
w = pi * linspace(0,1,L); w = w(:);
w1 = .2 * pi; w2 = .4 * pi;
I = find( w > w1 & w < w2 );
P = zeros(L,1);
P(I) = pi;
W = ones(L,1);

% 10th order allpass
N = 10;
tau10 = 8;
P2 = P - w * tau10;
a = iir_ap( N, w/pi, P2, W );
[A10, ww] = freqz( flipud(a), a, 2^12);

% 20th order allpass
N = 20;
tau20 = 18;
P2 = P - w * tau20;
a = iir_ap( N, w/pi, P2, W );
[A20, ww] = freqz( flipud(a), a, 2^12);


The figure below shows the design results. For the plots I removed the linear phase term. Note that the maximum phase error is not improved by choosing a higher filter order. What is improved by using a higher filter order is the steepness at discontinuities of the desired phase.

• That's an excellent solution. Works very well.
– Jiro
Nov 12, 2023 at 17:48
• Is there a reason why you don't choose an integer delay tau? It would make aligning signals easier, isn't it?
– Jiro
Nov 12, 2023 at 18:17
• @Jiro: I didn't assume any requirements on the delays. It's no problem to change them to integers. I changed them in my answer, and the result is virtually the same. Nov 12, 2023 at 18:31