# How to calculate the delay generated by a 2nd order all-pass filter

I'm trying to simulate the dispersion effect of a stiff string in a digital waveguide system, by using second-order all-pass filters.

I use a second order all-pass filter with this transfer function:

H(z) =    a2 + a1 * z^-1 + z^-2
--------------------------
1 + a1 * z^-1 + a2 * z^-2


Let's suppose the filter coefficients are:

a1 = -1.86377279
a2 =  0.86982215


With the above coefficients, if I insert the 2nd order All-Pass filter inside a digital waveguide loop with a delay line of 168 samples (=> 262.5 Hz if the playback rate is 44100), I get a nice dispersion effect (a little exaggerated to study the phenomenon) but I get a big delay too in the output signal, so now my note is detuned.

A little simplified schema:

impulse --> (+) -------------------------------+-----> output
^                                 |
|                                 |
very much                             v
detuned!                       |DELAY LINE|
|                           |168 smpls |
|                                 |
+---------|2nd ord.|<-------------+
|ALL-PASS|


I noticed that if I manually modify the delay line length (by subtracting 41 samples from it) to compensate for the delay induced by the All-Pass filter, I could get the right tuning of the original note (about 262.5 Hz).

Now my question is:

• is there a formula to know exactly how much I should modify the delay line length to compensate for the delay generated from the 2nd order All-Pass filter with arbitrary a1, a2 coefficients?

The phase of a second-order all-pass system with poles at $z=re^{j\theta}$ and $z=re^{-j\theta}$ is $[1]$

$$\phi(\omega)=-2\omega-2\arctan\left[\frac{r\sin(\omega-\theta)}{1-r\cos(\omega-\theta)}\right]-2\arctan\left[\frac{r\sin(\omega+\theta)}{1-r\cos(\omega+\theta)}\right]\tag{1}$$

The poles are the roots of the denominator polynomial $z^2+a_1z+a_2$. For the given values of $a_1$ and $a_2$ you get $r=0.93264$ and $\theta=0.040271$.

The delay of a sinusoid with frequency $\omega_0$ when passing through the all-pass filter is given by the phase delay

$$\tau_p=-\frac{\phi(\omega_0)}{\omega_0}\tag{2}$$

Evaluating $(2)$ using $(1)$ with $\omega_0=2\pi\frac{262.5}{44100}$ gives a phase delay of $42.7$ samples, which is pretty close to the value you've determined experimentally.

$[1]$ Oppenheim, Schafer, Buck, Discrete-Time Signal Processing, 2nd ed., p. 275.

• Thank you very much for your fast and accurate answer! I'm a novice and I lack the theoretical foundations of the Digital Signal Processing but I suspected that I would have to calculate the phase delay of the filter to get my answer. The problem was that, in the setting of the omega input parameter, I didn't divide 2*pi*freq by 44100, so I got strange results, but now it works! :-) The only problem left now is that, after applying the all-pass filter, the output signal (regardless if it's tuned or not) has some audible clicks towards the end of its period... Do you know why? – Mauro Jun 27 '18 at 8:33
• @Mauro: Great that it now makes more sense to you! About the clicks, that must be some implementation (buffer?) problem, that's all I can say given the information I have. – Matt L. Jun 27 '18 at 9:16