Salta al contenuto
Note per Studenti Fast Fourier Transform Report

Fast Fourier Transform

In questa pagina 4

The FFT is a divide-and-conquer algorithm (Divide and Conquer - Paradigm and Call TreeA divide-and-conquer algorithm comes from a substructure property (threshold n0, Divide and Conquer functions) with strictly smaller subinstances; the call tree describes the execution, and D&C has no memory (equal subinstances are recomputed). Complete example: MAX of an array, with correctness by induction, the recurrence, and the exact count n-1 via the theorem on full binary trees.Divide and Conquer - Paradigm and Call Tree →) that computes the DFT (DFT and Roots of UnityThe DFT of order n evaluates a polynomial with degree bound n at the n n-th roots of unity, y_i = p(w^i) with w = e^{2 pi i/n}; it is the product by the Fourier matrix F_n, an invertible Vandermonde matrix. The roots form a multiplicative group and satisfy three lemmas: cancellation (w_{dn}^{dk} = w_n^k, w_n^{n/2} = -1), halving (the squares of the n roots are the n/2 roots of order n/2, each twice) and summation (the sum of the powers of w^k is n if k is a multiple of n, 0 otherwise). Linear convolution theorem: (c|0) = DFT^{-1}{2n}(DFT{2n}(a|0) times DFT_{2n}(b|0)).DFT and Roots of Unity →) in Θ(nlog⁡n)\Theta(n\log n) instead of Θ(n2)\Theta(n^2). From now on nn is a power of two; for convolution this is no restriction, since one can pad up to the next power of two.

Substructure

Base. n=1n=1: p(x)=a0p(x)=a_0, evaluated at ω10=1\omega_1^0=1: DFT1(a)=a\mathrm{DFT}_1(\mathbf a)=\mathbf a.

Divide. For n>1n>1 split the coefficients by index parity: a[0]=(a0,a2,…,an−2),a[1]=(a1,a3,…,an−1),\mathbf a^{[0]}=(a_0,a_2,\dots,a_{n-2}),\quad\mathbf a^{[1]}=(a_1,a_3,\dots,a_{n-1}), the coefficients of the polynomials with bound n/2n/2 p[0](x)=∑j=0n/2−1a2jxj,p[1](x)=∑j=0n/2−1a2j+1xj.p^{[0]}(x)=\sum_{j=0}^{n/2-1}a_{2j}x^j,\qquad p^{[1]}(x)=\sum_{j=0}^{n/2-1}a_{2j+1}x^j. (Not two contiguous halves, unlike MAX or the matrix blocks.)

Polynomial identity. For all xx:   p(x)=p[0](x2)+x p[1](x2).\;p(x)=p^{[0]}(x^2)+x\,p^{[1]}(x^2).

Proof. p[0](x2)=∑ja2jx2jp^{[0]}(x^2)=\sum_ja_{2j}x^{2j} is the even-degree part, x p[1](x2)=∑ja2j+1x2j+1x\,p^{[1]}(x^2)=\sum_ja_{2j+1}x^{2j+1} the odd-degree part. □\square

Example. p=3+4x+5x2+6x3p=3+4x+5x^2+6x^3: p[0]=3+5xp^{[0]}=3+5x, p[1]=4+6xp^{[1]}=4+6x, and (3+5x2)+x(4+6x2)=p(3+5x^2)+x(4+6x^2)=p.

Theorem (substructure). Let y=DFTn(a)\mathbf y=\mathrm{DFT}_n(\mathbf a), y[0]=DFTn/2(a[0])\mathbf y^{[0]}=\mathrm{DFT}_{n/2}(\mathbf a^{[0]}), y[1]=DFTn/2(a[1])\mathbf y^{[1]}=\mathrm{DFT}_{n/2}(\mathbf a^{[1]}). For 0≤i≤n2−10\le i\le\frac n2-1: yi=yi[0]+ωni yi[1],yi+n/2=yi[0]−ωni yi[1].y_i=y^{[0]}_i+\omega_n^i\,y^{[1]}_i,\qquad y_{i+n/2}=y^{[0]}_i-\omega_n^i\,y^{[1]}_i.

Proof. First half: by the identity and the halving lemma, yi=p(ωni)=p[0]((ωni)2)+ωnip[1]((ωni)2)=p[0](ωn/2i)+ωnip[1](ωn/2i)=yi[0]+ωniyi[1].y_i=p(\omega_n^i)=p^{[0]}\bigl((\omega_n^i)^2\bigr)+\omega_n^ip^{[1]}\bigl((\omega_n^i)^2\bigr)=p^{[0]}(\omega_{n/2}^i)+\omega_n^ip^{[1]}(\omega_{n/2}^i)=y^{[0]}_i+\omega_n^iy^{[1]}_i. Second half: ωn2(i+n/2)=ωn2iωnn=ωn/2i\omega_n^{2(i+n/2)}=\omega_n^{2i}\omega_n^n=\omega_{n/2}^i and ωni+n/2=−ωni\omega_n^{i+n/2}=-\omega_n^i, so yi+n/2=p[0](ωn/2i)−ωnip[1](ωn/2i)=yi[0]−ωniyi[1]. □y_{i+n/2}=p^{[0]}(\omega_{n/2}^i)-\omega_n^ip^{[1]}(\omega_{n/2}^i)=y^{[0]}_i-\omega_n^iy^{[1]}_i.\ \square

Each pair of evaluations at the same point ωn/2i\omega_{n/2}^i gives, with the two combinations yi[0]±ωniyi[1]y^{[0]}_i\pm\omega_n^iy^{[1]}_i, two values of pp: common terms are computed once. This pair of formulas is the butterfly.

The algorithm

FFT(a)
n <- a.len
if n = 1 then return a                      { base }
a0 <- (a[0], a[2], ..., a[n-2])             { divide: even indices }
a1 <- (a[1], a[3], ..., a[n-1])             { divide: odd indices }
y0 <- FFT(a0);  y1 <- FFT(a1)               { recurse }
ON <- e^(i 2 pi / n)                        { conquer: ON = w_n }
ONI <- 1                                    { at the start of iteration i, ONI = w_n^i }
for i <- 0 to n/2 - 1
    t <- ONI * y1[i]
    y[i] <- y0[i] + t
    y[i + n/2] <- y0[i] - t
    ONI <- ONI * ON
return y

Correctness. Induction on kk with n=2kn=2^k. k=0k=0 is the base. For k≥1k\ge1, by hypothesis y0, y1 are DFTn/2(a[0])\mathrm{DFT}_{n/2}(\mathbf a^{[0]}), DFTn/2(a[1])\mathrm{DFT}_{n/2}(\mathbf a^{[1]}); the loop computes the two formulas of the substructure theorem with ONI =ωni=\omega_n^i (invariant: at the end of iteration ii it equals ωni+1\omega_n^{i+1}). □\square

Exact cost. Each iteration does 44 complex operations (two products, a sum and a difference), n/2n/2 iterations: Conquer work 2n2n. T(1)=0,T(n)=2T(n2)+2n(n≥2).T(1)=0,\qquad T(n)=2T(\tfrac n2)+2n\quad(n\ge2). Exact solution T(n)=2nlog⁡2nT(n)=2n\log_2n (induction: T(1)=0T(1)=0; 2⋅n(log⁡2n−1)+2n=2nlog⁡2n2\cdot n(\log_2n-1)+2n=2n\log_2n). Also from the Master TheoremThe Master Theorem for T(n) = aT(n/b) + w(n) compares w(n) with the threshold n^(log_b a). Case 1 (w polynomially smaller): Theta(n^(log_b a)); case 2 (same order): Theta(n^(log_b a) log n); case 3 (polynomially larger and regular): Theta(w(n)). Full proof from the levels of the tree, extended version with floors and ceilings, a table of applications, and the misuse when no case applies (w = n log n).Master Theorem →: s=2s=2, threshold nn, w=2nw=2n, Case 2, Θ(nlog⁡n)\Theta(n\log n). The tree has log⁡2n+1\log_2n+1 levels, nn leaves, and every level has total work 2n2n (Recurrence Relations - Recursion Tree and General FormulaThe recurrence tree has the shape of the call tree of the worst instance and T(n) is the sum of the works of its nodes. For the family T(n) = s(n) T(f(n)) + w(n) with base T0 for n <= n0, counting nodes per level with the iterates of f and the level f*(n,n0) gives the closed formula T(n) = sum over levels of [product of s] w + [product of s] T0, proved by induction and applied to merge sort, Hanoi and square-root recurrences.Recurrence Relations - Recursion Tree and General Formula →).

Remark. With precomputed powers ωni\omega_n^i, each iteration is one product and two sums: 32nlog⁡2n\frac32n\log_2n in total. The course count 2nlog⁡2n2n\log_2n includes updating ONI.

Example n=4n=4

a=(3,4,5,6)\mathbf a=(3,4,5,6), ω4=i\omega_4=i. Subinstances (3,5)(3,5) and (4,6)(4,6), with ω2=−1\omega_2=-1: FFT(u,v)=(u+v,u−v)\mathrm{FFT}(u,v)=(u+v,u-v). y[0]=(8,−2)\mathbf y^{[0]}=(8,-2), y[1]=(10,−2)\mathbf y^{[1]}=(10,-2). i=0i=0: y0=18y_0=18, y2=−2y_2=-2. i=1i=1: t=i(−2)=−2it=i(-2)=-2i, y1=−2−2iy_1=-2-2i, y3=−2+2iy_3=-2+2i. Result (18,−2−2i,−2,−2+2i)(18,-2-2i,-2,-2+2i).

Example n=8n=8

a=(1,…,8)\mathbf a=(1,\dots,8). Leaves read left to right: a0,a4,a2,a6,a1,a5,a3,a7a_0,a_4,a_2,a_6,a_1,a_5,a_3,a_7 (bit-reversed indices). Order-2 level: (1,5)→(6,−4)(1,5)\to(6,-4), (3,7)→(10,−4)(3,7)\to(10,-4), (2,6)→(8,−4)(2,6)\to(8,-4), (4,8)→(12,−4)(4,8)\to(12,-4). Order-4 level: even part (1,3,5,7)→(16,−4−4i,−4,−4+4i)(1,3,5,7)\to(16,-4-4i,-4,-4+4i); odd part (2,4,6,8)→(20,−4−4i,−4,−4+4i)(2,4,6,8)\to(20,-4-4i,-4,-4+4i). Last level (ω8=1+i2\omega_8=\frac{1+i}{\sqrt2}):

ii ω8i\omega_8^i t=ω8iyi[1]t=\omega_8^iy^{[1]}_i yi=yi[0]+ty_i=y^{[0]}_i+t yi+4=yi[0]−ty_{i+4}=y^{[0]}_i-t
0 11 2020 3636 −4-4
1 1+i2\frac{1+i}{\sqrt2} −42 i-4\sqrt2\,i −4−(4+42)i-4-(4+4\sqrt2)i −4+(42−4)i-4+(4\sqrt2-4)i
2 ii −4i-4i −4−4i-4-4i −4+4i-4+4i
3 −1+i2\frac{-1+i}{\sqrt2} −42 i-4\sqrt2\,i −4+(4−42)i-4+(4-4\sqrt2)i −4+(4+42)i-4+(4+4\sqrt2)i

DFT8(1,…,8)=(36,−4−9.657i,−4−4i,−4−1.657i,−4,−4+1.657i,−4+4i,−4+9.657i)\mathrm{DFT}_8(1,\dots,8)=(36,-4-9.657i,-4-4i,-4-1.657i,-4,-4+1.657i,-4+4i,-4+9.657i), with conjugate symmetry yn−i=yi‾y_{n-i}=\overline{y_i} because a\mathbf a is real.

The inverse transform

DFTn−1(y)=Fn−1y\mathrm{DFT}^{-1}_n(\mathbf y)=F_n^{-1}\mathbf y is the interpolation from Ωn\Omega_n.

Theorem. [Fn−1]ij=1n ωn−ij\bigl[F_n^{-1}\bigr]_{ij}=\frac1n\,\omega_n^{-ij}.

Proof. Let Gij=1nωn−ijG_{ij}=\frac1n\omega_n^{-ij}. Then (GFn)ij=1n∑k=0n−1ωn−ikωnkj=1n∑k(ωnj−i)k(GF_n)_{ij}=\frac1n\sum_{k=0}^{n-1}\omega_n^{-ik}\omega_n^{kj}=\frac1n\sum_{k}(\omega_n^{j-i})^k. If i=ji=j the ratio is 11 and the sum is nn: entry 11. If i≠ji\ne j, j−i∈[−(n−1),n−1]∖{0}j-i\in[-(n-1),n-1]\setminus\{0\} contains no multiple of nn, so by the summation lemma the sum is 00. Hence GFn=IGF_n=I (a left inverse of a square matrix is the inverse). □\square

Explicitly ai=1n∑kykωn−ik=1nq(ωn−i)a_i=\frac1n\sum_ky_k\omega_n^{-ik}=\frac1nq(\omega_n^{-i}) with q(x)=∑kykxkq(x)=\sum_ky_kx^k: the interpolation from Ωn\Omega_n is an evaluation of the polynomial with coefficients y\mathbf y at the points ωˉni=ωn−i\bar\omega_n^i=\omega_n^{-i}, followed by a division by nn.

Method 1: FFT on negative powers. The lemmas hold for ωˉn=ωn−1\bar\omega_n=\omega_n^{-1} too, so the same FFT works with ωˉn\bar\omega_n:

FFT_NEG(y)
n <- y.len
if n = 1 then return y
y0 <- (y[0], y[2], ..., y[n-2]);  y1 <- (y[1], y[3], ..., y[n-1])
a0 <- FFT_NEG(y0);  a1 <- FFT_NEG(y1)
ON_NEG <- e^(-i 2 pi / n)                   { = w_n^(-1) }
ONI <- 1
for i <- 0 to n/2 - 1
    t <- ONI * a1[i]
    a[i] <- a0[i] + t
    a[i + n/2] <- a0[i] - t
    ONI <- ONI * ON_NEG
return a

INV_FFT(y)
n <- y.len
a <- FFT_NEG(y)
for i <- 0 to n-1
    a[i] <- a[i] / n
return a

The division by nn must be done outside the recursion, once: inside, each level would divide by its own size and the wrong factor would accumulate. Cost TINV(n)=2nlog⁡2n+n=Θ(nlog⁡n)T_{\text{INV}}(n)=2n\log_2n+n=\Theta(n\log n).

Method 2: same FFT and reversal. Since ωˉni=ωn(n−i) mod n\bar\omega_n^i=\omega_n^{(n-i)\bmod n}, DFTn−1(y)=1n DFTn(y)R,xR=(x0,xn−1,xn−2,…,x1).\mathrm{DFT}^{-1}_n(\mathbf y)=\tfrac1n\,\mathrm{DFT}_n(\mathbf y)^R,\qquad\mathbf x^R=(x_0,x_{n-1},x_{n-2},\dots,x_1).

INV_FFT(y)
n <- y.len
a' <- FFT(y)
a[0] <- a'[0] / n
for i <- 1 to n-1
    a[i] <- a'[n-i] / n
return a

Example. y=(18,−2−2i,−2,−2+2i)\mathbf y=(18,-2-2i,-2,-2+2i): FFT(y)=(12,24,20,16)\mathrm{FFT}(\mathbf y)=(12,24,20,16) (e.g. z1=18+i(−2−2i)+(−1)(−2)+(−i)(−2+2i)=18+(2−2i)+2+(2+2i)=24z_1=18+i(-2-2i)+(-1)(-2)+(-i)(-2+2i)=18+(2-2i)+2+(2+2i)=24); reversed (12,16,20,24)(12,16,20,24); divided by 44: (3,4,5,6)(3,4,5,6).

Polynomial product with the FFT

LIN_CONV(a, b)
n <- a.len                                   { n power of two }
ya <- FFT((a | 0_n));  yb <- FFT((b | 0_n))  { evaluation on Omega_2n }
for i <- 0 to 2n-1
    yc[i] <- ya[i] * yb[i]
return INV_FFT(yc)                           { returns (c | 0) }

Exact cost. Three transforms of order 2n2n, each 2⋅2nlog⁡2(2n)=4nlog⁡2(2n)2\cdot2n\log_2(2n)=4n\log_2(2n); plus 2n2n products ⊙\odot and 2n2n divisions in the inverse: TLIN_CONV(n)=12 nlog⁡2(2n)+4n=Θ(nlog⁡n).T_{\text{LIN\_CONV}}(n)=12\,n\log_2(2n)+4n=\Theta(n\log n). Direct convolution costs 2n2−2n+12n^2-2n+1:

nn 8 16 32 64 128 1024
definition 113 481 1985 8065 32513 2095105
LIN_CONV 416 1024 2432 5632 12800 139264

The FFT wins from n=64n=64 (8065 against 5632); for small nn the definition is cheaper (lower constants), the same phenomenon that motivates Hybrid AlgorithmsHybridization combines two algorithms for the same problem so the result has the asymptotics of the better one and, if possible, lower constants. Choosing the algorithm only by the starting size (STUPID_HYBRID) does not help; changing the base case of the recursion (H_MUL: iterative product for n <= n0 at every leaf of Strassen's tree) gives cost n^(log2 7)(2n0+5)/n0^(log2 7 - 2) - 6n^2, minimal for n0 = 8 with leading constant about 3.92.Hybrid Algorithms →.

General sizes. If nn is not a power of two, pad both vectors to 2⌈log⁡2n⌉+12^{\lceil\log_2n\rceil+1}, the first power of two at least 2n2n (and below 4n4n). For lengths m,nm,n pad to the first power of two ≥m+n−1\ge m+n-1. The cost remains Θ(nlog⁡n)\Theta(n\log n).

Numerical note. In floating point the roots are approximate and the result has small errors (e.g. imaginary parts of order 10−1510^{-15} instead of 00); with integer coefficients one rounds.

Versione ripasso

  • Substructure (n=2kn=2^k, DFT and Roots of UnityThe DFT of order n evaluates a polynomial with degree bound n at the n n-th roots of unity, y_i = p(w^i) with w = e^{2 pi i/n}; it is the product by the Fourier matrix F_n, an invertible Vandermonde matrix. The roots form a multiplicative group and satisfy three lemmas: cancellation (w_{dn}^{dk} = w_n^k, w_n^{n/2} = -1), halving (the squares of the n roots are the n/2 roots of order n/2, each twice) and summation (the sum of the powers of w^k is n if k is a multiple of n, 0 otherwise). Linear convolution theorem: (c|0) = DFT^{-1}{2n}(DFT{2n}(a|0) times DFT_{2n}(b|0)).DFT and Roots of Unity →): DFT1(a)=a\mathrm{DFT}_1(\mathbf a)=\mathbf a. Split the coefficients into even a[0]\mathbf a^{[0]} and odd a[1]\mathbf a^{[1]}; identity p(x)=p[0](x2)+x p[1](x2)p(x)=p^{[0]}(x^2)+x\,p^{[1]}(x^2) (even-degree plus odd-degree monomials). For 0≤i<n/20\le i<n/2: yi=yi[0]+ωniyi[1]y_i=y^{[0]}_i+\omega_n^iy^{[1]}_i, yi+n/2=yi[0]−ωniyi[1]y_{i+n/2}=y^{[0]}_i-\omega_n^iy^{[1]}_i. Proof: identity, halving (ωni)2=ωn/2i(\omega_n^i)^2=\omega_{n/2}^i; second half uses ωnn/2=−1\omega_n^{n/2}=-1, ωnn=1\omega_n^n=1.
  • FFT: base n=1n=1; divide even/odd; two calls of size n/2n/2; loop for i <- 0 to n/2-1: t <- ONI*y1[i], y[i] <- y0[i]+t, y[i+n/2] <- y0[i]-t, ONI <- ONI*ON. Correctness by induction on kk with n=2kn=2^k (hypothesis on the two subinstances, ONI =ωni=\omega_n^i).
  • Cost: 44 operations per iteration, n/2n/2 iterations: T(n)=2T(n/2)+2nT(n)=2T(n/2)+2n, T(1)=0T(1)=0. Exact T(n)=2nlog⁡2nT(n)=2n\log_2n (induction: 2n(log⁡2n−1)+2n2n(\log_2n-1)+2n); Case 2 of the Master TheoremThe Master Theorem for T(n) = aT(n/b) + w(n) compares w(n) with the threshold n^(log_b a). Case 1 (w polynomially smaller): Theta(n^(log_b a)); case 2 (same order): Theta(n^(log_b a) log n); case 3 (polynomially larger and regular): Theta(w(n)). Full proof from the levels of the tree, extended version with floors and ceilings, a table of applications, and the misuse when no case applies (w = n log n).Master Theorem → (threshold nn, work 2n2n). With precomputed powers 32nlog⁡2n\frac32n\log_2n. Tree: log⁡2n+1\log_2n+1 levels, nn leaves, work 2n2n per level.
  • Example n=4n=4: (3,4,5,6)(3,4,5,6): (3,5)↦(8,−2)(3,5)\mapsto(8,-2), (4,6)↦(10,−2)(4,6)\mapsto(10,-2); y0=18y_0=18, y2=−2y_2=-2, y1=−2+i(−2)=−2−2iy_1=-2+i(-2)=-2-2i, y3=−2+2iy_3=-2+2i.
  • Example n=8n=8: DFT8(1,…,8)=(36,−4−9.657i,−4−4i,−4−1.657i,−4,−4+1.657i,−4+4i,−4+9.657i)\mathrm{DFT}_8(1,\dots,8)=(36,-4-9.657i,-4-4i,-4-1.657i,-4,-4+1.657i,-4+4i,-4+9.657i); even (1,3,5,7)→(16,−4−4i,−4,−4+4i)(1,3,5,7)\to(16,-4-4i,-4,-4+4i), odd (2,4,6,8)→(20,−4−4i,−4,−4+4i)(2,4,6,8)\to(20,-4-4i,-4,-4+4i); conjugate symmetry since a\mathbf a is real.
  • Inverse: [Fn−1]ij=1nωn−ij[F_n^{-1}]_{ij}=\frac1n\omega_n^{-ij}. Proof: (Fn−1Fn)ij=1n∑k(ωnj−i)k(F_n^{-1}F_n)_{ij}=\frac1n\sum_k(\omega_n^{j-i})^k is 11 if i=ji=j, 00 otherwise because j−i∈[−(n−1),n−1]∖{0}j-i\in[-(n-1),n-1]\setminus\{0\} is not a multiple of nn (summation lemma). So ai=1nq(ωn−i)a_i=\frac1nq(\omega_n^{-i}) with q≡yq\equiv\mathbf y: interpolation from Ωn\Omega_n is an evaluation at negative powers.
  • INV_FFT, method 1: FFT_NEG (same FFT with ωn−1\omega_n^{-1}), then divide by nn outside the recursion; cost 2nlog⁡2n+n2n\log_2n+n. Method 2: DFTn−1(y)=1nDFTn(y)R\mathrm{DFT}^{-1}_n(\mathbf y)=\frac1n\mathrm{DFT}_n(\mathbf y)^R with xR=(x0,xn−1,…,x1)\mathbf x^R=(x_0,x_{n-1},\dots,x_1), since ωn−i=ωnn−i\omega_n^{-i}=\omega_n^{n-i}. Example: FFT(18,−2−2i,−2,−2+2i)=(12,24,20,16)\mathrm{FFT}(18,-2-2i,-2,-2+2i)=(12,24,20,16), reversed (12,16,20,24)(12,16,20,24), divided by 44: (3,4,5,6)(3,4,5,6).
  • LIN_CONV: pad to 2n2n, two FFT, product ⊙\odot, INV_FFT; cost 12nlog⁡2(2n)+4n12n\log_2(2n)+4n against 2n2−2n+12n^2-2n+1 by definition (FFT wins from n=64n=64: 56325632 against 80658065). Non-power-of-two sizes: pad to 2⌈log⁡2n⌉+1<4n2^{\lceil\log_2n\rceil+1}<4n; lengths m,nm,n: first power of two ≥m+n−1\ge m+n-1.
  • Pseudocode of the inverse: FFT_NEG is the FFT with ON_NEG <- e^(-i 2 pi / n); INV_FFT(y): a <- FFT_NEG(y); for i <- 0 to n-1: a[i] <- a[i]/n; return a. Method 2: a' <- FFT(y); a[0] <- a'[0]/n; for i <- 1 to n-1: a[i] <- a'[n-i]/n.
  • Common mistakes: dividing by nn inside the recursion; using ωn\omega_n instead of ωn−1\omega_n^{-1} in the inverse; forgetting to reverse the indices in method 2 (index 00 stays); applying the FFT to non-powers of two without padding.

Teoria collegata