Algorithm Search⌘K

Proofs: Goertzel algorithm

These are the human proofs of Goertzel algorithm, at full length.

GOERTZEL returns X_k with n + 1 multiplications and 2n + 1 additions, and its rounding error is at most (13u + η + μ)(n + 2) M ‖x‖_1 to first order full

This proof shows: correctness, complexity.

Statement. Let RR be a commutative ring, n≥1n \ge 1, ω∈R\omega \in R with ωn=1\omega^n = 1, kk an integer, z=ω−kz = \omega^{-k} and c=z+z−1c = z + z^{-1}. For every x∈Rnx \in R^n, GOERTZEL returns Xk=∑j=0n−1xjωjkX_k = \sum_{j=0}^{n-1} x_j \omega^{jk}, using exactly n+1n + 1 multiplications and 2n+12n + 1 additions and subtractions in RR, besides computing zz and cc. Over C\mathbb{C} with ω=e−2πi/n\omega = e^{-2\pi i/n}, in floating-point arithmetic with unit roundoff uu, if the constants are computed with ∣c^−c∣≤η|\hat c - c| \le \eta and ∣z^−z∣≤μ|\hat z - z| \le \mu, then to first order in uu (dropping terms of order u2u^2, uηu\eta and uμu\mu)

∣X^k−Xk∣≤(13u+η+μ)(n+2) M ∥x∥1,M=min⁡(n,1∣sin⁡(2πk/n)∣),|\hat X_k - X_k| \le (13u + \eta + \mu)(n + 2)\, M\, \lVert x \rVert_1, \qquad M = \min\left(n, \frac{1}{|\sin(2\pi k/n)|}\right),

with M=nM = n when sin⁡(2πk/n)=0\sin(2\pi k/n) = 0.

The sequence. Let s−2=s−1=0s_{-2} = s_{-1} = 0 and sj=xj+c sj−1−sj−2s_j = x_j + c\, s_{j-1} - s_{j-2} for 0≤j<n0 \le j < n. By induction on the loop at line L4, after the iteration for jj the variables hold s1=sjs_1 = s_j and s2=sj−1s_2 = s_{j-1}: before the loop they hold s−1s_{-1} and s−2s_{-2} (line L3), and lines L5 and L6 compute sjs_j from them and shift. So line L7 returns zsn−1−sn−2z s_{n-1} - s_{n-2}.

Lemma 1 (Horner). For j≥0j \ge 0 let yj=∑i=0jxizj−iy_j = \sum_{i=0}^{j} x_i z^{j-i}. Then yj=sj−z−1sj−1y_j = s_j - z^{-1} s_{j-1}. Proof. For j=0j = 0: y0=x0=s0−z−1s−1y_0 = x_0 = s_0 - z^{-1} s_{-1}. If it holds for jj, then yj+1=zyj+xj+1=zsj−sj−1+xj+1y_{j+1} = z y_j + x_{j+1} = z s_j - s_{j-1} + x_{j+1}, using zz−1=1z z^{-1} = 1, while sj+1−z−1sj=xj+1+(c−z−1)sj−sj−1=xj+1+zsj−sj−1s_{j+1} - z^{-1} s_j = x_{j+1} + (c - z^{-1}) s_j - s_{j-1} = x_{j+1} + z s_j - s_{j-1}, using c−z−1=zc - z^{-1} = z. □\square

Correctness. Since ωn=1\omega^n = 1, zn=ω−kn=1z^n = \omega^{-kn} = 1, so ωjk=z−j=zn−j=z⋅zn−1−j\omega^{jk} = z^{-j} = z^{n-j} = z \cdot z^{n-1-j}. Hence Xk=z∑jxjzn−1−j=z yn−1=zsn−1−sn−2X_k = z \sum_j x_j z^{n-1-j} = z\, y_{n-1} = z s_{n-1} - s_{n-2} by Lemma 1, which is the value returned. Over C\mathbb{C}, z=e2πik/nz = e^{2\pi i k/n} and c=2cos⁡(2πk/n)c = 2\cos(2\pi k/n) is real.

Operation count. Each of the nn iterations of line L5 does one multiplication c⋅s1c \cdot s_1, one addition and one subtraction; line L6 only moves values. Line L7 does one multiplication and one subtraction. The total is n+1n + 1 multiplications and 2n+12n + 1 additions and subtractions, for every input. The space is the three variables s,s1,s2s, s_1, s_2 and the two constants, Θ(1)\Theta(1).

Lemma 2 (the propagation factors). Let U−1=0U_{-1} = 0, U0=1U_0 = 1 and Um=cUm−1−Um−2U_m = c U_{m-1} - U_{m-2} for m≥1m \ge 1. Then (a) zUm−Um−1=zm+1z U_m - U_{m-1} = z^{m+1} for m≥0m \ge 0; (b) sj=∑i=0jUj−ixis_j = \sum_{i=0}^{j} U_{j-i} x_i; (c) over C\mathbb{C} with c=2cos⁡θc = 2\cos\theta, ∣Um∣≤min⁡(m+1,1/∣sin⁡θ∣)|U_m| \le \min(m + 1, 1/|\sin\theta|). Proof. (a) For m=0m = 0 it reads z=zz = z. For m≥1m \ge 1, zUm−Um−1=(zc−1)Um−1−zUm−2z U_m - U_{m-1} = (z c - 1) U_{m-1} - z U_{m-2}, and zc−1=z2z c - 1 = z^2, so this is z(zUm−1−Um−2)=z⋅zmz (z U_{m-1} - U_{m-2}) = z \cdot z^m by induction. (b) Both sides satisfy the recurrence tj=xj+ctj−1−tj−2t_j = x_j + c t_{j-1} - t_{j-2} with t−1=t−2=0t_{-1} = t_{-2} = 0, by linearity. (c) UmU_m is the Chebyshev polynomial of the second kind at cos⁡θ\cos\theta, and Um(cos⁡θ)=sin⁡((m+1)θ)/sin⁡θU_m(\cos\theta) = \sin((m+1)\theta)/\sin\theta when sin⁡θ≠0\sin\theta \ne 0 (this satisfies the same recurrence, by sin⁡((m+1)θ)+sin⁡((m−1)θ)=2cos⁡θsin⁡(mθ)\sin((m+1)\theta) + \sin((m-1)\theta) = 2\cos\theta \sin(m\theta)), which gives the bound 1/∣sin⁡θ∣1/|\sin\theta|. Also Um=∑r=0mei(m−2r)θU_m = \sum_{r=0}^{m} e^{i(m - 2r)\theta} (both sides are 11 and 2cos⁡θ2\cos\theta for m=0,1m = 0, 1 and satisfy the recurrence, since multiplying the sum for m−1m - 1 by eiθ+e−iθe^{i\theta} + e^{-i\theta} gives the sums for mm and m−2m - 2), so ∣Um∣≤m+1|U_m| \le m + 1. □\square

By (b) and (c), with θ=2πk/n\theta = 2\pi k/n and j≤n−1j \le n - 1, ∣sj∣≤M∥x∥1|s_j| \le M \lVert x \rVert_1.

The rounding errors of the loop. We use the standard model fl(a∘b)=(a∘b)(1+δ)\mathrm{fl}(a \circ b) = (a \circ b)(1 + \delta), ∣δ∣≤u|\delta| \le u, for real operations. Since c^\hat c is real, each operation of line L5 acts on the real and imaginary parts separately. For one real component, s^=fl(fl(x+fl(c^ s^1))−s^2)\hat s = \mathrm{fl}(\mathrm{fl}(x + \mathrm{fl}(\hat c\, \hat s_1)) - \hat s_2) equals x+c^s^1−s^2+ex + \hat c \hat s_1 - \hat s_2 + e with ∣e∣≤3u(∣x∣+∣c^s^1∣+∣s^2∣)|e| \le 3u (|x| + |\hat c \hat s_1| + |\hat s_2|) to first order. Adding the bounds of the two components and using ∣ar∣+∣ai∣≤2∣a∣|a_r| + |a_i| \le \sqrt 2 |a| gives, for the complex values,

s^j=xj+c s^j−1−s^j−2+fj,∣fj∣≤32 u(∣xj∣+2∣sj−1∣+∣sj−2∣)+η∣sj−1∣,\hat s_j = x_j + c\, \hat s_{j-1} - \hat s_{j-2} + f_j, \qquad |f_j| \le 3\sqrt2\, u (|x_j| + 2|s_{j-1}| + |s_{j-2}|) + \eta |s_{j-1}|,

to first order, where fjf_j also absorbs (c^−c)s^j−1(\hat c - c)\hat s_{j-1} and we used ∣c^∣≤2+η|\hat c| \le 2 + \eta. The differences dj=s^j−sjd_j = \hat s_j - s_j satisfy dj=cdj−1−dj−2+fjd_j = c d_{j-1} - d_{j-2} + f_j with d−1=d−2=0d_{-1} = d_{-2} = 0, so by Lemma 2(b), dj=∑i≤jUj−ifid_j = \sum_{i \le j} U_{j-i} f_i. Then by Lemma 2(a),

zs^n−1−s^n−2−Xk=zdn−1−dn−2=∑i=0n−1fi(zUn−1−i−Un−2−i)=∑i=0n−1fizn−i,z \hat s_{n-1} - \hat s_{n-2} - X_k = z d_{n-1} - d_{n-2} = \sum_{i=0}^{n-1} f_i (z U_{n-1-i} - U_{n-2-i}) = \sum_{i=0}^{n-1} f_i z^{n-i},

and since ∣z∣=1|z| = 1 its modulus is at most ∑i∣fi∣≤32 u∥x∥1+(92 u+η) nM∥x∥1\sum_i |f_i| \le 3\sqrt2\, u \lVert x \rVert_1 + (9\sqrt2\, u + \eta)\, n M \lVert x \rVert_1, using ∣sj∣≤M∥x∥1|s_j| \le M \lVert x \rVert_1.

The rounding errors of line L7. For a complex product, fl(ab)=ab(1+δ)\mathrm{fl}(a b) = a b (1 + \delta) with ∣δ∣≤2γ2=22u|\delta| \le \sqrt2 \gamma_2 = 2\sqrt2 u to first order (Higham, Lemma 3.5), and a complex subtraction has relative error at most uu. So X^k=(z^s^n−1(1+δ1)−s^n−2)(1+δ2)\hat X_k = (\hat z \hat s_{n-1} (1 + \delta_1) - \hat s_{n-2})(1 + \delta_2), and X^k−(zs^n−1−s^n−2)\hat X_k - (z \hat s_{n-1} - \hat s_{n-2}) has modulus at most μ∣sn−1∣+22u∣sn−1∣+u∣Xk∣\mu |s_{n-1}| + 2\sqrt2 u |s_{n-1}| + u |X_k| to first order, which is at most (μ+22u)M∥x∥1+u∥x∥1(\mu + 2\sqrt2 u) M \lVert x \rVert_1 + u \lVert x \rVert_1.

The bound. Adding the two parts,

∣X^k−Xk∣≤[(92 u+η) nM+(32+1)u+(22u+μ)M]∥x∥1.|\hat X_k - X_k| \le \left[(9\sqrt2\, u + \eta)\, n M + (3\sqrt2 + 1) u + (2\sqrt2 u + \mu) M\right] \lVert x \rVert_1.

Now 92<139\sqrt 2 < 13, and since M≥1M \ge 1, (32+1)u+(22u+μ)M≤(8.1u+μ)M≤2(13u+μ)M(3\sqrt2 + 1) u + (2\sqrt2 u + \mu) M \le (8.1 u + \mu) M \le 2 (13 u + \mu) M. So the right side is at most (13u+η+μ)(n+2)M∥x∥1(13 u + \eta + \mu)(n + 2) M \lVert x \rVert_1. ■\blacksquare

Remark. The implementation computes z^\hat z by cmath.exp and c^=2Re⁡z^\hat c = 2 \operatorname{Re} \hat z, so η≤2μ\eta \le 2\mu; the tests assume μ≤2u\mu \le 2u for the library exponential, hence η+μ≤6u\eta + \mu \le 6u, and use the constant 20≥13+720 \ge 13 + 7.

Back to Goertzel algorithm