TExcelTAD

Roots of Unity and the DFT

The fast Fourier transform evaluates a polynomial at nn points in Θ(nlg⁡n)\Theta(n \lg n) time, but only at very special points: the complex nn-th roots of unity. Three small lemmas about these numbers, cancellation, halving and summation, are all the FFT needs. This lesson proves them, defines the discrete Fourier transform (DFT) as evaluation at the roots of unity and computes it the slow way, in Θ(n2)\Theta(n^2), as a reference for the fast versions to come. For the signal-processing view of the same transform, see The DFT in Signals and Fourier Analysis; complex numbers themselves are reviewed in Complex Numbers.

The nn-th roots of unity

A complex nn-th root of unity is a complex number ω\omega with ωn=1\omega^n = 1. There are exactly nn of them, equally spaced on the unit circle:

ωnk=e2πik/n=cos⁡2πkn+isin⁡2πkn,k=0,1,…,n−1,\omega_n^k = e^{2\pi i k / n} = \cos\frac{2\pi k}{n} + i \sin\frac{2\pi k}{n}, \qquad k = 0, 1, \ldots, n - 1,

where ωn=e2πi/n\omega_n = e^{2\pi i/n} is the principal nn-th root of unity. Multiplying by ωn\omega_n rotates by 2π/n2\pi/n, so the powers of ωn\omega_n walk around the circle and repeat with period nn: ωnk+n=ωnk\omega_n^{k + n} = \omega_n^k. The roots form a group under multiplication, and ωnjωnk=ωn(j+k) mod n\omega_n^j \omega_n^k = \omega_n^{(j + k) \bmod n}.

python · try it yourself
import cmath

def clean(z, digits=4):
    """round a complex number and remove negative zeros, so the printout is stable"""
    return complex(round(z.real, digits) + 0.0, round(z.imag, digits) + 0.0)

n = 8
w = cmath.exp(2j * cmath.pi / n)
roots = [w ** k for k in range(n)]
for k, r in enumerate(roots):
    print(f"w_8^{k} = {clean(r)}")
print("w_8^8 =", clean(w ** 8))
w_8^0 = (1+0j)
w_8^1 = (0.7071+0.7071j)
w_8^2 = 1j
w_8^3 = (-0.7071+0.7071j)
w_8^4 = (-1+0j)
w_8^5 = (-0.7071-0.7071j)
w_8^6 = -1j
w_8^7 = (0.7071-0.7071j)
w_8^8 = (1+0j)

Three lemmas

Cancellation lemma. For integers n,k≥0n, k \ge 0 and d>0d > 0: ωdndk=ωnk\omega_{dn}^{dk} = \omega_n^k. Proof: ωdndk=e2πidk/(dn)=e2πik/n\omega_{dn}^{dk} = e^{2\pi i dk/(dn)} = e^{2\pi i k/n}. As a special case, ωnn/2=ω2=−1\omega_n^{n/2} = \omega_2 = -1 for even nn.

Halving lemma. If n>0n > 0 is even, the squares of the nn complex nn-th roots of unity are the n/2n/2 complex (n/2)(n/2)-th roots of unity, each appearing exactly twice. Proof: by cancellation, (ωnk)2=ωn/2k(\omega_n^k)^2 = \omega_{n/2}^k; and (ωnk+n/2)2=ωn2k+n=ωn2k=(ωnk)2(\omega_n^{k + n/2})^2 = \omega_n^{2k + n} = \omega_n^{2k} = (\omega_n^k)^2, so kk and k+n/2k + n/2 give the same square. Equivalently, ωnk+n/2=−ωnk\omega_n^{k + n/2} = -\omega_n^k: the roots come in pairs of opposite numbers.

Summation lemma. For every integer kk that is not a multiple of nn,

∑j=0n−1(ωnk)j=0.\sum_{j=0}^{n-1} \left(\omega_n^k\right)^j = 0 .

Proof: it is a geometric series with ratio ωnk≠1\omega_n^k \ne 1, so it equals (ωnk)n−1ωnk−1=(ωnn)k−1ωnk−1=1−1ωnk−1=0\frac{(\omega_n^k)^n - 1}{\omega_n^k - 1} = \frac{(\omega_n^n)^k - 1}{\omega_n^k - 1} = \frac{1 - 1}{\omega_n^k - 1} = 0. When kk is a multiple of nn, every term is 1 and the sum is nn.

The halving lemma is what makes divide and conquer work: evaluating at nn points will reduce to evaluating at n/2n/2 points, the squares. The summation lemma will make the inverse transform easy. The program checks both numerically.

python · try it yourself
import cmath

def clean(z, digits=6):
    return complex(round(z.real, digits) + 0.0, round(z.imag, digits) + 0.0)

n = 8
w = lambda m, k: cmath.exp(2j * cmath.pi * k / m)
squares = [clean(w(n, k) ** 2) for k in range(n)]
quarter_roots = [clean(w(n // 2, k)) for k in range(n // 2)]
print("squares of the 8th roots:", squares)
print("the 4th roots:           ", quarter_roots)
print("each 4th root appears twice:", all(squares.count(r) == 2 for r in quarter_roots))
print("w_8^(k+4) = -w_8^k for all k:", all(clean(w(n, k + 4)) == clean(-w(n, k)) for k in range(n)))
for k in range(0, 9):
    s = sum(w(n, k) ** j for j in range(n))
    print(f"k = {k}: sum of (w_8^k)^j = {clean(s)}")
squares of the 8th roots: [(1+0j), 1j, (-1+0j), -1j, (1+0j), 1j, (-1+0j), -1j]
the 4th roots:            [(1+0j), 1j, (-1+0j), -1j]
each 4th root appears twice: True
w_8^(k+4) = -w_8^k for all k: True
k = 0: sum of (w_8^k)^j = (8+0j)
k = 1: sum of (w_8^k)^j = 0j
k = 2: sum of (w_8^k)^j = 0j
k = 3: sum of (w_8^k)^j = 0j
k = 4: sum of (w_8^k)^j = 0j
k = 5: sum of (w_8^k)^j = 0j
k = 6: sum of (w_8^k)^j = 0j
k = 7: sum of (w_8^k)^j = 0j
k = 8: sum of (w_8^k)^j = (8+0j)

The discrete Fourier transform

Let A(x)=∑j=0n−1ajxjA(x) = \sum_{j=0}^{n-1} a_j x^j have degree-bound nn, a power of 2. Its DFT is the vector of its values at the nn-th roots of unity:

yk=A(ωnk)=∑j=0n−1aj ωnkj,k=0,1,…,n−1,y_k = A(\omega_n^k) = \sum_{j=0}^{n-1} a_j\, \omega_n^{kj}, \qquad k = 0, 1, \ldots, n - 1,

written y=DFTn(a)y = \mathrm{DFT}_n(a). In matrix form y=Vnay = V_n a, where VnV_n is the Vandermonde matrix of the roots, with entries (Vn)kj=ωnkj(V_n)_{kj} = \omega_n^{kj}. Computing it directly costs n2n^2 complex multiplications (one per matrix entry).

To multiply polynomials of degree-bound nn we need 2n2n values, so the coefficient vectors are padded with zeros to length 2n2n before the transform. Padding does not change the polynomial, only the degree-bound.

python · try it yourself
import cmath

def clean(z, digits=4):
    return complex(round(z.real, digits) + 0.0, round(z.imag, digits) + 0.0)

def dft(a):
    n = len(a)
    w = cmath.exp(2j * cmath.pi / n)
    mults = 0
    y = []
    for k in range(n):
        total = 0
        for j in range(n):
            total += a[j] * w ** (k * j)
            mults += 1
        y.append(total)
    return y, mults

a = [5, -2, 0, 3, 0, 0, 0, 0]                    # 5 - 2x + 3x^3, padded to n = 8
y, mults = dft(a)
print(f"DFT_8 used {mults} multiplications")
for k, v in enumerate(y):
    print(f"  A(w_8^{k}) = {clean(v)}")
print("y_0 = sum of the coefficients:", clean(y[0]), " y_4 = A(-1):", clean(y[4]))
DFT_8 used 64 multiplications
  A(w_8^0) = (6+0j)
  A(w_8^1) = (1.4645+0.7071j)
  A(w_8^2) = (5-5j)
  A(w_8^3) = (8.5355+0.7071j)
  A(w_8^4) = (4+0j)
  A(w_8^5) = (8.5355-0.7071j)
  A(w_8^6) = (5+5j)
  A(w_8^7) = (1.4645-0.7071j)
y_0 = sum of the coefficients: (6+0j)  y_4 = A(-1): (4+0j)

Two values are easy to check by hand: y0=A(1)y_0 = A(1) is the sum of the coefficients, 5−2+3=65 - 2 + 3 = 6, and y4=A(ω84)=A(−1)=5+2−3=4y_4 = A(\omega_8^4) = A(-1) = 5 + 2 - 3 = 4.

Note: Signal-processing libraries, including NumPy's np.fft.fft, use the opposite sign convention, ∑jaje−2πikj/n\sum_j a_j e^{-2\pi i kj/n}. With that convention our DFT is n * np.fft.ifft(a). Either sign works for multiplication, as long as the inverse uses the other one.

In C++

The direct DFT in C++ with std::complex<double>. Each root is computed from the angle 2πkj/n2\pi kj/n instead of by repeated multiplication, which keeps rounding errors small.

cpp · try it yourself
#include <cmath>
#include <complex>
#include <cstdio>
#include <vector>
using cd = std::complex<double>;

std::vector<cd> dft(const std::vector<cd>& a) {
    size_t n = a.size();
    const double PI = std::acos(-1.0);
    std::vector<cd> y(n);
    for (size_t k = 0; k < n; k++)
        for (size_t j = 0; j < n; j++)
            y[k] += a[j] * std::polar(1.0, 2 * PI * double((k * j) % n) / double(n));
    return y;
}

double clean(double x) { double r = std::round(x * 10000) / 10000; return r == 0 ? 0.0 : r; }

int main() {
    std::vector<cd> a = {5, -2, 0, 3, 0, 0, 0, 0};
    auto y = dft(a);
    for (size_t k = 0; k < y.size(); k++)
        std::printf("A(w_8^%zu) = %8.4f %+8.4fi\n", k, clean(y[k].real()), clean(y[k].imag()));
}
A(w_8^0) =   6.0000  +0.0000i
A(w_8^1) =   1.4645  +0.7071i
A(w_8^2) =   5.0000  -5.0000i
A(w_8^3) =   8.5355  +0.7071i
A(w_8^4) =   4.0000  +0.0000i
A(w_8^5) =   8.5355  -0.7071i
A(w_8^6) =   5.0000  +5.0000i
A(w_8^7) =   1.4645  -0.7071i

Test yourself

What is ω84\omega_8^{4}?

  1. 11
  2. ii
  3. −1-1
  4. −i-i

What does the halving lemma say about the squares of the 8th roots of unity?

  1. They are all 1
  2. They are the 4th roots of unity, each appearing twice
  3. They are the 16th roots of unity
  4. They are the 8th roots in a different order

What is ∑j=07(ω83)j\sum_{j=0}^{7} (\omega_8^3)^j?

  1. 00
  2. 11
  3. 88
  4. ω83\omega_8^3

How many complex multiplications does the direct DFT of length nn use?

  1. nn
  2. nlg⁡nn \lg n
  3. n2n^2
  4. 2n2^n