TExcelTAD
Home › Linear Algebra & Matrix Theory › Orthogonal Projections and Fourier Series

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 WW be a subspace of an inner product space VV with an orthogonal basis u1,…,uk\mathbf{u}_1, \ldots, \mathbf{u}_k. The orthogonal projection of v\mathbf{v} onto WW is

proj⁡W(v)=∑i⟨v,ui⟩⟨ui,ui⟩ui(=∑⟨v,qi⟩qi for an orthonormal basis)\operatorname{proj}_W(\mathbf{v}) = \sum_i \frac{\langle \mathbf{v}, \mathbf{u}_i \rangle}{\langle \mathbf{u}_i, \mathbf{u}_i \rangle} \mathbf{u}_i \qquad \left(= \sum \langle \mathbf{v}, \mathbf{q}_i \rangle \mathbf{q}_i \text{ for an orthonormal basis}\right)

The key property: the remainder r=v−proj⁡W(v)\mathbf{r} = \mathbf{v} - \operatorname{proj}_W(\mathbf{v}) is orthogonal to WW. Check it against each uj\mathbf{u}_j: ⟨r,uj⟩=⟨v,uj⟩−⟨v,uj⟩⟨uj,uj⟩⟨uj,uj⟩=0\langle \mathbf{r}, \mathbf{u}_j \rangle = \langle \mathbf{v}, \mathbf{u}_j \rangle - \frac{\langle \mathbf{v}, \mathbf{u}_j \rangle}{\langle \mathbf{u}_j, \mathbf{u}_j \rangle}\langle \mathbf{u}_j, \mathbf{u}_j \rangle = 0. So every v\mathbf{v} splits uniquely as

v=w+r,w∈W,r∈W⊥\mathbf{v} = \mathbf{w} + \mathbf{r}, \qquad \mathbf{w} \in W, \qquad \mathbf{r} \in W^\perp

Best approximation theorem. proj⁡W(v)\operatorname{proj}_W(\mathbf{v}) is the point of WW closest to v\mathbf{v}. For any other w′\mathbf{w}' in WW, v−w′=r+(w−w′)\mathbf{v} - \mathbf{w}' = \mathbf{r} + (\mathbf{w} - \mathbf{w}') with r⊥(w−w′)\mathbf{r} \perp (\mathbf{w} - \mathbf{w}'), so by Pythagoras ∥v−w′∥2=∥r∥2+∥w−w′∥2≥∥r∥2\lVert \mathbf{v} - \mathbf{w}' \rVert^2 = \lVert \mathbf{r} \rVert^2 + \lVert \mathbf{w} - \mathbf{w}' \rVert^2 \ge \lVert \mathbf{r} \rVert^2.

If you only have a non-orthogonal basis, put it in the columns of a matrix AA; then the projection matrix is P=A(ATA)−1ATP = A(A^TA)^{-1}A^T. With orthonormal columns QQ it simplifies to P=QQTP = QQ^T. Projection matrices satisfy P2=PP^2 = P (projecting twice changes nothing) and PT=PP^T = P.

python · try it yourself
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 [−π,π][-\pi, \pi] with ⟨f,g⟩=∫−ππf(x)g(x) dx\langle f, g \rangle = \int_{-\pi}^{\pi} f(x) g(x) \, dx, the functions

1,cos⁡x,sin⁡x,cos⁡2x,sin⁡2x,cos⁡3x,sin⁡3x,…1, \cos x, \sin x, \cos 2x, \sin 2x, \cos 3x, \sin 3x, \ldots

are mutually orthogonal, with ∥1∥2=2π\lVert 1 \rVert^2 = 2\pi and ∥cos⁡nx∥2=∥sin⁡nx∥2=π\lVert \cos nx \rVert^2 = \lVert \sin nx \rVert^2 = \pi for n≥1n \ge 1. (For example ∫sin⁡mxsin⁡nx dx=0\int \sin mx \sin nx \, dx = 0 for m≠nm \ne n, by the product-to-sum identity.)

Projecting ff onto the span of the first terms is therefore just the projection formula above. The coefficients are

a0=⟨f,1⟩⟨1,1⟩=12π∫f dxan=⟨f,cos⁡nx⟩π=1π∫f(x)cos⁡nx dxbn=⟨f,sin⁡nx⟩π=1π∫f(x)sin⁡nx dxSn(x)=a0+∑k=1n(akcos⁡kx+bksin⁡kx)\begin{aligned} a_0 &= \frac{\langle f, 1 \rangle}{\langle 1, 1 \rangle} = \frac{1}{2\pi} \int f \, dx \\ a_n &= \frac{\langle f, \cos nx \rangle}{\pi} = \frac{1}{\pi} \int f(x) \cos nx \, dx \\ b_n &= \frac{\langle f, \sin nx \rangle}{\pi} = \frac{1}{\pi} \int f(x) \sin nx \, dx \\ S_n(x) &= a_0 + \sum_{k=1}^{n} (a_k \cos kx + b_k \sin kx) \end{aligned}

SnS_n is the Fourier partial sum: the best approximation of ff, in the L2L^2 sense, by a trigonometric polynomial of degree nn.

A square wave

Take f(x)=−1f(x) = -1 for x<0x < 0 and +1+1 for x>0x > 0. ff is odd, so every ana_n is 0. For bnb_n, 1π∫−ππfsin⁡nx dx=2π∫0πsin⁡nx dx=2(1−cos⁡nπ)nπ\frac{1}{\pi}\int_{-\pi}^{\pi} f \sin nx \, dx = \frac{2}{\pi}\int_0^{\pi} \sin nx \, dx = \frac{2(1 - \cos n\pi)}{n\pi}, which is 4nπ\frac{4}{n\pi} for odd nn and 0 for even nn.

python · try it yourself
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: ∥f−Sn∥2=∥f∥2−∥Sn∥2=2π−π∑bk2\lVert f - S_n \rVert^2 = \lVert f \rVert^2 - \lVert S_n \rVert^2 = 2\pi - \pi\sum b_k^2. 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 L2L^2 error still goes to 0.

Note: Whether SnS_n converges to ff point by point is a subtle analysis question; convergence in the L2L^2 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.fft computes all inner products with sampled complex exponentials in O(nlog⁡n)O(n \log n) time (SciPy FFT).
  • Machine learning. Linear regression's prediction y^=Pb\hat{\mathbf{y}} = P\mathbf{b} 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 v=(2,3)\mathbf{v} = (2, 3) onto the line spanned by u=(1,1)\mathbf{u} = (1, 1), and give the remainder.

Answer

proj⁡=(2.5,2.5)\operatorname{proj} = (2.5, 2.5), remainder=(−0.5,0.5)\text{remainder} = (-0.5, 0.5).

Solution
⟨v,u⟩⟨u,u⟩=52proj⁡=52(1,1)=(2.5,2.5)r=(2,3)−(2.5,2.5)=(−0.5,0.5),r⋅u=0 ✓\begin{aligned} &\frac{\langle \mathbf{v}, \mathbf{u} \rangle}{\langle \mathbf{u}, \mathbf{u} \rangle} = \frac{5}{2} \\ \operatorname{proj} &= \frac{5}{2}(1, 1) = (2.5, 2.5) \\ \mathbf{r} &= (2, 3) - (2.5, 2.5) = (-0.5, 0.5), \qquad \mathbf{r} \cdot \mathbf{u} = 0 \ \checkmark \end{aligned}

Exercise 2: Onto a plane with an orthogonal basis

Project v=(1,2,3)\mathbf{v} = (1, 2, 3) onto W=span⁡{(1,0,1),(1,0,−1)}W = \operatorname{span}\{(1, 0, 1), (1, 0, -1)\}.

Answer

(1,0,3)(1, 0, 3).

Solution

The basis is orthogonal. Coefficients: 42=2\frac{4}{2} = 2 and −22=−1\frac{-2}{2} = -1. proj⁡=2(1,0,1)−(1,0,−1)=(1,0,3)\operatorname{proj} = 2(1, 0, 1) - (1, 0, -1) = (1, 0, 3). WW is the xzxz-plane, so the answer simply drops the yy-coordinate.

Exercise 3: A projection matrix

Find PP for the line spanned by u=(1,2,2)\mathbf{u} = (1, 2, 2), and check P2=PP^2 = P.

Answer

P=uuT/9=19[122244244]P = \mathbf{u}\mathbf{u}^T/9 = \frac{1}{9}\begin{bmatrix} 1 & 2 & 2 \\ 2 & 4 & 4 \\ 2 & 4 & 4 \end{bmatrix}.

Solution

For one vector, A(ATA)−1AT=uuTuTuA(A^TA)^{-1}A^T = \frac{\mathbf{u}\mathbf{u}^T}{\mathbf{u}^T\mathbf{u}} and uTu=9\mathbf{u}^T\mathbf{u} = 9. P2=u(uTu)uT81=9uuT81=PP^2 = \frac{\mathbf{u}(\mathbf{u}^T\mathbf{u})\mathbf{u}^T}{81} = \frac{9\mathbf{u}\mathbf{u}^T}{81} = P.

python · try it yourself
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 f(x)=xf(x) = x (code)

On [−π,π][-\pi, \pi], find bnb_n for f(x)=xf(x) = x and the first three values.

Answer

bn=2(−1)n+1nb_n = \frac{2(-1)^{n+1}}{n}: b1=2b_1 = 2, b2=−1b_2 = -1, b3=23b_3 = \frac{2}{3}.

Solution

ff is odd, so an=0a_n = 0. Integration by parts gives ∫−ππxsin⁡nx dx=−2πcos⁡(nπ)n=2π(−1)n+1n\int_{-\pi}^{\pi} x \sin nx \, dx = -\frac{2\pi \cos(n\pi)}{n} = \frac{2\pi(-1)^{n+1}}{n}; divide by π\pi.

python · try it yourself
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 v−proj⁡W(v)\mathbf{v} - \operatorname{proj}_W(\mathbf{v})?

  1. It lies in WW
  2. It is orthogonal to every vector of WW
  3. It is always zero
  4. It has the same length as v\mathbf{v}

Which identity does every orthogonal projection matrix satisfy?

  1. P2=IP^2 = I
  2. P−1=PTP^{-1} = P^T
  3. P2=PP^2 = P and PT=PP^T = P
  4. det⁡P=1\det P = 1

The Fourier coefficient bnb_n is computed as ⟨f,sin⁡nx⟩/π\langle f, \sin nx \rangle / \pi because…

  1. the sines are orthogonal and ∥sin⁡nx∥2=π\lVert \sin nx \rVert^2 = \pi on [−π,π][-\pi, \pi]
  2. π\pi is the period
  3. ff is always odd
  4. it is a convention with no reason