clc;
close all;
clear variables;

% Create phantom image (Shepp-Logan style)
n = 32;
[P_pd, P_t1, P_t2] = get_nmr_phantom(n);

% Plot the properties of the tissue within the phantom
figure;
subplot(131); imagesc(P_pd, [0.9, 1.1]);  colormap gray; title('\rho (proton density)');
subplot(132); imagesc(P_t1);  colormap gray; title('T1 - weighted');
subplot(133); imagesc(P_t2);  colormap gray; title('T2 - weighted');

% Set up sequence parameters
tr = 5; te = 1;
n_rep = 16;

% Call your function to construct the k_space 
[k_space, t] = reconstruction_nmr(P_pd, P_t1, P_t2, tr, te, n_rep);

% Plot the k-space (re-arrange with fftshift to get low frequencies in the
% middle part
figure; 
subplot(121); imagesc( log(abs(fftshift(k_space))) ); colormap gray;
title('k-space');

%% Part II.

% Get the reconstructed image (crop the k-space to contain the image)
image = abs(ifft2(k_space));
image = image(:, 2:32);

subplot(122);
t_str='TE = %d [ms], TR = %d [ms]';
title_str = sprintf(t_str, te, tr);
imagesc(image);
title(title_str);
colormap gray;