%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%function [array_sig,sa_delay]=siggal_con(DOA,SIR,SNR,sigSOI,use_default);
%	This fucntion generate received array signal.
%	Input:
%		DOA_0: DOA of desired signal, in radian
%		DOA_1: DOA of interference siganl, in radian
%		SIR: signal to interference ratio
%		SIN: signal to ambient noise ratio
%		sigSOI: desired source signal
%		use_default: flag for using default DOAs.
%	Output:
%		array_sig: the received array signal
%		sa_delay: ?
%
% 	Last modified on:
%		Feb 12, 2003: add the input parameters
%		Feb 20, modified to 64
%		Aug 20, modified to 204, 8000kHz
%		Jan 09, 2004, modified to 214 
% 		modified on Feb 17,2005, for different partition schemes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [array_sig,sa_delay]=signal_con(DOA,SIR,SNR,sigSOI,rSOI,rInt,mpos,use_default,disp_flag,array1_ref,desiredspeaker)

%element location
%Get the xyz coordinates of the sensors
%pos_Mics= [mpos' zeros(length(mpos),1)];
pos_Mics= [mpos']; %for 3D we already have the z dimension
%We need to reference the position of the mics because the reference is not
%(0,0)
pos_Mics(:,1)=pos_Mics(:,1)-array1_ref(1);

%Find SOI energy in the case we need to add SIR or SNR in the future
Fs=16000;
total_n=length(sigSOI);
s0_energy=sum(sigSOI.*sigSOI)/total_n;

%New_Fs=5*Fs;%new sample freq
New_Fs=20*Fs;%new sample freq to achieve integer number of delay samples!!

%source location put here the radius
%rSOI=1e10;%rSOI should be large enough to satisfy the far field assumption
phi_0=DOA(1,1); %Azimuth
theta_0=DOA(2,1);   %Polar
x0=rSOI*sin(theta_0)*cos(phi_0);
y0=rSOI*sin(theta_0)*sin(phi_0);
z0=rSOI*cos(theta_0);
pos_SOI=[x0,y0,z0]; %SOI position
%generate the array signal for the SOI
[array_sig,sa_delay]=signal_delay(Fs,New_Fs,DOA(:,1),pos_Mics,pos_SOI,rSOI,sigSOI); % enable when use desired signal
if disp_flag
	disp('	Desired signal added...');
end

%inter_sig=zeros(size(array_sig));
if use_default==0
    %interference location
    %no_inter=length(SIR)-1;
    %inter_f=[950:20:1050];

    for	k=2:size(DOA,2)
        %1. Generate the location of the interference
        rInt=rInt;%rInt should be large enough to satisfy the far field assumption
        x_i=rInt*sin(DOA(2,k))*cos(DOA(1,k));
        y_i=rInt*sin(DOA(2,k))*sin(DOA(1,k));
        z_i=rInt*cos(DOA(2,k));
        pos_Int=[x_i,y_i,z_i];

        %2. Generate the interference signal
        if k==2 %First interference
            %Read the interference from a wavefile
            if desiredspeaker==1
                s_i=wavread('..\data\LoopyMusic.wav');
            else
                s_i=wavread('..\data\speech_dft.wav');
            end
            
            s_i=s_i(1:total_n); %Be sure the interference has same samples as the signal
            if 0 %Plot the interferent signal
                figure('Name',['Interference (speaker2)']);
                plot(s_i,'DisplayName',...
                    ['Interference'],'Marker','none','LineStyle','-','LineWidth',2,'Color','blue');
                set(gca,'fontsize',12);
                title('Interference (speaker2)')
                legend(gca,'Location','SouthEast');
                axis([0 95800 -1 1])
                grid on
            end %if 0

        else
            keyboard; %Stop here because this is not coded for more interferences
            %generate a random tone signal with frequency large than 400Hz.
            f=1000;

            A=0.8;phase=0;rn_seed=k+100;over_sam=1;
            s_i=gen_tone(A,f,over_sam*Fs,phase,over_sam*total_n,1,rn_seed);
            s_i=s_i(1:total_n);
        end

        %3. Adjust interference signal energy accoding to SIR
        if 0 %Luis: do not do that for now
            s_i_energy=sum(s_i.*s_i)/total_n;
            ratio_s_i=s0_energy/10^(SIR(k-1)/10)/s_i_energy;
            s_i=s_i*sqrt(ratio_s_i);
            s_i_energy=sum(s_i.*s_i)/total_n
        end

        %4. Generate the array signal for interference
        %noise_loc=fix(total_n/8);
        noise_loc=1;

        temp=signal_delay(Fs,New_Fs,DOA(:,k),pos_Mics,pos_Int,rInt,s_i); %enable when use desired signal

        array_sig(:,noise_loc:end)=array_sig(:,noise_loc:end)+temp(:,noise_loc:end); %original, including both desire sig and interference

        if disp_flag
            disp(['	',num2str(k-1),'th	interference added...']);
        end
    end %for	k=2:size(DOA,2)
else%use_default==1
    keyboard; %Luis, we should not be here!!!

    k=2;
    load inter_sig1.mat;
    s_i_energy=sum(inter_sig(1,:).*inter_sig(1,:))/total_n;
    ratio_s_i=s0_energy/10^(SIR(k-1)/10)/s_i_energy;
    array_sig=array_sig+inter_sig(:,1:total_n)*sqrt(ratio_s_i);
    disp('	One interference signal added...');

    k=3;
    load inter_sig2.mat;
    s_i_energy=sum(inter_sig(1,:).*inter_sig(1,:))/total_n;
    ratio_s_i=s0_energy/10^(SIR(k-1)/10)/s_i_energy;
    array_sig=array_sig+inter_sig(:,1:total_n)*sqrt(ratio_s_i);
    disp('	One interference signal added...');
    clear inter_sig;
end

%generate ambient noise
if 0 %Luis, do not add any noise so far!!!
    N=size(pos_Mics,1);
    randn('state',7);
    amb_n=randn(N,total_n);%Gaussian noise
    %amb_n=HothNse(N,total_n);%HothNoise upto 4kHz.
    %amb_n=HothNse(N,total_n);%HothNoise upto 8kHz.
    %load amb_noise.mat;
    %amb_n=amb_n(1:N,1:total_n);
    n_energy=sum(amb_n(1,:).*amb_n(1,:))/total_n;
    SNR_n=SNR; %desired SNR of source to interference, in dB
    ratio_n=s0_energy/10^(SNR_n/10)/n_energy;
    amb_n=amb_n*sqrt(ratio_n)*1;
    n_energy2=sum(amb_n(1,:).*amb_n(1,:))/total_n;
    %add ambient noise to signal
    noise_loc=fix(size(amb_n,2)/8);
    %noise_loc=1;
    use_saved=0;
    array_sig(:,noise_loc:end)=array_sig(:,noise_loc:end)+amb_n(:,noise_loc:end);
    %save amb_n.mat amb_n;
    clear amb_n;
    if disp_flag
        disp('	Ambient noise added...');
    end
end

%save ..\data\array_sig.mat array_sig;

%plot the spectrum of array signal.
plot_no=0;
if plot_no==1
    figure('Name',['SOI']);;
    plot(sigSOI);
    
    temp=resample(array_sig(1,:),Fs,New_Fs);
    Y=fft(temp,256);
    freq=[0:127]/256*Fs;
    figure('Name',['SOI Frequency Spectrum']);
    plot(freq,abs(Y(1:128)));
end
return