Haar Wavelet Transform Simulator — Multiresolution Analysis and Compression
Apply the Haar wavelet recursively to a 1D signal and visualize the approximation and detail coefficient pyramid. Learn compression ratio and reconstruction error from thresholding in real time.
Parameters
Samples N = 2^k
Decomposition level L
Detail-coefficient threshold
Signal type (0=sine, 1=step, 2=chirp)
If N is not a power of two, it is rounded to the nearest power of two with a warning.
Results
—
Samples N = 2^k
—
Decomposition level L
—
Non-zero / total coefficients
—
Reconstruction MSE
Coefficient Pyramid and Reconstruction
Blue = original x[n] / green = final approximation a^(L) / red = detail coefficients d^(L) at each level / purple = reconstruction x_rec after thresholding
Theory & Key Formulas
The Haar wavelet is the simplest orthogonal wavelet, defined by the average and difference of adjacent pairs of samples.
One-level decomposition: approximation a and detail d, normalized by $\sqrt{2}$ for orthogonality:
Compression: zeroing detail coefficients below the threshold reduces the number of non-zero coefficients with only a small increase in MSE. Since Haar is an orthogonal basis, energy is preserved and the MSE in coefficient space equals the MSE in signal space.
What is the Haar Wavelet Transform Simulator
🙋
How is the wavelet transform different from the Fourier transform? Textbooks always confuse me on this.
🎓
Roughly, the Fourier transform tells you "what frequencies are present" but not "when each frequency appears". A wavelet sees time and frequency at the same time. The Haar wavelet is the simplest: take the sum and the difference of two adjacent samples, divide each by sqrt(2), and that is it. In the simulator above, switch the signal type to "2 = chirp" and you will see that the level-1 detail coefficients grow on the right. That means "the high frequency arrived in the second half" — something Fourier cannot show directly.
🙋
I see. What happens when I raise the "decomposition level"? At L=6 the final approximation (green) is almost flat.
🎓
Each extra level halves the approximation length. With N=256 and L=6 the final approximation has only 256/64=4 samples. The coarser it gets, the more it only carries the DC-like overall shape of the signal. At the same time, the topmost detail d^(1) has 128 samples and captures the finest high-frequency variation. That is why this is called multiresolution analysis (MRA): the signal is split hierarchically into "coarse shape + fine change + finer change + ...".
🙋
The compression part is fascinating. With threshold 0.1 the reconstruction MSE stays tiny. What is happening?
🎓
Good question. Natural signals split into "a few large coefficients" and "many small coefficients". Setting the small ones to zero loses little information — this is the idea of sparsity. Because Haar is an orthogonal basis, error in coefficient space equals error in signal space (Parseval's theorem). Move the threshold slider from 0.0 to 0.3: the number of non-zero coefficients drops sharply while the MSE rises only a little. That is exactly why JPEG 2000 compresses with wavelets.
🙋
With signal type 1, the step signal, raising the threshold makes the reconstruction (purple) look jagged near the jump. What is that?
🎓
That is Haar's weakness. The Haar basis itself is a step function, so it represents discontinuities well, but the reconstructed waveform tends to look staircase-like. On smooth curves like a pure sine, Haar leaves residual jaggedness. To fix this, longer wavelets such as Daubechies (db4 or db9/7) were developed. Compression efficiency and image quality improve far beyond Haar, but at the cost of more computation and more delicate filter design — a classic engineering trade-off.
Frequently Asked Questions
JPEG 2000 uses the Daubechies biorthogonal db9/7 for lossy compression and db5/3 for lossless. Haar is poor at approximating continuous functions and leaves visible block-like reconstruction errors in flat regions, while db9/7 has many vanishing moments and represents smooth signals with very few coefficients. At the same compression ratio, db9/7 is dramatically better, so the image standards barely use Haar. Haar's niche is education and difference coding on extremely constrained embedded devices.
Use Haar for edge detection, analysis of step functions, and embedded uses where speed is paramount. For image compression, audio analysis and feature extraction on smooth signals, prefer Daubechies (db2, db4, db9/7), Coiflet or Symlet. A useful rule of thumb is "longer filters for smoother signals, shorter filters for signals with many edges". Compare signal type 1 (step) and 0 (sine) in this tool to feel Haar's strengths and weaknesses in one session.
For 2D images, the "2D discrete wavelet transform" applies separable filters along rows and then columns. Each level produces four sub-bands — LL (approximation), LH (horizontal detail), HL (vertical detail), HH (diagonal detail) — and the transform is repeated recursively on LL. Because most detail sub-band coefficients are small, combining quantization and arithmetic coding achieves high compression ratios. JPEG 2000 and the FBI fingerprint database (WSQ compression) are representative applications.
The absolute value of a detail coefficient corresponds to the local change in the signal at that position, so its peaks are edges. Combining detail coefficients from several levels distinguishes noise (appearing only at the finest scale) from genuine edges (consistent across scales). Mallat and Zhong's multiscale edge representation (1992) is the classic example. It is used in medical imaging, seismic analysis and ECG processing where robustness beyond Sobel or Canny is needed.
Real-World Applications
Image compression (JPEG 2000): The 2D discrete wavelet transform is the core of the next-generation image standard that replaces JPEG's DCT. It avoids block artifacts in principle and produces smoother, more natural images at the same file size. It is the standard choice in fields with demanding image quality, such as medical archives, digital cinema distribution (DCP) and satellite image transmission.
Compression and analysis of audio and music: Compared to the MDCT used by MP3, wavelets handle transient sounds (drum attacks, plosives) much better. They represent short sharp changes — the onset of a piano note, the consonants of speech — accurately with only a few coefficients. The same property helps with noise reduction, source separation and instrument identification.
Feature extraction in seismology, EEG and ECG: Non-stationary biological signals and seismic traces fit poorly with Fourier analysis that assumes stationarity, and wavelets become the first choice. Haar's difference coefficients are widely used for P-wave onset detection in seismology, R-wave detection in ECG, and pre-seizure detection in EEG. Examining detail coefficients across scales helps separate noise from true features.
Machine learning features and dimensionality reduction: Before the CNN era, wavelet coefficients as input features delivered strong performance in image and text recognition. They are still useful today on resource-constrained embedded systems, where wavelet features paired with a lightweight classifier provide an efficient alternative. Examples include preprocessing in fingerprint and face recognition pipelines.
Common Misconceptions and Cautions
The most common misconception is to assume that wavelets are a replacement for the Fourier transform and are always better. In practice the tools are complementary: Fourier wins overwhelmingly when you need precise spectral information on stationary signals (purity of a sine wave, identification of resonance frequencies). Wavelets shine when you need to see "which frequency appeared when", that is, for transient or locally featured signals. Pick signal type 2 (chirp) in this tool and watch how the detail coefficients grow toward the right — that time information cannot be captured by Fourier alone.
Another common mistake is to believe that raising the decomposition level always improves resolution. The final approximation length is $N/2^L$, so with N=256 and L=8 only one sample remains and you essentially see only the signal average. This tool caps L at 6 as a practical guideline: beyond this you only get coarser, not better. In practice, choose L from the signal length and the frequency band of interest. For instance, 1024 samples at 1 kHz often needs only L=5.
Finally, do not forget that aggressive thresholding always trades quality for compression. The MSE this tool reports is a numerical mean-squared error and is not the same thing as perceived image or sound quality. Real codecs such as JPEG 2000 and MP3 use non-uniform quantization based on psycho-visual and psycho-acoustic models, removing high-frequency content the human eye or ear is least likely to notice. Switching from a hard threshold to a "soft threshold" (subtracting the threshold from values that exceed it) yields denoising in the sense of Donoho's WaveShrink. Choosing the right thresholding strategy for the application is the real engineering of wavelet methods.