Topic

The discrete Fourier transform

For n numbers in a row, the transform is a finite sum, and the frequencies are the n-th roots of unity. With ζ=e2πi/n\zeta = e^{2\pi i/n},

Xk=∑j=0n−1xj ζ−jk,xj=1n∑k=0n−1Xk ζjk. X_k = \sum_{j=0}^{n-1} x_j\,\zeta^{-jk}, \qquad x_j = \frac1n\sum_{k=0}^{n-1} X_k\,\zeta^{jk} .
dft([1, 2, 3, 4]).text;            // ["10", "-2 + 2ζ", "-2", "-2 - 2ζ"]   (ζ = i here)
dft(X.values, { inverse: true });  // back to the rationals, exactly
fft([1, 2, 3, 4, 5, 6, 7, 8]);     // the same values, 12 multiplications instead of 64
alias(7, 8);                       // 7 Hz sampled at 8 Hz looks like 1 Hz

Exact arithmetic with roots of unity

Floating point makes the transform of a pure tone a spike surrounded by entries like 1e-16. Here the entries are exact. Every power of ζ is a root of the cyclotomic polynomial Φn\Phi_n, whose degree is φ(n), so each number built from rationals and powers of ζ can be written in exactly one way as a polynomial in ζ of degree below φ(n). Cyclotomic stores those coefficients. Two such numbers are equal exactly when their coefficients are, a zero is the number 0, and the inverse transform returns the rationals it started from.

For n = 4, ζ is i and Φ4=ζ2+1\Phi_4 = \zeta^2 + 1, so the entries are Gaussian rationals a + bζ. For n = 5, Φ5=ζ4+ζ3+ζ2+ζ+1\Phi_5 = \zeta^4 + \zeta^3 + \zeta^2 + \zeta + 1 and an entry is a combination of 1, ζ, ζ², ζ³. enclose(digits) turns any entry into certified real and imaginary parts, and power() gives ∣Xk∣2|X_k|^2, which is the product with the conjugate (ζ ↦ ζ⁻¹) and is computed exactly too.

The fast algorithm beside the definition

The definition does n multiplications by a root of unity for each of n outputs: n² in all. When n is even, the sum splits into the even-numbered and the odd-numbered inputs,

Xk=Ek+ζ−kOk,Xk+n/2=Ek−ζ−kOk, X_k = E_k + \zeta^{-k} O_k, \qquad X_{k + n/2} = E_k - \zeta^{-k} O_k,

where E and O are transforms of half the length. So one transform of length n costs two of length n/2 and n/2 multiplications, and for n a power of two that is n2log⁡2n\tfrac n2 \log_2 n in all. fft runs this recursion in the same exact arithmetic, counts its multiplications, and compares its output with dft, exactly. For n = 1024 the counts are 5120 against 1048576.

Sampling and aliasing

Sampling at rate r reads a signal only at the times k/r. At those times cos⁡2πft\cos 2\pi f t and cos⁡2π(f+r)t\cos 2\pi (f + r) t are the same list of numbers, because the extra term adds a whole number of turns at every sample. So every frequency has an alias between 0 and r/2 (the Nyquist frequency) that gives identical samples, and alias(f, r) computes it with exact rationals. A sine comes back with sign: -1 when the fold reverses it: 7 Hz sampled at 8 Hz is a 1 Hz sine upside down.

sinusoidSamples({ frequency, rate, count }) gives the samples exactly, as Cyclotomic numbers (it needs the samples to cover whole periods). Their transform is exactly two spikes, one at the alias and one at its mirror image, and nothing else. sample(f, { rate, count }) samples any formula as enclosures.

callreturns
dft(values, { inverse }){ n, values, text, enclose(digits), power(), multiplications }
fft(values, { inverse, check })the same, with multiplications, definition, levels, perLevel, agrees
rootOfUnity(n, k), cyclotomic(n)Cyclotomic ζᵏ; the coefficients of Φₙ
z.add, subtract, multiply, scale, rotate(k), conjugate(), norm2()Cyclotomic
z.equals(w), z.rational(), z.enclose(digits), z.toNumber(), z.toString()
alias(frequency, rate){ frequency, rate, nyquist, alias, sign, aliased }
sinusoidSamples({ frequency, rate, count, kind }), sample(f, { rate, count, from })Cyclotomic[]; { times, values }
Try itEdit the code and run it with @ratmath/analytic and Core's R, Rational, RationalInterval in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs