%Purpose:get the carrier frequency from the carrier interferogram;
%input:  'Holo', off-axis hologram; 'Pixel', Pixel size;
%Output: 'w0x','w0y', Carrier frequency in x or y direction; Unit:1/m;
       
function [w0x,w0y]=Carrier_frequency_detection(I_Holo,Pixel)
     
        [y_length,x_length]=size(I_Holo); %Get the size of hologram; 
        Fre_I=fftshift(fft2( fftshift(I_Holo) )); %Frequency spectrum; 
        Fre_I(Fre_I~=Fre_I)=1; %Eleminate the null data; 
        Fre_I=abs(Fre_I); %Filtering; 
        
    %Frequency coordinates: 
        u=linspace(-1/(2*Pixel),1/(2*Pixel),x_length); %Spectrum coordinate; 
        v=linspace(-1/(2*Pixel),1/(2*Pixel),y_length);  %Range: 0~2pi; 
        [uu,vv]=meshgrid(u,v); %Generate the two-dimensional coordinates; 
        Angle_pol=angle(uu+1i*vv); %Azimuth from -pi ~ pi; 
        
        %Remove the dc term:
        Mask1=double( sqrt(uu.^2+vv.^2)>=max(u)/15 );  %remove the dc term;
        Fre_I=Fre_I.*Mask1; %Filtering the zeroth order; 
        
    %For one carrier-frequenccy: (in quadrature 1 and 2 only): 
        Mask2=(Angle_pol>0 & Angle_pol<=pi); 
        uu1=uu(Mask2); vv1=vv(Mask2); % Quadrature 1; 
        Fre_sel_x=Fre_I(Mask2);  Fre_sel_y=Fre_I(Mask2); % Quadrature 1; 
        w0x=uu1(Fre_sel_x==max(max(Fre_sel_x))); %Carrier frequency in x direction; 
        w0y=vv1(Fre_sel_y==max(max(Fre_sel_y))); %Carrier frequency in y direction;  
        
        figure(10); imagesc(u,v,log(Fre_I)); grid on; %Display the frequency spectrum;   
        
end