% This is the simulation program for MUSIC  Min-Norm and
% Subspace-Rotation algrithms.
% I generate the data from Uniform Linear Array
% Y:data observed at the sensor array.
% A:matrix of array character
% S:signal matrix
% L,M,P: number of sensors, snapshots,sourers.
% d:the distance between two sensors. it was set to half of the 
%   wavelength of the coming signals.
% Y=A*S+N
clear
rand('seed',0);
ij=sqrt(-1);
P=2;
L=8;
Theta=[-0.4 -0.55]*180/pi;
M=20;
A=[];
% Form the matrix of array character: Uniform Linear.
for j=1:P
	A=[A steer(Theta(j),L)];
end
S=randn(P,M);
Yclear=A*S;
for SNR=0:5:20
	var=sqrt(1/(10^(SNR/10)));
	Y=Yclear+sqrt(var)*N;
	[U,E,V]=svd(Y);
        Uo=U(:,P+1:L);
	Es=E(1:P,1:P);
	Us=U(:,1:P);
	thetaE=state(Us,Es);
        [Msp]=music(Uo);
		
	[Thetarm rootmu mrt]=rootmu(P,Uo);
        B=real(beamfrm(Y));
        Msp=Msp/norm(Msp)*50;
        B=B/norm(B)*50;
        figure(1)
        clf
                  plot([-89:90],Msp,'r')
                  hold
                  plot([-89:90],B)
                  mx=max([Msp B]);
                  plot(thetaE(1)'*180/pi*ones(1:2),[0 mx],'m')
                  plot(Thetarm(1)'*180/pi*ones(1:2),[0 mx],'g')
                  plot(Theta(1)'*ones(1:2),[0 mx],'c')
                  
                  legend('MUSIC','Beamforming','ESPRIT','Root-MUSIC','original')
                  stem(thetaE'*180/pi*ones(1:2),[0 mx;0 mx],'m')
                  stem(Thetarm'*180/pi*ones(1:2),[0 mx;0 mx],'g')
                  stem(Theta'*ones(1:2),[0 mx;0 mx],'c')
		  title(['SNR=',num2str(SNR)])                  
                  figure(2)
clf
plot(exp(sqrt(-1)*[0:359]/180*pi),'r')
hold
plot(rootmu,'co')
plot(mrt,'bx')
plot(mrt,'bo')
axis('equal')
title('Roots of MUSIC polynomial')
disp('pause')
pause
end % end of SNR





