Skip to content

Wavelet Explorer: CWT Scalogram and Haar DWT

Copy this iframe to your website:

1
<iframe src="https://dmccreary.github.io/signal-processing/sims/wavelet-explorer/main.html" width="100%" height="594px" scrolling="no"></iframe>

Run MicroSim in Fullscreen

Description

This MicroSim shows two ways to analyze a signal at several resolutions. Every signal has 256 samples at \(f_s = 256\) Hz, so it lasts 1 s. Choose it from the second list: two tones (8 Hz + 32 Hz), a linear chirp whose frequency rises as \(f(t) = 4 + 56t\) Hz, an 8 Hz tone with a spike of height 3 at \(t = 0.625\) s, a step at \(t = 0.41\) s, or a 4 Hz sine with Gaussian noise (\(\sigma = 0.3\)).

CWT scalogram mode computes

\[W(a,b) = \frac{1}{\sqrt{a}} \sum_n x[n]\,\psi\!\left(\frac{n-b}{a}\right)\]

at 32 log-spaced scales \(a\) (in samples) for the Morlet \(\psi(t) = e^{-t^2/2}\cos 5t\), the Mexican hat \(\psi(t) = (1-t^2)e^{-t^2/2}\) or the Haar wavelet. The color map shows \(|W|\) with time on the x axis. The y axis is labeled with the pseudo-frequency \(f = f_c f_s / a\) on the left and the scale \(a\) on the right, where \(f_c\) is the frequency at which the wavelet's spectrum peaks (0.796, 0.225 and 0.742). The Scale and Position sliders, or a click on the scalogram, choose \((a, b)\): the gold curve on the signal is the dilated wavelet (peak scaled to fit), and the read-outs give \(W(a,b)\) and the largest \(|W|\) in that row. dB color scale maps the top 40 dB of \(|W|\), which reveals weak features. The signal is mirror-extended at both ends.

Haar DWT mode splits the signal into detail coefficients \(d_1 \ldots d_L\) and an approximation \(a_L\) (\(L\) = 1 to 5) with \(a[k] = (x[2k] + x[2k+1])/\sqrt{2}\) and \(d[k] = (x[2k] - x[2k+1])/\sqrt{2}\). Each bar is as wide as the samples its coefficient covers. Details with \(|d| \le T\) are set to zero (gray bars), and the maroon curve is the reconstruction, with its mean squared error (MSE).

This MicroSim supports Chapter 8: Advanced Transforms.

Lesson Plan

Learning Objectives

After using this MicroSim, students will be able to:

  1. Explain why scale is inversely related to frequency, and convert between scale and pseudo-frequency with \(f = f_c f_s / a\)
  2. Interpret a scalogram to locate a chirp's changing frequency, a tone's band and a transient's time of occurrence
  3. Compare the time and frequency resolution of the Morlet, Mexican hat and Haar wavelets
  4. Calculate Haar approximation and detail coefficients, and the number of coefficients at each level
  5. Analyze how the number of levels and the hard threshold trade noise removal against signal distortion

Target Audience

Undergraduate students in a signals and systems or digital signal processing course (junior or senior engineering, physics or computer science). Plan 25 minutes for the four activities.

Prerequisites

  • Sinusoids, frequency and sampling (\(f_s\), samples per cycle)
  • Convolution and correlation as sliding sums
  • The Fourier transform and the idea of a spectrum (Chapters 6 and 7)

Activities

Activity 1: Scale and pseudo-frequency (5 min). Start with the defaults: chirp, Morlet, scale \(a = 6.49\) samples, position \(b = 0.500\) s. The read-out converts the scale to \(f = 0.796 \times 256 / 6.49 = 31.4\) Hz, and the largest \(|W|\) in that row is 3.13 at \(t = 0.492\) s; the chirp reaches 31.4 Hz at \((31.4 - 4)/56 = 0.489\) s. Move the scale slider right to \(a = 12.7\) samples: the row is now 16.0 Hz and its maximum moves to 0.219 s (predicted 0.215 s). Larger scale, lower frequency, earlier in the chirp.

Activity 2: Time and frequency resolution (7 min). Choose two tones. With the Morlet wavelet the 8 Hz and 32 Hz tones form two separate bands with a dark gap between them. At \(a = 24.8\) samples (8.2 Hz) the row maximum is 6.20; at \(a = 6.49\) samples (31.4 Hz) it is 3.18. The tones have equal amplitude, but \(|W|\) grows as \(\sqrt{a}\) for a sinusoid, and \(\sqrt{24.8/6.49} = 1.96\). Switch to the Mexican hat and then Haar: the bands spread out and the gap fills in (poorer frequency resolution), and with the Mexican hat the brightest part of the low band sits near 7 Hz, a reminder that pseudo-frequency is approximate. Now choose tone + spike, the Morlet wavelet and dB color scale. The spike draws a cone that is narrow at small scales. Move the scale slider fully left (\(a = 2.12\) samples, 96.0 Hz): the row maximum is 2.06 at \(t = 0.625\) s, and at \(b = 0.645\) s (5 samples later) \(W\) is only 0.09. At \(a = 6.49\) samples the spike gives \(W = 1.18\) at 0.625 s, smaller by \(\sqrt{6.49/2.12}\), while a tone's response grows with scale.

Activity 3: Sparsity and perfect reconstruction (6 min). Choose Haar DWT, step edge, \(L = 5\), \(T = 0\). The read-out shows 10 nonzero coefficients out of 256 (5 approximation + 5 detail) and MSE = 0 (perfect reconstruction): each detail row holds a single bar at the edge (values −0.707, −0.500, −0.354, −1.75 and −1.59 for \(d_1\) to \(d_5\)). With \(L = 1\) the count is 77, and with \(L = 3\) it is 22. Set \(T = 0.40\): only \(d_3\) is removed, 9 coefficients remain and MSE = 0.0005, which equals \(0.354^2/256\).

Activity 4: Denoising by hard thresholding (7 min). Choose noisy sine and \(L = 3\). At \(T = 0\) the error against the clean sine equals that of the noisy input, 0.0821. Raise the threshold: the error falls to 0.0564 at \(T = 0.50\) and 0.0385 at \(T = 1.00\) (the universal threshold \(\sigma\sqrt{2 \ln N} = 0.3\sqrt{2 \ln 256} = 1.00\)). From \(T = 1.05\) every detail is removed and the 32 approximation coefficients alone give 0.0350. Now set \(L = 5\): the error is lowest near \(T = 0.65\) (0.0462) and climbs to 0.0965 at \(T = 2.00\), worse than doing nothing. Over all settings the lowest error is 0.0265, at \(L = 2\) with \(T \ge 1.05\).

Assessment

  1. For the Morlet wavelet (\(f_c = 0.796\)) and \(f_s = 256\) Hz, what scale corresponds to 20 Hz? Answer: \(a = f_c f_s / f = 0.796 \times 256 / 20 = 10.2\) samples.
  2. Two tones have equal amplitudes, at 8 Hz and 32 Hz. Why does the 8 Hz band of the Morlet scalogram reach about twice the \(|W|\) of the 32 Hz band? Answer: with the \(1/\sqrt{a}\) normalization a sinusoid gives \(|W| \propto \sqrt{a}\); the 8 Hz tone matches a scale 4 times larger, so its response is \(\sqrt{4} = 2\) times larger (the sim shows 6.20 and 3.18).
  3. A 256-sample signal is decomposed with \(L = 3\). How many coefficients are in \(d_1\), \(d_2\), \(d_3\) and \(a_3\)? Answer: 128, 64, 32 and 32, which add up to 256.
  4. For the step, \(T = 0.75\) removes \(d_1\), \(d_2\) and \(d_3\) (−0.707, −0.500 and −0.354). Predict the MSE without running the reconstruction. Answer: the Haar DWT is orthonormal, so the squared error equals the energy of the removed coefficients: \((0.5 + 0.25 + 0.125)/256 = 0.0034\), which the sim shows.
  5. At \(L = 5\) and \(T = 2.00\) the denoised sine has a larger error (0.0965) than the noisy input (0.0821). Why? Answer: the large \(d_4\) and \(d_5\) coefficients carry the 4 Hz sine itself; removing them leaves only 8 block averages, each 32 samples long, which is half a period of the sine.

Common Misconceptions

"Each bright stripe along a band is a separate event." All three wavelets here are real, so \(W(a,b)\) follows the oscillation of the signal and \(|W|\) rises and falls twice per cycle. With two tones the 8 Hz band shows 16 bright patches per second although the tone is steady. Drag the position slider along that row (\(a = 24.8\) samples): the read-out \(W(a,b)\) changes sign 16 times in the 1 s signal, once per half cycle.

"A larger threshold always removes more noise and gives a cleaner signal." Hard thresholding also removes signal. In Activity 4 the error at \(L = 5\) first falls from 0.0821 to 0.0462 and then rises to 0.0965 as \(T\) grows, because the threshold starts deleting the coefficients that describe the sine.

References

  1. Wavelet - Wikipedia - mother wavelets, scaling and translation, and multiresolution analysis
  2. Continuous wavelet transform - Wikipedia - definition of \(W(a,b)\), the scalogram and the scale to frequency relation
  3. Discrete wavelet transform - Wikipedia - the filter-bank cascade of approximation and detail coefficients
  4. Haar wavelet - Wikipedia - the Haar function and the orthonormal Haar transform used in DWT mode
  5. Morlet wavelet - Wikipedia - the Gaussian-windowed cosine with good frequency localization
  6. Ricker wavelet - Wikipedia - the Mexican hat wavelet, the negative second derivative of a Gaussian
  7. The Scientist and Engineer's Guide to Digital Signal Processing - Steven W. Smith - free online textbook covering convolution, filter banks and multirate processing