%Purpose:Reconstruct the object wave from the phase-shifting interferograms;

%Input parameters:'I1','I2','I3','I4','I5'Phase-shifting interferograms;
                  %'ph1','ph2','ph3', phase shifts in 'I2' and 'I3' with respect to'I1';

%Output parameters;Complex_Amp, complex amplitude of the reconstructed object wave; 
                   %'Amp_R','Amp_O', The amplitude of the%object and reference wave;
                   
function [Complex_Amp,Amp_R,Amp_O]=Five_Step_PSI_Reconstruction(I1,I2,I3,I4,I5,ph1,ph2,ph3,ph4),
  Q1=I3-I1;
  Q2=I4-I2;
  Numerator=Q1.*(exp(j*ph3)-exp(j*ph1))-Q2.*(exp(j*ph2)-1);  %Numerator of 'OR*';
  Denominator=(exp(-j*ph2)-1)*(exp(j*ph3)-exp(j*ph1))-(exp(-j*ph3)-exp(-j*ph1))*(exp(j*ph2)-1); %Denominator of 'OR*';
  Complex_Amp=Numerator./Denominator; %Reconstructed 'OR*';

  a=I1-2*real(Complex_Amp)+2*abs(Complex_Amp); %(|O|+|R|)^2;
  b=I1-2*real(Complex_Amp)-2*abs(Complex_Amp); %(|O|-|R|)^2;
  Amp_R=( sqrt(abs(a))-sqrt(abs(b)) )/2;       %Reconstructed Amplitude of Reference wave;
  Amp_O=( sqrt(abs(a))+sqrt(abs(b)) )/2;       %Reconstructed Amplitude of Object wave;
  
  Numerator_1=2*(I2-I4);
  Denominator_1=I1+I5-2*I3;
  Wr_phase=atan(-Numerator_1./Denominator_1);
end
%Reference: OPTICS LETTERS,34(22),3553-3555; 