Orthogonal Projections and Fourier Series
The orthogonal projection of a vector onto a subspace is the closest point of that subspace to the vector. With an orthonormal basis it is a sum of inner products, and applied to functions with the basis of sines and cosines it becomes a Fourier series.
Projection onto a subspace
Let be a subspace of an inner product space with an orthogonal basis . The orthogonal projection of onto is
The key property: the remainder is orthogonal to . Check it against each : . So every splits uniquely as
Best approximation theorem. is the point of closest to . For any other in , with , so by Pythagoras .
If you only have a non-orthogonal basis, put it in the columns of a matrix ; then the projection matrix is . With orthonormal columns it simplifies to . Projection matrices satisfy (projecting twice changes nothing) and .
import numpy as np
A = np.array([[1.0, 0.0],
[1.0, 1.0],
[1.0, 2.0]]) # columns span a plane W in R^3
P = A @ np.linalg.inv(A.T @ A) @ A.T
v = np.array([6.0, 0.0, 0.0])
w = P @ v
r = v - w
print("proj =", np.round(w, 4), " remainder =", np.round(r, 4))
print("remainder ⊥ columns:", np.round(A.T @ r, 10) + 0.0)
print("P^2 = P:", np.allclose(P @ P, P), " P^T = P:", np.allclose(P.T, P))
Q, _ = np.linalg.qr(A)
print("QQ^T equals P:", np.allclose(Q @ Q.T, P))proj = [ 5. 2. -1.] remainder = [ 1. -2. 1.]
remainder ⊥ columns: [0. 0.]
P^2 = P: True P^T = P: True
QQ^T equals P: True
Functions: the trigonometric system
On the space of functions on with , the functions
are mutually orthogonal, with and for . (For example for , by the product-to-sum identity.)
Projecting onto the span of the first terms is therefore just the projection formula above. The coefficients are
is the Fourier partial sum: the best approximation of , in the sense, by a trigonometric polynomial of degree .
A square wave
Take for and for . is odd, so every is 0. For , , which is for odd and 0 for even .
import numpy as np
from scipy.integrate import quad
import matplotlib.pyplot as plt
f = lambda x: np.sign(x)
b = {n: quad(lambda x: f(x) * np.sin(n * x), -np.pi, np.pi, points=[0])[0] / np.pi
for n in range(1, 10)}
print("b_n:", {n: round(v, 4) + 0.0 for n, v in b.items()})
print("4/(n*pi) for n = 1, 3, 5:", [round(4 / (n * np.pi), 4) for n in (1, 3, 5)])
xs = np.linspace(-np.pi, np.pi, 801)
plt.figure(figsize=(6, 4))
plt.plot(xs, f(xs), "k", lw=1, label="square wave")
for N in (1, 3, 9):
S = sum(b[n] * np.sin(n * xs) for n in range(1, N + 1))
plt.plot(xs, S, label=f"S_{N}")
err2 = 2 * np.pi - np.pi * sum(b[n] ** 2 for n in range(1, N + 1))
print(f"N={N}: squared L2 error = {err2:.4f}")
plt.legend(); plt.title("Fourier partial sums are projections")b_n: {1: 1.2732, 2: 0.0, 3: 0.4244, 4: 0.0, 5: 0.2546, 6: 0.0, 7: 0.1819, 8: 0.0, 9: 0.1415}
4/(n*pi) for n = 1, 3, 5: [1.2732, 0.4244, 0.2546]
N=1: squared L2 error = 1.1902
N=3: squared L2 error = 0.6243
N=9: squared L2 error = 0.2538
The squared error comes straight from Pythagoras: . It shrinks as more terms are added. Near the jump the partial sums overshoot by about 9% of the jump (peaking near 1.18) no matter how many terms you take (the Gibbs phenomenon); the overshoot gets narrower, so its contribution to the error still goes to 0.
Note: Whether converges to point by point is a subtle analysis question; convergence in the norm, which is what projection gives you, is the clean statement.
In software
- Audio and images. MP3, AAC and JPEG store a few coefficients of a cosine basis (the discrete cosine transform) and drop the small ones. That is projection onto a subspace of "important" frequencies.
- FFT.
np.fft.fftcomputes all inner products with sampled complex exponentials in time (SciPy FFT). - Machine learning. Linear regression's prediction is the projection of the target vector onto the column space of the feature matrix (Least Squares).
- Graphics. Projecting velocity onto a surface (removing the normal component) is how characters slide along walls (Projection and Reflection).
Exercises
Exercise 1: Onto a line
Project onto the line spanned by , and give the remainder.
Answer
, .
Solution
Exercise 2: Onto a plane with an orthogonal basis
Project onto .
Answer
.
Solution
The basis is orthogonal. Coefficients: and . . is the -plane, so the answer simply drops the -coordinate.
Exercise 3: A projection matrix
Find for the line spanned by , and check .
Answer
.
Solution
For one vector, and . .
from sympy import Matrix
u = Matrix([1, 2, 2])
P = u * u.T / u.dot(u)
print(P.tolist(), P**2 == P)[[1/9, 2/9, 2/9], [2/9, 4/9, 4/9], [2/9, 4/9, 4/9]] True
Exercise 4: Fourier coefficients of (code)
On , find for and the first three values.
Answer
: , , .
Solution
is odd, so . Integration by parts gives ; divide by .
from sympy import symbols, integrate, sin, pi
x = symbols("x")
print([integrate(x * sin(n * x), (x, -pi, pi)) / pi for n in (1, 2, 3)])[2, -1, 2/3]
Test yourself
What is true of the remainder ?
- It lies in
- It is orthogonal to every vector of
- It is always zero
- It has the same length as
Which identity does every orthogonal projection matrix satisfy?
- and
The Fourier coefficient is computed as because…
- the sines are orthogonal and on
- is the period
- is always odd
- it is a convention with no reason