%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%	function [array_sig,sa_delay]=signal_delay(Fs,New_Fs,pos_Mics,pos_Src,dist_src_ctr,sig);
%
%	This function add delay to signal as if they were received at array element.
%	Input:
%		Fs: original sampling frequency		
%		New_Fs: the new upsampling frequency	
%		pos_Mics: a matrix contains the location of each element, has the form (x_i,y_i,z_i) in ith row.
%		pos_Src: position vector of the source
%		dist_src_ctr: distance of source to origin
%		sig: source signal 
%		tone_sig: flag for tone signal
%	Output:
%		array_sig: constructed signal in time domain.
%		sa_delay: delay in the unit of samples
%	
%	Last modified on:	
%		Jan. 13 2003
%		Feb. 06, 03, add input parameters
%		Feb. 09, 04, use ideal far field model.
%       Sep. 16, 2008, use for near field.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [array_sig,sa_delay]=signal_delay(Fs,New_Fs,DOA,pos_Mics,pos_Src,dist_src_ctr,sig);

phi_0=DOA(1,1); %azimuth
theta_0=DOA(2,1);

if size(sig,1)>size(sig,2)
	sig=sig.';
end

N_array_els=size(pos_Mics,1); %number of array elements
dist_src_mic=zeros(N_array_els,1);
dist_mic_ctr=zeros(N_array_els,1);
dist_mic_ctr_farfield=zeros(N_array_els,1);

k0=[sin(theta_0)*cos(phi_0) sin(theta_0)*sin(phi_0) cos(theta_0)]; %Direction cosines???? TAKE CARE WITH THIS
for k_array_els=1:N_array_els
	%absolute distance from the source to the mics
	dist_src_mic(k_array_els)=sqrt(sum((pos_Mics(k_array_els,:)-pos_Src).^2)); % from each mic to the signal source
	%relative distance from the mics to the reference in the direction of the source
	dist_mic_ctr(k_array_els)=dist_src_mic(k_array_els)-dist_src_ctr; %distance from each mic to the signal minus the distance from the signal to the reference!!
    %If positive, we have a delay, if negative we have an advance!!!
    
	dist_mic_ctr_farfield(k_array_els)=-sum(pos_Mics(k_array_els,:).*k0); %This is nothing but a^T*pi, so this is for far field only!!!
    
    %Attenuation of the signal with respect the center
    att_ctr_mic(k_array_els) = dist_src_ctr/dist_src_mic( k_array_els);
end
%dist_mic_ctr=dist_mic_ctr_farfield; %Luis eliminated this, because is for far field only!!!

v=343;%speed of sound
tdelay_ctr_mic=1/v*(dist_mic_ctr); %time delay in seconds if positive is a delay, if neg. an advance

sa_delay=tdelay_ctr_mic*Fs;%sample delay
sa_delay=round(sa_delay*New_Fs/Fs);%new sample delay. This is done so the delay is an sample integer very smart YLi!!

resampled_sig=resample(sig,New_Fs,Fs);   %Resample the signal
array_sig2=zeros(N_array_els,length(sig)); %Allocate array signal to fill up

for k_array_els=1:N_array_els %Loop for the array elements, create the delayed signal
	array_sig=zeros(1,length(resampled_sig)); %Allocate array signal to fill up
	if sa_delay(k_array_els)>0 %If the delay is >0 add zeros to the signal!!
		array_sig=[zeros(1,sa_delay(k_array_els)),resampled_sig(1:end-sa_delay(k_array_els))];
		array_sig2(k_array_els,:)=resample(array_sig,Fs,New_Fs); %resample back
	elseif sa_delay(k_array_els)<0 %If the delay is <0 eliminate samples and add zeros at the end
		array_sig=[resampled_sig(-sa_delay(k_array_els)+1:end),zeros(1,-sa_delay(k_array_els))];
		array_sig2(k_array_els,:)=resample(array_sig,Fs,New_Fs); %resample back
	elseif sa_delay(k_array_els)==0 %else do nothing
		array_sig=resampled_sig; 
		array_sig2(k_array_els,:)=resample(array_sig,Fs,New_Fs); %resample back
    end
    %Add the attenuation due to Near field
    array_sig2(k_array_els,:)=att_ctr_mic(k_array_els).*array_sig2(k_array_els,:);
end

array_sig=array_sig2; %Each row has the signal for each sensor!!!
return