MATLAB Code for Reconstructing the Original Signal from DWT
% Sampling frequency and time vector
Fs = 1000; % Sampling frequency (Hz)
t = 0:1/Fs:1-1/Fs; % Time vector (1 second duration)
% Frequencies of the sinusoids (in Hz)
f1 = 50; % Frequency of first sinusoid
f2 = 150; % Frequency of second sinusoid
f3 = 300; % Frequency of third sinusoid
% Create a mixture of sinusoids (Original Signal)
x = sin(2*pi*f1*t) + 0.5*sin(2*pi*f2*t) + 0.2*sin(2*pi*f3*t);
% Plot the original signal
figure;
subplot(4,1,1);
plot(t, x);
title('Original Signal: Mixture of Sinusoids');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
% Define low-pass (scaling) and high-pass (wavelet) filters
h = [1/2, 1/2]; % Low-pass filter (scaling function)
g = [1/2, -1/2]; % High-pass filter (wavelet function)
% First Level Filtering (DWT)
A1 = conv(x, h, 'valid'); % Approximation (low frequency)
D1 = conv(x, g, 'valid'); % Detail (high frequency)
% Downsample by a factor of 2
A1 = A1(1:2:end); % Downsample approximation
D1 = D1(1:2:end); % Downsample detail
% Reconstruct the original signal using Inverse DWT (IDWT)
% First, we need to upsample the approximation and detail components
A1_up = zeros(1, length(x)); % Allocate space for upsampled approximation
D1_up = zeros(1, length(x)); % Allocate space for upsampled detail
A1_up(1:2:end) = A1; % Insert zeros between A1 samples
D1_up(1:2:end) = D1; % Insert zeros between D1 samples
% Now convolve with the low-pass and high-pass filters (IDWT step)
x_reconstructed = conv(A1_up, h, 'same') + conv(D1_up, g, 'same');
% Plot the reconstructed signal
subplot(4,1,2);
plot(t, x_reconstructed);
title('Reconstructed Signal from A1 and D1');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
% Compare the original signal with the reconstructed signal
subplot(4,1,3);
plot(t, x, 'b', t, x_reconstructed, 'r--');
title('Comparison: Original vs. Reconstructed');
xlabel('Time (s)');
ylabel('Amplitude');
legend('Original', 'Reconstructed');
grid on;
% Display message
disp('Signal reconstruction using IDWT completed.');
Output