%%
addpath src;
addpath data;
load('10%noise_Diffraction_Pattern.mat');
data = importdata('data\data_vesicle_real.mat');
model = data.model;
%% GPS reconstruction
clear GPS_input;

% define a support
width = 26;
height = 52;
support = ones(height, width);
support = My_paddzero(support, [512,512]);
support = support>0;

% input
GPS_input.diffpat    = diffpat;
GPS_input.support    = support;
GPS_input.isreal     = true;        %whether you reconstruct a real or complex object
GPS_input.iterations = 2000;
GPS_input.sigma      = 0.01;        %parameter to tune, sigma in (0.0, 0.1]



% reconstruction
[rec_gps,y] = GPS_algorithm(GPS_input);
rec_gps_crop = croppedOut( rec_gps, [height, width]);
%Rfactor should be 5.91% for optimal solution

figure(21); img( rec_gps_crop,'GPS rec');

%% OSS reconstruction
rec_OSS = my_OSS(GPS_input);
rec_OSS_crop = croppedOut( rec_OSS, [height, width]);
figure(22); img( rec_OSS_crop,'OSS rec');

%% FSC result
model_crop = model / max(model(:));
model_crop = croppedOut(model_crop, [height, width]);

rec_gps_crop_aligned = abs(rec_gps_crop);
rec_gps_crop_aligned = rec_gps_crop_aligned / max(rec_gps_crop_aligned(:));
rec_gps_crop_aligned = align2D(model_crop, rec_gps_crop_aligned);

rec_oss_crop_aligned = abs(rec_OSS_crop);
rec_oss_crop_aligned = rec_oss_crop_aligned / max(rec_oss_crop_aligned(:));
rec_oss_crop_aligned = align2D(model_crop, rec_oss_crop_aligned);


figure(23); img(rec_oss_crop_aligned,'oss', rec_gps_crop_aligned,'gps',model_crop,'model');

[correlation_gps, freq]  = FourierShellCorrelate(model_crop, rec_gps_crop_aligned,20);
[correlation_oss, freq]  = FourierShellCorrelate(model_crop, rec_oss_crop_aligned,20);
figure(24);
plot(freq, correlation_gps, freq, correlation_oss,'LineWidth',2);
title('FSC');
legend('GPS','OSS')
ylim([0,1]);




