Don Stephenson Don Stephenson

Basis Functions Lab

One idea underneath the other labs

Fourier series, the DFT, Walsh–Hadamard, Taylor series, even a neural network: each one rebuilds a signal as a weighted sum of simple pieces. Only the pieces change. This lab puts six families of pieces side by side on the same signal, so you can see what each family is good at, why some behave better than others, and how they connect to the rest of the site.

Choose a signal — or drag on the plot to draw your own — then pick a family and how many terms it may use. Each family always uses its k simplest pieces (lowest frequency, lowest degree, widest pulse first), so the leaderboard below is a fair race: same signal, same number of terms, smallest error wins.

Fit explorer

Yellow is the signal, teal is the fit, pink is what the fit misses.

Error (% of signal)
—
Orthogonal
—
Reach
—

Leaderboard · same signal, same number of terms · click a row to switch

Experiments to try

Every family wins somewhere

At k = 4, step through the signals: Fourier takes the smooth wave, Walsh and Haar the square wave, ReLU the kink, the polynomials the cubic. No basis is best at everything.

Fourier versus the ramp

A straight line is trivial for polynomials (k = 2) yet hard for Fourier, which treats the interval as repeating and sees a jump where the end meets the start.

Draw a corner

Drag a zig-zag with two or three sharp corners. ReLU hinges climb the leaderboard: a network of hinges is built for corners, and smooth families have to round them off.

Three questions sort every basis: global or local (does each piece span the whole signal or one patch?), fixed or learned (chosen by a mathematician or fitted to data?), and orthogonal or not (filled dots are orthogonal).

GLOBAL — each piece spans everything LOCAL — each piece covers a patch FIXED LEARNED learned from data ↑ chosen in advance ↓ Gram–Schmidt training moves the knots ±1 instead of smooth Fourier Legendre Powers of x Walsh Haar wavelets ReLU hinges Trained network hinges where the data needs them PCA the data's own main directions
FamilyPieces look likeOrthogonalReachWhere it is usedRelated lab
Fouriersines and cosinesyesglobalaudio, radio, spectrograms; JPEG uses its cousin, the cosine transformDFT, Fourier Series
Walsh±1 square wavesyesgloballow-cost codecs, CDMA spreading codes, the quantum Hadamard gateDFT basis explorer
Haar waveletsshort up/down pulsesyeslocalthe simplest wavelet; smoother wavelets power JPEG 2000 and signal denoising—
Legendreorthogonal polynomialsyesglobalnumerical integration (Gauss–Legendre), spectral equation solversTaylor Series
Powers of x1, x, x², …noglobalTaylor series, quick curve fitsTaylor Series
ReLU hingesbent linesnolocalevery ReLU neural network; linear splinesActivation Functions
PCAthe data's main directionsyesglobalcompression, embeddings, visualizationLatent Space, Matrices
Trained networkhinges at learned positionsnolocaldeep learningGradient Descent

Choosing a basis is choosing what counts as "simple". A signal is cheap to describe in a basis whose pieces already look like it — and that one idea runs from audio codecs to neural networks.

1. One recipe

Pick pieces $\varphi_0, \varphi_1, \dots$ and approximate the signal as $$f(x) \approx \sum_{k=0}^{K-1} c_k\, \varphi_k(x)$$ For an orthogonal family the best weights are simple projections, $c_k = \langle f, \varphi_k \rangle / \langle \varphi_k, \varphi_k \rangle$ — measure how much the signal looks like each piece, independently. For any other family the weights come from solving a small least-squares problem. The lab does both with one routine: make the pieces orthogonal first (Gram–Schmidt), then project.

2. Why orthogonality matters

With orthogonal pieces each coefficient is measured on its own, so adding a term never changes the ones you already have, and energy splits cleanly between terms (Parseval). Plain powers of x are not orthogonal: on $[-1,1]$, $x^4$ and $x^6$ are nearly the same curve, so the fit has to balance large coefficients of opposite sign, every coefficient shifts when a term is added, and high-degree fits become numerically fragile. Run Gram–Schmidt on $1, x, x^2, \dots$ and out come exactly the Legendre polynomials — the same span, cleaned up. That is why Legendre and powers of x score identically on the leaderboard, but only one of them is pleasant to compute with.

3. Global versus local

A Fourier or Legendre piece spans the whole interval, so one sharp feature disturbs every coefficient and leaves ringing everywhere. Haar pulses and ReLU hinges each cover one patch, so a jump or corner costs a few terms near it and nothing elsewhere. Wavelets were invented to get both: local pieces at many widths. Their smoother descendants are why JPEG 2000 avoids the blocky artifacts of the original, cosine-based JPEG at high compression.

4. Fixed versus learned

Every family in the Fit explorer is chosen in advance. Two famous bases are learned instead. PCA finds the orthogonal directions along which a dataset varies most — the eigenvectors of its covariance matrix — and keeping the top few is the best possible linear compression of that data (see Latent Space). A neural network with ReLU activations is a sum of hinges whose positions and slopes are learned by gradient descent. The ReLU family here is that network with its hinges frozen at even spacing; training is what lets a real network put its hinges exactly where the signal bends.

5. Where you have already met them

  • Fourier Series and the DFT: the Fourier family, built up by hand and measured from samples.
  • DFT basis explorer: Fourier against Walsh, keeping the largest coefficients instead of the simplest — the idea behind compression.
  • Taylor Series: powers of x, centered on one point.
  • Matrices: a basis change is a matrix; orthogonal bases are rotations.
  • Activation Functions: the shapes a network's pieces can take.

Self check

1. Adding a term to a Fourier or Legendre fit leaves the earlier coefficients unchanged, but with plain powers of x they all shift. Why?

2. Which family fits the kink |x| exactly with only three terms?

3. Changing one Haar wavelet coefficient changes the rebuilt signal…

4. What sets PCA and a trained neural network apart from Fourier or Legendre?

Answer every question before checking.

DRAG ON THE PLOT TO DRAW A SIGNAL · RACE THE FAMILIES ON THE LEADERBOARD