0% found this document useful (0 votes)
13 views14 pages

Report DMD CurveFit

The document outlines a MATLAB workflow for generating, plotting, and saving sine wave data, followed by performing Dynamic Mode Decomposition (DMD) on the data. It includes detailed MATLAB code for creating the sine waves, saving figures, and conducting curve fitting using the cftool function. The final step involves comparing the results of the DMD with the curve-fitted modes to analyze the signal characteristics.

Uploaded by

You Cuber
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views14 pages

Report DMD CurveFit

The document outlines a MATLAB workflow for generating, plotting, and saving sine wave data, followed by performing Dynamic Mode Decomposition (DMD) on the data. It includes detailed MATLAB code for creating the sine waves, saving figures, and conducting curve fitting using the cftool function. The final step involves comparing the results of the DMD with the curve-fitted modes to analyze the signal characteristics.

Uploaded by

You Cuber
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Step1: Create Data, plot figures and Save the data in a .

csv File

Table 1 Matlab Code: create_save_data.m

clc;
clear;
close all;

% Define folder to save figures (Change this path as needed)


% saveFolder = 'DMD/matfig'; % Update this path
saveFolder = fullfile(userpath, 'MATLAB Drive', 'DMD_CB',
'image_fig'); % Update this path

if ~exist(saveFolder, 'dir')
mkdir(saveFolder);
end

dt = .0005; % time step of signal


L = 1.0;
t = 0:dt:L;
fn = 10;
omegan = 2*pi*fn;

Ns = 8;
An = 1.5;
% signal: sum of sines
x(1,:) = An*sin(2*pi*(fn)*t);
x(2,:) = (An/4)*sin(2*pi*(2*fn)*t);
x(3,:) = (An/8)*sin(2*pi*(3*fn)*t);
x(4,:) = (An/16)*sin(2*pi*(4*fn)*t);
x(5,:) = (An/32)*sin(2*pi*(5*fn)*t);
x(6,:) = (An/64)*sin(2*pi*(6*fn)*t);
x(7,:) = (An/128)*sin(2*pi*(7*fn)*t);
x(8,:) = (An/256)*sin(2*pi*(8*fn)*t);
xclean =
x(1,:)+x(2,:)+x(3,:)+x(4,:)+x(5,:)+x(6,:)+x(7,:)+x(8,:);

figure(1)
% Create axes
axes1 = axes('YGrid','on','XGrid','on','FontSize',20,...
'PlotBoxAspectRatio',[0.24 0.23 0.23]);
% % Uncomment the following line to preserve the Y-limits of
the axes
%ylim(axes1,[-1.1 1.1]);
box(axes1,'on');
hold(axes1,'on');
% Create plot
plot(t,xclean,'LineWidth',1.5,'Color','k')
% Create ylabel
ylabel('$x$','Interpreter','latex');

% Create xlabel
xlabel('$t$','Interpreter','latex');

% Loop to plot precomputed data


for i = 1:Ns
figure; % Create a new figure
% Create axes
axes1 = axes('YGrid','on','XGrid','on','FontSize',20,...
'PlotBoxAspectRatio',[0.24 0.23 0.23]);
% % Uncomment the following line to preserve the Y-limits
of the axes
%ylim(axes1,[-1.1 1.1]);
box(axes1,'on');
hold(axes1,'on');
% Create plot
plot(t, x(i, :), 'LineWidth', 1.5);

% Formatting with underscores in labels


title(['Sine Wave $x_', num2str(i), '$'], 'Interpreter',
'latex');
xlabel('Time (s)', 'Interpreter', 'latex');
ylabel(['$x_', num2str(i), '$'], 'Interpreter', 'latex');
% y_i subscript notation
grid on;

% % Define full save path for the figure


% savePath = fullfile(saveFolder, ['x_', num2str(i),
'.fig']);
%
% % Save figure in .fig format
% saveas(gcf, savePath);

% Define full save path for the figure


savePath = fullfile(saveFolder, ['x_', num2str(i),
'.png']);

% Save figure in .fig format


saveas(gcf, savePath);

% Pause to visualize each figure


pause(0.5);
Table 2 Original Signal and Its eight Components

Step2: Open the file, do DMD, match the results generated in the previous
code with the DMD generated results

Table 3 Matlab Code: DMD_Test3.com


clc; clear; close all;

% Define folder to save figures (Change this path as needed)


saveFolder = fullfile(userpath, 'MATLAB Drive', 'DMD_CB',
'image_fig');

if ~exist(saveFolder, 'dir')
mkdir(saveFolder);
end

A = dlmread(fullfile(userpath, 'MATLAB Drive', 'DMD_CB',


'image_fig', 'dmd_data.csv'));
t = A(:,1);
x = A(:,2);

s = 500; % number of times to shift -stack signal


for k = 1:s
X(:,k) = x(k:end-s+k);
end

Lt = 500;
X1 = X(:,1:Lt-1);
X2 = X(:,2:Lt);

dt = t(5)-t(4);
r = 16;
[Phi,omega,lambda,b,Xdmd] = DMD(X1,X2,r,dt);

% DMD Reconstruction
time_dynamics = zeros(r,length(t));
for iter = 1:length(t)
time_dynamics(:,iter) = (b.*exp(omega*t(iter)));
end
X_dmd = Phi*time_dynamics;
X_dmdb = zeros(1,size(time_dynamics,2));
X_dmda = zeros(size(Phi,2),size(time_dynamics,2));
for i = 1:size(Phi,2)
X_dmda(i,:) = Phi(1,i)*time_dynamics(i,:);
X_dmdb(1,:) = X_dmdb+X_dmda(i,:);
end

figure(1)
plot(t,x,'k','LineStyle','-','LineWidth',1.5)
hold on
plot(t,real(X_dmd(1,:)),'b','LineStyle','--','LineWidth',1.5)
hold on
plot(t,real(X_dmdb(1,:)),'r','LineStyle','-.','LineWidth',1.5)

% Sort in ascending order based on absolute imaginary value


imag_omega = imag(omega);
[~, idx] = sort(abs(imag_omega),'ascend');
omega_sort = omega(idx);
n = 0;
for i = 1:length(omega_sort)/2
figure; % Create a new figure
% Create axes
axes1 = axes('YGrid','on','XGrid','on','FontSize',20,...
'PlotBoxAspectRatio',[0.24 0.23 0.23]);
% % Uncomment the following line to preserve the Y-limits
of the axes
%ylim(axes1,[-1.1 1.1]);
box(axes1,'on');
hold(axes1,'on');
% Create plot
plot(t, real(X_dmda(idx(i+n),:)+X_dmda(idx(i+n+1),:)),
'r','LineWidth', 1.5);

% Formatting with underscores in labels


title(['Sine Wave $DMD_', num2str(i), '$'], 'Interpreter',
'latex');
xlabel('Time (s)', 'Interpreter', 'latex');
ylabel(['$DMD_', num2str(i), '$'], 'Interpreter',
'latex'); % y_i subscript notation
grid on;

% % Define full save path for the figure


% savePath = fullfile(saveFolder, ['DMD_', num2str(i),
'.fig']);
%
% % Save figure in .fig format
% saveas(gcf, savePath);

% Define full save path for the figure


savePath = fullfile(saveFolder, ['DMD_', num2str(i),
'.png']);

% Save figure in .fig format


saveas(gcf, savePath);

% Pause to visualize each figure

pause(0.5);
% i
% i1 = i+n
% i2 = i+n+1
n = n+1;
end
Step3: Curve Fit the Signal Generated in Step 1 (Use cftool)
i. Run the following code:
Table 4 Matlab Code: open_csv

clc; clear; close all;

A = dlmread(fullfile(userpath, 'MATLAB Drive', 'DMD_CB',


'image_fig', 'dmd_data.csv'));
t = A(:,1);
x = A(:,2);

ii. Type ‘cftool’ in Matlab command window. The following Window will
appear:

iii. Give a file name. Choose ‘t’ as ‘X data’ and ‘x’ as ‘Y data’ from the dropdown
menu.
iv. Choose ‘Custom Equation’. Generate a function y = f(x) according to your
signal that you have generated. My function:
a1*sin(w1*x)+a2*sin(2*w1*x)+a3*sin(3*w1*x)+a4*sin(4*w1*x)+a5*sin(5*w1*
x)+a6*sin(6*w1*x)+a7*sin(7*w1*x)+a8*sin(8*w1*x)
I entered this function in the box provided:

v. Click on ‘Fit Options’. You have to give lower and upper bound to some
parameters (Mostly the fundamental frequency). I know my fundamental
frequency is 62.83. So I provide the lower and upper bound of w_1 as 62 and
63, respectively:

With that, we got the curve fitting exactly.


vi. Copy the results in the report:
Table 5 Matlab Code: cftool_modes

General model:
f(x) = a1*sin(w1*x)+a2*sin(2*w1*x)+a3*sin(3*w1*x)+a4*sin(4*w1*x)+a5*sin(5*w1*x)
+a6*sin(6*w1*x)+a7*sin(7*w1*x)+a8*sin(8*w1*x)
Coefficients (with 95% confidence bounds):
a1 = 1.5 (1.5, 1.5)
a2 = 0.375 (0.375, 0.375)
a3 = 0.1875 (0.1875, 0.1875)
a4 = 0.09375 (0.09375, 0.09375)
a5 = 0.04688 (0.04688, 0.04688)
a6 = 0.02344 (0.02344, 0.02344)
a7 = 0.01172 (0.01172, 0.01172)
a8 = 0.005859 (0.005859, 0.005859)
w1 = 62.83 (62.83, 62.83)

Goodness of fit:
SSE: 1.4060e-24
R-square: 1
Adjusted R-square: 1
RMSE: 2.6568e-14
vii. Copy the coefficients in a matlab script file:

clc; clear; close all;

% Define folder to save figures


saveFolder = fullfile(userpath, 'MATLAB Drive', 'DMD_CB',
'image_fig'); % Update as needed

% Amplitude values for curve-fitted modes


a = [1.5, 0.375, 0.1875, 0.09375, 0.04688, 0.02344, 0.01172,
0.005859];

w1 = 62.83; % Base frequency


dt = 0.0005; % Time step
L = 1.0; % Signal duration
t = 0:dt:L; % Time vector

% Placeholder for DMD reconstructed modes (replace with actual


data)
X_DMD = zeros(8, length(t)); % Assuming DMD modes are
available

for i = 1:8
% Generate curve-fit (CFT) sine waves
x_CFT = a(i) * sin(i * w1 * t);

% Placeholder: Generate DMD modes (Replace X_DMD(i,:) with


actual DMD data)
X_DMD(i,:) = a(i) * sin(i * w1 * t + pi/20); % Example DMD
mode with slight phase shift

% Create figure
figure;
hold on;

% Plot DMD mode (Solid Blue)


plot(t, X_DMD(i,:), 'b', 'LineWidth', 1.5, 'LineStyle',
'-');

% Plot Curve-Fit mode (Magenta Dash-dot)


plot(t, x_CFT, 'm', 'LineWidth', 1.5, 'LineStyle', '-.');
% Formatting
title(['Sine Wave Mode-', num2str(i)], 'Interpreter',
'latex');
xlabel('Time (s)', 'Interpreter', 'latex');
ylabel('Displacement', 'Interpreter', 'latex');

% Legend at the top-right corner


legend(['DMD_', num2str(i)], ['CFT_', num2str(i)],
'Location', 'northeast');

grid on;
hold off;

% Save the figure


savePath = fullfile(saveFolder, ['mode_', num2str(i),
'.png']);
saveas(gcf, savePath);

pause(0.5); % Pause for visualization

viii. Compare the 8-modes obtained from curve fitting with the 8-modes
obtained from DMD.

You might also like