Clear Filters
Clear Filters

Trouble with for loop to determine the discrete wavelet transform (DWT)

3 views (last 30 days)
Hi, I need assistance plotting the approximation (A) and detail (D) coefficients separately for all sheets in an Excel file over a given time frame. Currently, the existing code generates the transform only for the final sheet (Sheet 8). I am uncertain how to modify the code to plot A1 and D1 separately for all sheets. I would be grateful for any guidance you may provide. Thank you for your time and help.
clc
close all
clear all
filename = 's.xlsx';
% Get the sheet names
[~, sheetNames] = xlsfinfo(filename);
for i = 1:numel(sheetNames)
% Read the data from the sheet
data = xlsread(filename, i);
% Extract the time and signal columns
t(:,i) = data(:, 1);
sig (:,i) = data(:, 2);
N = length(sig);
dt = t(3) - t(2); % sampling time
fs = 1/dt; % freq
signal = sig (:,i);
wname = 'bior6.8';
% Discrete Wavelet, DWT
[CA1,CD1] = dwt(signal,wname);
A1 = idwt(CA1,[],wname,N);
D1 = idwt([],CD1,wname,N);
t (:,i) = linspace(0,N,N)*(1/fs);
subplot(1,2,1);
plot (t,A1,'k','LineWidth',1.5);
title(sprintf('Approximation for sheet%d', i));
set(gca,'fontname','Times New Roman','FontSize',10)
xlabel('Time (secs)')
ylabel('Amplitude')
grid on
subplot(1,2,2);
plot (t,D1,'k','LineWidth',1.5);
title(sprintf('Detail for sheet%d', i));
set(gca,'fontname','Times New Roman','FontSize',10)
xlabel('Time (secs)')
ylabel('Amplitude')
grid on
end

Answers (1)

Sreeram
Sreeram on 13 Aug 2024
Hi Navid,
As per my understanding, you are trying to plot approximation (A) and detail (D) for all the sheets.
The code you provided uses subplot(1,2,p). This divides the current figure into an 1-by-2 grid and will place the plot in position specified by p. You may read more about the MATLAB function subplot here: https://www.mathworks.com/help/releases/R2023b/matlab/ref/subplot.html
To have the A and D plots for all the sheets in a single figure, you may want to choose number of rows in the grid of the figure to be number of sheets in the Excel file (num_sheets) and modify p accordingly.
Here’s how you can implement it:
clc
close all
clear all
filename = 's.xlsx';
% Get the sheet names
[~, sheetNames] = xlsfinfo(filename);
num_sheets = numel(sheetNames);
for i = 1:num_sheets
% Read the data from the sheet
data = xlsread(filename, i);
% Extract the time and signal columns
t(:,i) = data(:, 1);
sig (:,i) = data(:, 2);
N = length(sig);
dt = t(3) - t(2); % sampling time
fs = 1/dt; % freq
signal = sig (:,i);
wname = 'bior6.8';
% Discrete Wavelet, DWT
[CA1,CD1] = dwt(signal,wname);
A1 = idwt(CA1,[],wname,N);
D1 = idwt([],CD1,wname,N);
t (:,i) = linspace(0,N,N)*(1/fs);
subplot(num_sheets,2,2*i - 1); % Note the changes made to the subplot call
plot (t,A1,'k','LineWidth',1.5);
title(sprintf('Approximation for sheet%d', i));
set(gca,'fontname','Times New Roman','FontSize',10)
xlabel('Time (secs)')
ylabel('Amplitude')
grid on
subplot(num_sheets,2,2*i); % Note the changes made to the subplot call
plot (t,D1,'k','LineWidth',1.5);
title(sprintf('Detail for sheet%d', i));
set(gca,'fontname','Times New Roman','FontSize',10)
xlabel('Time (secs)')
ylabel('Amplitude')
grid on
end
set(gcf,'position',[10,10,850,1600]) % So that the output below is legible
Hope it helps.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!