A Matlab code of how to do dynamic mode decomposition of a video sequence. (ChatGPT)
% DMD on a Video Sequence clear; close all; clc;
%% Load Video videoFile = 'video.mp4'; % Change to your video file v = VideoReader(videoFile);
% Parameters resizeFactor = 0.25; % Reduce size to speed up numFrames = min(100, floor(v.Duration * v.FrameRate)); % Limit number of frames
% Read and preprocess video frames X = []; for i = 1:numFrames frame = readFrame(v); grayFrame = im2double(rgb2gray(frame)); % Convert to grayscale grayFrame = imresize(grayFrame, resizeFactor); % Resize X(:,i) = grayFrame(:); % Vectorize and store as column end
%% Build data matrices X1 and X2 X1 = X(:, 1:end-1); X2 = X(:, 2:end);
%% Singular Value Decomposition [U, S, V] = svd(X1, 'econ');
% Choose r - number of modes r = 20; % Adjust based on singular value decay U_r = U(:, 1:r); S_r = S(1:r, 1:r); V_r = V(:, 1:r);
P1. Can you give me a Matlab code for Dynamic Mode Decomposition ? The code should be nicely commented so one can understand the purpose of each section.
P2. Modify the Matlab code to handle an array of flattened image data
P3. For the video sequence, how many snapshots, captured at what fps will be needed to show a mode at 200 Hz and a mode at 2000 Hz?
P4. So DMD gives the eigen modes, their amplitudes and their complex frequencies. From the complex frequencies, the real part gives the growth or decay rate and the imaginary part gives the circular frequncy. Is this interpretation correct ?
P5. How to get the amplitude of a mode ?
P6. Can you give an example of complex amplitude ?
 479     0      0      0
A Matlab code of how to do dynamic mode decomposition of a video sequence.
Amardip Ghosh #Advanced Propulsion Systems (APSYS) Lab
A Matlab code of how to do dynamic mode decomposition of a video sequence. (ChatGPT)
% DMD on a Video Sequence
clear; close all; clc;
%% Load Video
videoFile = 'video.mp4'; % Change to your video file
v = VideoReader(videoFile);
% Parameters
resizeFactor = 0.25; % Reduce size to speed up
numFrames = min(100, floor(v.Duration * v.FrameRate)); % Limit number of frames
% Read and preprocess video frames
X = [];
for i = 1:numFrames
frame = readFrame(v);
grayFrame = im2double(rgb2gray(frame)); % Convert to grayscale
grayFrame = imresize(grayFrame, resizeFactor); % Resize
X(:,i) = grayFrame(:); % Vectorize and store as column
end
%% Build data matrices X1 and X2
X1 = X(:, 1:end-1);
X2 = X(:, 2:end);
%% Singular Value Decomposition
[U, S, V] = svd(X1, 'econ');
% Choose r - number of modes
r = 20; % Adjust based on singular value decay
U_r = U(:, 1:r);
S_r = S(1:r, 1:r);
V_r = V(:, 1:r);
%% Build A tilde
A_tilde = U_r' * X2 * V_r / S_r;
%% Eigen decomposition
[W, D] = eig(A_tilde);
Phi = X2 * V_r / S_r * W; % DMD modes
%% Compute DMD eigenvalues and frequencies
lambda = diag(D);
omega = log(lambda);
%% Compute initial amplitudes b
x1 = X(:,1);
b = Phi x1;
%% Time dynamics
time_dynamics = zeros(r, numFrames - 1);
t = (0:numFrames-2) / v.FrameRate; % Time vector in seconds
for i = 1:length(t)
time_dynamics(:, i) = (b .* exp(omega * t(i)));
end
X_dmd = real(Phi * time_dynamics);
%% Visualize original vs reconstructed frame
originalFrame = reshape(X(:, 30), size(grayFrame));
reconstructedFrame = reshape(X_dmd(:, 30), size(grayFrame));
figure;
subplot(1,2,1); imshow(originalFrame,[]); title('Original Frame 30');
subplot(1,2,2); imshow(reconstructedFrame,[]); title('DMD Reconstructed Frame 30');
Some prompts I used for ChatGPT
P1. Can you give me a Matlab code for Dynamic Mode Decomposition ? The code should be nicely commented so one can understand the purpose of each section.
P2. Modify the Matlab code to handle an array of flattened image data
P3. For the video sequence, how many snapshots, captured at what fps will be needed to show a mode at 200 Hz and a mode at 2000 Hz?
P4. So DMD gives the eigen modes, their amplitudes and their complex frequencies. From the complex frequencies, the real part gives the growth or decay rate and the imaginary part gives the circular frequncy. Is this interpretation correct ?
P5. How to get the amplitude of a mode ?
P6. Can you give an example of complex amplitude ?