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.
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.
The first few pieces of each family, each scaled to fit its own box. Read across a row to see how a family grows more detailed; read down a column to compare families at the same step. The first piece (yellow) is the same in every family: a constant, standing for the signal's average level.
Gallery
Smooth or blocky, global or local, orthogonal or not — it shows in the shapes.
What to look for
Fourier and Walsh both wiggle faster and faster across the whole interval — one smoothly, one in steps. Haar's pulses instead get narrower and move around: that is what "local" means. Legendre and plain powers of x look alike at first, but the powers bunch together (x⁴ and x⁶ are nearly the same shape), which is the visual signature of "not orthogonal". The ReLU row is a set of bent lines, each switching on at a different point.
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).
| Family | Pieces look like | Orthogonal | Reach | Where it is used | Related lab |
|---|---|---|---|---|---|
| Fourier | sines and cosines | yes | global | audio, radio, spectrograms; JPEG uses its cousin, the cosine transform | DFT, Fourier Series |
| Walsh | ±1 square waves | yes | global | low-cost codecs, CDMA spreading codes, the quantum Hadamard gate | DFT basis explorer |
| Haar wavelets | short up/down pulses | yes | local | the simplest wavelet; smoother wavelets power JPEG 2000 and signal denoising | — |
| Legendre | orthogonal polynomials | yes | global | numerical integration (Gauss–Legendre), spectral equation solvers | Taylor Series |
| Powers of x | 1, x, x², … | no | global | Taylor series, quick curve fits | Taylor Series |
| ReLU hinges | bent lines | no | local | every ReLU neural network; linear splines | Activation Functions |
| PCA | the data's main directions | yes | global | compression, embeddings, visualization | Latent Space, Matrices |
| Trained network | hinges at learned positions | no | local | deep learning | Gradient 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
Answer every question before checking.
