clc
clear
N=10;                         % Number of the Antennas
M=120;                        % Number of the directions
KK=1;
Pt=1;                         % Transmit Power of each Antenna
theta = linspace(-pi/2,pi/2,M);
teta = linspace(-pi/2,pi/2,M);

Pd=zeros(M,1);% Desired pattern
%Pd2=ones(M+1,1)*(0);
shift=0;
counter = 0;
t1=(-10+shift)/180*pi;                   % x/180*M should be a integer numberv
i1=t1/pi*M+M/2+1;               % for the case of M=120, x=3*k
t2=(0+shift)/180*pi;
i2=t2/pi*M+M/2+1;

eta = 15/180*pi;
index1 = find(teta <= t2 & teta >= t1);

Pd(index1) = 10;

index2 = find(teta <= t1 & teta >= t1 - eta);
Pd(index2) = [];
teta(index2) = [];

t3=(0+shift)/180*pi;                   % x/180*M should be a integer numberv
i3=t3/pi*M+M/2+1;               % for the case of M=120, x=3*k
t4=(10+shift)/180*pi;
i4=t4/pi*M+M/2+1;

index3 = find(teta <= t4 & teta >= t3);

Pd(index3) = 10;

index4 = find(teta <= t4 + eta & teta >= t4);

Pd(index4) = [];
teta(index4) = [];

T = length(Pd);




cvx_begin sdp
variable X1(N,N) hermitian;
variable X2(N,N) hermitian
variable z
variable t
minimize z
subject to
for i=1:T
  
    trace(matA(teta(i),N)*X1) + trace(matA(teta(i),N)*X2) - Pd(i)*t  <= z ;
    trace(matA(teta(i),N)*X1) + trace(matA(teta(i),N)*X2) - Pd(i)*t  >= -z ;
    
   
end

X1(1,1)+X1(10,10) + X2(1,1)+X2(10,10) == Pt/10;
X1(2,2)+X1(9,9) + X2(2,2)+X2(9,9) == Pt/10;
X1(3,3)+X1(8,8) + X2(3,3)+X2(8,8) == Pt/10;
X1(4,4)+X1(7,7) + X2(4,4)+X2(7,7) == Pt/10;
X1(5,5)+X1(6,6) + X2(5,5)+X2(6,6) == Pt/10;

X1  >= 0
X2  >= 0


cvx_end

WW = randomize(X1,Pd,teta,t);

WW = [WW(:,1) WW(:,3) WW(:,2) WW(:,4)];

for k = 1:length(theta)
    for l = 1:N;
    a(l,1)=exp(j*pi*sin(theta(k))*(l-1));
    end
    b(k) = a'*WW*WW'*a;
    d(k) = a'*X1*a;
    e(k) = a'*X2*a;
end
   

% for teta1= -pi/2:.01:pi/2;
%     counter = counter+1;
%     teyf1(counter) = trace(matA(teta1,N)*WW*WW') + trace(matA(teta1,N)*WW1*WW1');
% end
% 
% teta2 = [-pi/2:.01:pi/2];




 figure
% plot(teta2,teyf1)
 hold on
 plot(theta,b,'b-')

figure(2)
plot(theta,d,'r-',theta,e,'b--')

Xopt = WW;
save Xopt.mat Xopt

Xrot = [Xopt(:,1) Xopt(:,2)];

Xrot1 = integ(Xrot,N);

save Xrot1.mat Xrot1

e = [1 -1];
ee = e.'*e;



%U_opt = main_code(100,2,'min',ee,Xrot1);




%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
% Randomization for recovery


% [u,v]=eig(X1);
% root_mat=u*v.^(.5);
% for shomara=1:1000
%     disp(shomara)
%     Candidatew=[];
%     for lll=1:KK
%         ran_vec=randn(N,1)+j*randn(N,1);
%         ran_vec=ran_vec./abs(ran_vec);
%         Candidatetemp=root_mat*ran_vec;
%         Candidatew=[Candidatew Candidatetemp];
%     end
%     for lll=1:N/2
%         aaa=[Candidatew(lll) Candidatew(N-(lll-1))];
%         Candidatew(lll)= Candidatew(lll,:)/norm(aaa)*sqrt(Pt/10);
%         Candidatew(N-(lll-1))= Candidatew(N-(lll-1))/norm(aaa)*sqrt(Pt/10);
%     end
% 
% 
%     for ind1=1:M
%         temp(ind1)=abs(trace(matA(teta(ind1),N)*Candidatew*Candidatew')-Pd1(ind1));
%     end
%     Val1=max(temp);
%     if (shomara ==1)
%         Val=Val1;
%         Candidatefinal=Candidatew;
%     else
%         if ( abs(Val)- abs(Val1) > 0  )
%             Val=Val1;
%             Candidatefinal=Candidatew;
%         end
%     end
% end
% 
% %--------------------------------------------------------------------------
% %--------------------------------------------------------------------------
% %--------------------------------------------------------------------------
% % Plot of the Patterns
% counter=0;
% for teta1=-pi/2:.01:pi/2
%     counter=counter+1;
%     teyf(counter)=trace(matA(teta1,N)*X1);
% end
% plot((-pi/2:.01:pi/2)/pi*180,10*log10(abs(teyf)),'b')
% 
% hold on
% counter=0;
% WW=[Candidatefinal conj(Candidatefinal(10:-1:1))]/sqrt(2);
% for teta1=-pi/2:.01:pi/2
%     counter=counter+1;
%     teyf1(counter)=trace(matA(teta1,N)*WW*WW');
% end
% plot((-pi/2:.01:pi/2)/pi*180,10*log10(abs(teyf1)),'m--')
% save MAT2


