Wavelet Problems Using MATLAB
Wavelets are powerful tools for signal and image processing. They provide a way to analyze different frequency components of a signal at different resolutions. MATLAB’s Wavelet Toolbox offers various functions to perform wavelet analysis efficiently.
Introduction to Wavelets, and Basic Wavelet Operations
Wavelets are mathematical functions that divide a signal into different components and analyze each with a resolution that matches its scale. A signal can be represented as a sum of wavelet functions with different time shifts and scales.
To begin working with wavelets, you can use the MATLAB Wavelet Toolbox. Here’s how to perform some basic operations:
Wavelet Decomposition
Wavelet decomposition divides a signal into approximation (low-frequency) and detail (high-frequency) components.
% Wavelet Decomposition
signal = [4 6 10 12 8 6 4 2];
[ca, cd] = dwt(signal, 'db1'); % Using Daubechies wavelet
disp('Approximation Coefficients:');
disp(ca);
disp('Detail Coefficients:');
disp(cd);
The dwt
function performs single-level decomposition of the input signal using the specified wavelet.
Wavelet Reconstruction
Wavelet reconstruction combines the approximation and detail components to reconstruct the original signal.
% Wavelet Reconstruction
reconstructed_signal = idwt(ca, cd, 'db1');
disp('Reconstructed Signal:');
disp(reconstructed_signal);
Continuous Wavelet Transform (CWT)
The Continuous Wavelet Transform provides a detailed time-frequency representation of a signal.
% Continuous Wavelet Transform
time = 0:0.01:1;
signal = sin(2*pi*10*time) + sin(2*pi*20*time);
cwt(signal, 'amor', 0.01); % Using the Morlet wavelet
Wavelet Packet Decomposition
Wavelet Packet Decomposition allows finer decomposition of both approximation and detail components.
% Wavelet Packet Decomposition
wp_tree = wpdec(signal, 3, 'sym4'); % 3-level decomposition using Symlet wavelet
plot(wp_tree);
Examples of Wavelet Applications in Matlab
Example 1: Denoising a Signal
Wavelets are commonly used to remove noise from signals while preserving important features.
% Denoising a signal
signal = sin(2*pi*(1:100)/20) + 0.5*randn(1, 100); % Noisy signal
[c, l] = wavedec(signal, 4, 'db1'); % Wavelet decomposition
threshold = 0.2;
c_denoised = wthresh(c, 's', threshold); % Soft thresholding
signal_denoised = waverec(c_denoised, l, 'db1'); % Reconstruct signal
plot(signal); hold on;
plot(signal_denoised, 'r');
legend('Original Signal', 'Denoised Signal');
Example 2: Image Compression Using Wavelets
Wavelets can be used to compress images by selectively removing less significant wavelet coefficients.
% Image Compression
img = imread('example.jpg');
[c, s] = wavedec2(img, 2, 'bior4.4'); % 2-level decomposition
threshold = 20;
c_compressed = wthresh(c, 'h', threshold); % Hard thresholding
img_compressed = waverec2(c_compressed, s, 'bior4.4');
imshow(uint8(img_compressed));
Example 3: Multi-Level Wavelet Decomposition and Reconstruction
Perform multi-level wavelet decomposition and reconstruct the signal to verify the accuracy of decomposition.
% Multi-Level Wavelet Decomposition and Reconstruction
signal = sin(2*pi*(1:100)/20) + 0.3*randn(1, 100); % Noisy signal
levels = 3; % Number of decomposition levels
[c, l] = wavedec(signal, levels, 'db4'); % Decompose signal
reconstructed_signal = waverec(c, l, 'db4'); % Reconstruct signal
subplot(2, 1, 1);
plot(signal); title('Original Signal');
subplot(2, 1, 2);
plot(reconstructed_signal); title('Reconstructed Signal');
Example 4: Detecting Signal Discontinuities Using Wavelets
Wavelets can be used to detect sudden changes or discontinuities in signals.
% Detecting Discontinuities
signal = [zeros(1, 50), ones(1, 50), 2*ones(1, 50)]; % Piecewise constant signal
[c, l] = wavedec(signal, 1, 'haar'); % Perform single-level decomposition
detail_coeffs = detcoef(c, l, 1); % Extract detail coefficients
plot(detail_coeffs); title('Detail Coefficients');
Example 5: Time-Frequency Analysis of Non-Stationary Signals
Analyze the time-frequency characteristics of a non-stationary signal using the Continuous Wavelet Transform (CWT).
% Time-Frequency Analysis
time = 0:0.001:1;
signal = sin(2*pi*50*time) + sin(2*pi*120*time).*(time > 0.5);
cwt(signal, 'amor', 0.001); % Using Morlet wavelet
Example 6: Wavelet-Based Image Edge Detection
Wavelets are effective for detecting edges in images by isolating high-frequency components.
% Wavelet-Based Edge Detection
img = imread('cameraman.tif');
[cA, cH, cV, cD] = dwt2(img, 'db1'); % Single-level 2D wavelet decomposition
edges = abs(cH) + abs(cV) + abs(cD); % Combine horizontal, vertical, and diagonal details
imshow(uint8(edges)); title('Detected Edges');
Example 7: Compression of Audio Signals Using Wavelets
Reduce the size of an audio signal by selectively removing less significant wavelet coefficients.
% Audio Signal Compression
[audio, fs] = audioread('example.wav'); % Read audio signal
audio = audio(:, 1); % Use single channel
[c, l] = wavedec(audio, 5, 'db4'); % 5-level wavelet decomposition
threshold = 0.02;
c_compressed = wthresh(c, 'h', threshold); % Apply hard thresholding
audio_compressed = waverec(c_compressed, l, 'db4'); % Reconstruct compressed audio
sound(audio_compressed, fs); % Play compressed audio
Example 8: Extracting Features from Signals Using Wavelet Packet Decomposition
Wavelet packet decomposition provides a detailed time-frequency representation for feature extraction.
% Feature Extraction Using Wavelet Packet Decomposition
signal = sin(2*pi*10*(1:128)/128) + sin(2*pi*40*(1:128)/128);
wp_tree = wpdec(signal, 3, 'sym4'); % 3-level wavelet packet decomposition
node_coeffs = wpcoef(wp_tree, [3, 0]); % Extract coefficients for a specific node
disp('Node Coefficients:');
disp(node_coeffs);
Example 9: Wavelet-Based Noise Removal in Images
Use wavelets to remove noise from images while preserving details.
% Image Denoising
img = imread('noisy_image.jpg');
[c, s] = wavedec2(img, 2, 'db2'); % 2-level wavelet decomposition
threshold = 15;
c_denoised = wthresh(c, 's', threshold); % Apply soft thresholding
img_denoised = waverec2(c_denoised, s, 'db2'); % Reconstruct denoised image
imshow(uint8(img_denoised)); title('Denoised Image');
Useful MATLAB Functions for Wavelets
Practice Questions
Test Yourself
1. Perform single-level wavelet decomposition on a signal and reconstruct it.
2. Create a 2D wavelet decomposition of an image and plot the coefficients.
3. Use wavelets to denoise a signal with Gaussian noise.
4. Perform a 5-level wavelet decomposition on a signal and plot the approximation and detail coefficients for each level.
5. Use wavelet packet decomposition to analyze a signal’s energy distribution across different frequency bands.
6. Denoise an audio signal using wavelets and compare the original and denoised signals.
7. Implement a wavelet-based method for compressing and reconstructing a grayscale image.