%chelp+
%  This is a fun code to show the information contained in 
%  the "phases". Two images are taken -- one of a beautiful 
%  princess and the second of an energetic eagle. Both images 
%  are Fourier transformed. New images are then generated by 
%  inverse Fourier transforming the FFT-ed images, but after 
%  having mutually swapped the phases of either image. 
%chelp-

%=================================================================
% The rich "phases" of the lovely princess are indeed important -- 
% look at the beautiful reconstruction they allow! The spirit 
% of the eagle too is noticeable through its phases...
% many more experiments can be built -- like replacing the 
% amplitude distribution to a uniform one and then inverse 
% FFT-ing the phase distribution only. These experiments are 
% actually useful, for example, in the holography that we had 
% done in 2006, except, no one will be interested if I start 
% with such boring stuff as holography! And why should I start 
% with such boring stuff when there is no dearth of beauty 
% around -- like the pretty princess and the eagle...
%=================================================================

clc
clear all
close all

   % load the files: 
   f1 = load('c2.txt');
   f2 = load('e2.txt');


   nx = size(f1,2); 
   ny = size(f1,1);  

   x = linspace(1,nx,nx);
   y = linspace(ny,1,ny);


   % FT domain: 
   %1. 
   ft1 = fft2(f1);
   pha1 = atan2(imag(ft1),real(ft1));
   ft2 = fft2(f2); 
   pha2 = atan2(imag(ft2),real(ft2));

   aout = abs(ft1);
   im = imag(ft2); 
   re = real(ft2); 
   ph_out = atan2(im,re);
   iota = sqrt(-1); 
   ftmp = aout.*exp(iota*ph_out);
   fout_tmp = ifft2(ftmp);
   fout1 = abs(fout_tmp); 

   aout = abs(ft2);
   im = imag(ft1); 
   re = real(ft1); 
   ph_out = atan2(im,re);
   ftmp = aout.*exp(iota*ph_out);
   fout_tmp = ifft2(ftmp);
   fout2 = abs(fout_tmp); 

   a = 1; 
   colormap('cool')
   icnt = 0; 
while(a == 1 && icnt < 1)
   subplot(2,2,1),surface(x,y,f1)
   shading('flat')
   axis('off','equal')
   title('princess')

   subplot(2,2,2),surface(x,y,f2)
   shading('flat')
   axis('off','equal')
   title('eagle')

   subplot(2,2,3),surface(x,y,fout1)
   shading('flat')
   axis('off','equal')
   title('pha-eagle amp-princess')

   subplot(2,2,4),surface(x,y,fout2)
   shading('flat')
   axis('off','equal')
   title('pha-princess amp-eagle')

   yorn = yes_or_no("Do you wish to meet the +ve princess: ")
   if(yorn == 1)
	   a = 1; 
	   colormap('gray')
	   icnt = icnt + 1; 
   else
	   a = 0; 
	   break 
   end
end


