%% Function fplotVA3d. This function plots several 3-D beampatterns of a
%ULA. The ULA is lying on whatever axis
%Inputs
%lambda: wavelength
%sub_pos: cartesian positions of array elements, each element is a row of 3 columns
%v_va: weight vector
%mult: multiplier, if 1 the polar angle is calculated from 0 to pi/2, if 2 to pi

function fplotLA3d(lambda,d,sub_pos,v_va,mult)
%% Plot the VA 2-D beampattern for a lot of elevation angless

phiVec2D=0:2:360; %0:pi/(100):2*pi;
phiVec2D = phiVec2D*pi/180;

thetaVec2D=0:2:mult*90; %(0)*pi/180:pi/100:(90)*pi/180;
thetaVec2D = thetaVec2D*pi/180;

figure
itotal = 0; %Total index
for ithetaVec=1:length(thetaVec2D),
    theta_scan=thetaVec2D(ithetaVec);
    for iphiVec=1:length(phiVec2D),
        itotal = itotal +1;
        phi_scan=phiVec2D(iphiVec);
        
        xnor = sin(theta_scan)*cos(phi_scan);
        ynor = sin(theta_scan)*sin(phi_scan);
        znor = cos(theta_scan);
        DOA_scan=[xnor;ynor;znor];
        
        s_LAscan=exp(j*2*pi/lambda*sub_pos*DOA_scan); %LA steering vector
        %s_0=     exp(j*2*pi/lambda*d*sin(phi_scan)*sub_pos); %Column steering vector
      
        
        %Find the output power or beampattern for this particular DOA
        bp2D(iphiVec,ithetaVec)=abs(s_LAscan'*v_va)^2; %This is ro!!!
        
        %Convert to Cartesian coordinates of the beampattern
        xcart(itotal)= (bp2D(iphiVec,ithetaVec))*xnor;
        ycart(itotal)= (bp2D(iphiVec,ithetaVec))*ynor;
        zcart(itotal)= (bp2D(iphiVec,ithetaVec))*znor;
    end; %for iphiVec=1:length(phiVec2D),
    plot(phiVec2D/pi*180,(bp2D(:,ithetaVec)),'DisplayName',['Delay-and-Sum'],'LineStyle','--',...
        'LineWidth',2,'Color',0.8*[1 1 1]/ithetaVec^(1/3));
    %plot(phiVec2D/pi*180,10*log10(bp2D(:,ithetaVec)),'DisplayName',['Delay-and-Sum'],'LineStyle','--',...
    %    'LineWidth',2,'Color',0.8*[1 1 1]/ithetaVec^(1/3));

    hold on
end %for ithetaVec=1:length(thetaVec2D),

set(gca,'fontsize',12);
xlabel(['azimuth angle \phi (view from \theta=[',num2str(thetaVec2D(1)*180/pi),...
    ', ',num2str(thetaVec2D(end)*180/pi),'])'])
ylabel('Gain');
title(['VA_{das} Beampattern cuts for  \theta=[',num2str(thetaVec2D(1)*180/pi),...
', ',num2str(thetaVec2D(end)*180/pi),']'])
axis([0 2*180 0 1])

%% Plot VA beampattern it in a mesh
[thetamesh,phimesh]=meshgrid(thetaVec2D,phiVec2D);
meshVAbp = figure('Color',[1 1 1]);
axes('Visible','off','Parent',meshVAbp,'XDir','reverse','YGrid','on','XGrid','on');
%axes('Parent',meshRAbp,'XDir','reverse','FontSize',12);
surf(thetamesh*180/pi,phimesh*180/pi,bp2D)
view([65.5 10]);
set(gca,'fontsize',12);xlabel('Polar angle {\theta}'); 
ylabel('Azimuth angle {\phi}'); zlabel('Magnitude');
title(['VA 3D Beampattern']);

%% Create a VA 3-D beampattern with plot3
fig3DVA = figure('Color',[1 1 1]);
% Create axes
axes('Visible','on','Parent',fig3DVA,'YGrid','off','YDir','reverse','XGrid','off',...
    'XDir','reverse');
xlim([-1 1]); ylim([-1 1]); zlim([-1 1]);
view([-35.5 24]);
hold('all');
% Create xlabel
set(gca,'fontsize',12);xlabel('x, coordinate'); ylabel('y, coordinate'); zlabel('z, coordinate');
plot3(xcart,ycart,zcart,'MarkerSize',4,'Marker','none')
%Create our own axes
hold on
plot3([0 2],[0 0],[0 0],'MarkerSize',14,'Color',[0 0 0])
plot3([0 0],[0 2],[0 0],'MarkerSize',14,'Color',[0 0 0])
plot3([0 0],[0 0],[0 2],'MarkerSize',14,'Color',[0 0 0])
% Create textbox
annotation(fig3DVA,'textbox','String',{'y'},'FitHeightToText','off',...
    'EdgeColor',[1 1 1],...
    'Position',[0.8703 0.258 0.02692 0.04097]);

% Create textbox
annotation(fig3DVA,'textbox','String',{'z'},'FitHeightToText','off',...
    'EdgeColor',[1 1 1],...
    'Position',[0.5375 0.9087 0.02692 0.04097]);

% Create textbox
annotation(fig3DVA,'textbox','String',{'x'},'FitHeightToText','off',...
    'EdgeColor',[1 1 1],...
    'Position',[0.1057 0.3545 0.02692 0.04097]);

title(['VA 3D Beampattern']);

%% Create a VA 3-D beampattern with surf
%Create the mesh
[thetamesh,phimesh]=meshgrid(thetaVec2D,phiVec2D);
[xcartm,ycartm,zcartm]=sph2cart(phimesh,pi/2-thetamesh,bp2D);
fig3DmeshVA = figure('Color',[1 1 1]);
% Create axes
axes('Visible','off','Parent',fig3DmeshVA,'YGrid','off','YDir','reverse','XGrid','off',...
    'XDir','reverse');
xlim([-1 1]); ylim([-1 1]); zlim([-1 1]); 
view([-35.5 24]);
hold('all');
% Create xlabel
set(gca,'fontsize',12);xlabel('x, coordinate'); ylabel('y, coordinate'); zlabel('z, coordinate');
surf(xcartm,ycartm,zcartm);

%Create our own axes
hold on
plot3([0 2],[0 0],[0 0],'MarkerSize',14,'Color',[0 0 0])
plot3([0 0],[0 2],[0 0],'MarkerSize',14,'Color',[0 0 0])
plot3([0 0],[0 0],[0 2],'MarkerSize',14,'Color',[0 0 0])
% Create textbox
annotation(fig3DmeshVA,'textbox','String',{'y'},'FitHeightToText','off',...
    'EdgeColor',[1 1 1],...
    'Position',[0.8703 0.258 0.02692 0.04097]);

% Create textbox
annotation(fig3DmeshVA,'textbox','String',{'z'},'FitHeightToText','off',...
    'EdgeColor',[1 1 1],...
    'Position',[0.5375 0.9087 0.02692 0.04097]);

% Create textbox
annotation(fig3DmeshVA,'textbox','String',{'x'},'FitHeightToText','off',...
    'EdgeColor',[1 1 1],...
    'Position',[0.1057 0.3545 0.02692 0.04097]);
title(['VA 3D Beampattern']);
