Roots of Unity and the DFT
The fast Fourier transform evaluates a polynomial at points in time, but only at very special points: the complex -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 , 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 -th roots of unity
A complex -th root of unity is a complex number with . There are exactly of them, equally spaced on the unit circle:
where is the principal -th root of unity. Multiplying by rotates by , so the powers of walk around the circle and repeat with period : . The roots form a group under multiplication, and .
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 and : . Proof: . As a special case, for even .
Halving lemma. If is even, the squares of the complex -th roots of unity are the complex -th roots of unity, each appearing exactly twice. Proof: by cancellation, ; and , so and give the same square. Equivalently, : the roots come in pairs of opposite numbers.
Summation lemma. For every integer that is not a multiple of ,
Proof: it is a geometric series with ratio , so it equals . When is a multiple of , every term is 1 and the sum is .
The halving lemma is what makes divide and conquer work: evaluating at points will reduce to evaluating at points, the squares. The summation lemma will make the inverse transform easy. The program checks both numerically.
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 have degree-bound , a power of 2. Its DFT is the vector of its values at the -th roots of unity:
written . In matrix form , where is the Vandermonde matrix of the roots, with entries . Computing it directly costs complex multiplications (one per matrix entry).
To multiply polynomials of degree-bound we need values, so the coefficient vectors are padded with zeros to length before the transform. Padding does not change the polynomial, only the degree-bound.
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: is the sum of the coefficients, , and .
Note: Signal-processing libraries, including NumPy's
np.fft.fft, use the opposite sign convention, . With that convention our DFT isn * 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 instead of by repeated multiplication, which keeps rounding errors small.
#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 ?
What does the halving lemma say about the squares of the 8th roots of unity?
- They are all 1
- They are the 4th roots of unity, each appearing twice
- They are the 16th roots of unity
- They are the 8th roots in a different order
What is ?
How many complex multiplications does the direct DFT of length use?