MATLAB Code
% MATLAB Code to Demonstrate Discrete Wavelet Transform (DWT) and Spectrum Visualization
% 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
x = sin(2*pi*f1*t) + 0.5*sin(2*pi*f2*t) + 0.2*sin(2*pi*f3*t);
% Plot the original signal (Mixture of Sinusoids)
figure;
subplot(5,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
% Convolve with the low-pass filter (approximation)
A1 = conv(x, h, 'valid'); % Approximation (low frequency)
% Convolve with the high-pass filter (detail)
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
% New sampling frequency after decimation (downsampling by a factor of 2)
Fs1 = Fs / 2;
% Plot the first level approximation and detail
subplot(5,1,2);
stem([A1, D1], 'filled');
title('First Level Decomposition (Approximation and Detail)');
xlabel('Index');
ylabel('Amplitude');
legend('Approximation', 'Detail');
grid on;
% Calculate and Plot Spectrum for First Level (A1 and D1) using pwelch
subplot(5,1,3);
[pxx_A1, f_A1] = pwelch(A1, [], [], [], Fs1);
[pxx_D1, f_D1] = pwelch(D1, [], [], [], Fs1);
plot(f_A1, 10*log10(pxx_A1), 'b', 'LineWidth', 1.5); hold on;
plot(f_D1, 10*log10(pxx_D1), 'r', 'LineWidth', 1.5);
title('Power Spectral Density (PSD) for First Level Approximation and Detail');
xlabel('Frequency (Hz)');
ylabel('Power (dB)');
legend('Approximation', 'Detail');
grid on;
% Second Level Filtering (Decompose approximation further)
% Convolve approximation A1 with low-pass and high-pass filters
A2 = conv(A1, h, 'valid'); % Approximation (low frequency)
D2 = conv(A1, g, 'valid'); % Detail (high frequency)
% Downsample by a factor of 2
A2 = A2(1:2:end); % Downsample approximation
D2 = D2(1:2:end); % Downsample detail
% New sampling frequency after second decimation
Fs2 = Fs1 / 2;
% Plot the second level approximation and detail
subplot(5,1,4);
stem([A2, D2], 'filled');
title('Second Level Decomposition (Approximation and Detail)');
xlabel('Index');
ylabel('Amplitude');
legend('Approximation', 'Detail');
grid on;
% Calculate and Plot Spectrum for Second Level (A2 and D2) using pwelch
subplot(5,1,5);
[pxx_A2, f_A2] = pwelch(A2, [], [], [], Fs2);
[pxx_D2, f_D2] = pwelch(D2, [], [], [], Fs2);
plot(f_A2, 10*log10(pxx_A2), 'b', 'LineWidth', 1.5); hold on;
plot(f_D2, 10*log10(pxx_D2), 'r', 'LineWidth', 1.5);
title('Power Spectral Density (PSD) for Second Level Approximation and Detail');
xlabel('Frequency (Hz)');
ylabel('Power (dB)');
legend('Approximation', 'Detail');
grid on;
% Display final message
disp('DWT completed with 2-level decomposition and Spectrum Visualization.');

% 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
x = sin(2*pi*f1*t) + 0.5*sin(2*pi*f2*t) + 0.2*sin(2*pi*f3*t);
% Plot the original signal (Mixture of Sinusoids)
figure;
subplot(5,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
% Convolve with the low-pass filter (approximation)
A1 = conv(x, h, 'valid'); % Approximation (low frequency)
% Convolve with the high-pass filter (detail)
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
% New sampling frequency after decimation (downsampling by a factor of 2)
Fs1 = Fs / 2;
% Plot the first level approximation and detail
subplot(5,1,2);
stem([A1, D1], 'filled');
title('First Level Decomposition (Approximation and Detail)');
xlabel('Index');
ylabel('Amplitude');
legend('Approximation', 'Detail');
grid on;
% Calculate and Plot Spectrum for First Level (A1 and D1) using pwelch
subplot(5,1,3);
[pxx_A1, f_A1] = pwelch(A1, [], [], [], Fs1);
[pxx_D1, f_D1] = pwelch(D1, [], [], [], Fs1);
plot(f_A1, 10*log10(pxx_A1), 'b', 'LineWidth', 1.5); hold on;
plot(f_D1, 10*log10(pxx_D1), 'r', 'LineWidth', 1.5);
title('Power Spectral Density (PSD) for First Level Approximation and Detail');
xlabel('Frequency (Hz)');
ylabel('Power (dB)');
legend('Approximation', 'Detail');
grid on;
% Second Level Filtering (Decompose approximation further)
% Convolve approximation A1 with low-pass and high-pass filters
A2 = conv(A1, h, 'valid'); % Approximation (low frequency)
D2 = conv(A1, g, 'valid'); % Detail (high frequency)
% Downsample by a factor of 2
A2 = A2(1:2:end); % Downsample approximation
D2 = D2(1:2:end); % Downsample detail
% New sampling frequency after second decimation
Fs2 = Fs1 / 2;
% Plot the second level approximation and detail
subplot(5,1,4);
stem([A2, D2], 'filled');
title('Second Level Decomposition (Approximation and Detail)');
xlabel('Index');
ylabel('Amplitude');
legend('Approximation', 'Detail');
grid on;
% Calculate and Plot Spectrum for Second Level (A2 and D2) using pwelch
subplot(5,1,5);
[pxx_A2, f_A2] = pwelch(A2, [], [], [], Fs2);
[pxx_D2, f_D2] = pwelch(D2, [], [], [], Fs2);
plot(f_A2, 10*log10(pxx_A2), 'b', 'LineWidth', 1.5); hold on;
plot(f_D2, 10*log10(pxx_D2), 'r', 'LineWidth', 1.5);
title('Power Spectral Density (PSD) for Second Level Approximation and Detail');
xlabel('Frequency (Hz)');
ylabel('Power (dB)');
legend('Approximation', 'Detail');
grid on;
% Display final message
disp('DWT completed with 2-level decomposition and Spectrum Visualization.');
Output