Commit 37be30ea authored by Felix Kong's avatar Felix Kong

tested for hamlyn heart dataset as well

remember, DMD on hamlyn doesn't work well
parent 98377c8e
......@@ -56,7 +56,7 @@ classdef dmd < handle
% tell user how long it took
regressionTime = toc;
if(obj.verbose)
fprintf('DMD regression() took %f seconds.',regressionTime);
fprintf('DMD regression() took %f seconds.\n',regressionTime);
end
end
......@@ -71,7 +71,6 @@ classdef dmd < handle
error('Need to run regression() before running reconstruct().');
end
% UNTESTED, NOT SURE IF THIS IS MATHEMATICALLY CORRECT
X2hatComplex = obj.Phi*obj.Lambda*(obj.Phi\obj.X);
assert(norm(imag(X2hatComplex)) <= 1e-6);
X2hat = real(X2hatComplex);
......@@ -88,7 +87,7 @@ classdef dmd < handle
error('Need to run regression() before running predict().');
end
assert(all(size(initial_condition) == [size(obj.X,1),1]));
assert(all(size(initial_condition) == [size(obj.X,1),1]),'Initial condition must be a column vector with the same dimension as a column in data matrix X, which has length %d',size(obj.X,1));
xhat = nan(length(initial_condition),num_timesteps);
xhat(:,1) = initial_condition;
b0 = obj.Phi\initial_condition;
......
% convert hamlyn heart video to image sequence
% workingDir = tempname;
% mkdir(workingDir)
% mkdir(workingDir,'images')
%
% shuttleVideo = VideoReader('~/Downloads/hamlyn_heart.avi');
%
% ii = 1;
% while hasFrame(shuttleVideo)
% img = readFrame(shuttleVideo);
% filename = [sprintf('%03d',ii) '.jpg'];
% fullname = fullfile(workingDir,'images',filename);
% imwrite(img,fullname) % Write out to a JPEG file (img1.jpg, img2.jpg, etc.)
% ii = ii+1;
% end
%
% imageNames = dir(fullfile(workingDir,'images','*.jpg'));
% imageNames = {imageNames.name}';
%
%
% for ii = 1:length(imageNames)
% img = imread(fullfile(workingDir,'images',imageNames{ii}));
% grayImages(:,:,ii) = rgb2gray(img);
% end
%% Try DMD on it
clear variables
load data/HamlynHeartImages.mat;
......@@ -32,7 +6,7 @@ disp('Data preparation...')
num_images = 100;
% for k = 1:size(grayImages,3)
% Step 0: data preparation
for k = 1:num_images % first 100 images
image_k = grayImages(:,:,k);
Xall(:,k) = double(image_k(:));
......@@ -41,6 +15,18 @@ end
X = Xall(:,1:end-1);
X2 = Xall(:,2:end);
% Step 1: initialize DMD object
verbose = true;
heart_dmd = dmd(Xall,verbose);
% Step 2: perform DMD regression
truncation_rank = 50;
heart_dmd.regression(truncation_rank);
% Step 3: reconstruct the data AND predict into the future using predict()
initial_condition = Xall(:,1);
num_timesteps = size(Xall,2) + 100; % predict data and 100 frames into the future
xhat = heart_dmd.predict(initial_condition,num_timesteps);
disp('DMD Regression...')
[U0,S0,V0] = svd(X, 'econ');
......@@ -65,7 +51,8 @@ b0 = Phi\X(:,1);
%% Calculate DMD output
disp('Reconstructing...')
steps = size(X,2);
% steps = size(X,2);
steps = 200;
xkvec = zeros(steps,size(X,1));
% calculate the output states
......@@ -77,6 +64,8 @@ for j = 1:steps
reconstruction(:,:,j) = uint8((reshape(xkvec(j,:),size(grayImages,1),size(grayImages,2)))); % im2uint8 doesnt work
end
disp(norm(xkvec' - xhat))
%% replay
reconsructionHdl = implay(reconstruction);
set(reconsructionHdl.Parent,'Name',sprintf('Reconstruction with %d modes.',r));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment