How to modify the 2nd code like the 1st one?
Show older comments
I have two codes. I want to modify the 2nd code given below like that of the 1st code given below:
CODE1:
u=[30 40 50 80]; %i.e. u=[theta1 theta2 phi1 phi2]% Desired vector
[~,C]=size(u);
P=C/2;% No. of signal sources
M=C;% No. of antennas
% calculate observed vectors
xo=zeros(1,M);% received observed signal on antennas placed on x-axis
yo=zeros(1,M);% received observed signal on antennas placed on y-axis
zo=zeros(1,M);% received observed signal on antennas placed on z-axis
for k=1:M
for i=1:P
xo(1,k)=xo(1,k)+exp(-1i*(k-1)*pi*cosd(u(i))); %--------------------------(1)
yo(1,k)=yo(1,k)+exp(-1i*(k-1)*pi*sind(u(i))*cosd(u(P+i))); %-------------(2)
zo(1,k)=zo(1,k)+exp(-1i*(k-1)*pi*sind(u(i))*sind(u(P+i))); %-------------(3)
end
end
If you look at CODE1, we have put the values of vector u in the cosd and sind of equations (1), (2) and (3). Now we want to do the same logic with the equations (1), (2), (3) and (4) of CODE2. I have done a little bit but got confused.
CODE2:
u=[30 40 50 80]; %i.e. u=[theta1 theta2 phi1 phi2]% Desired vector
[~,C]=size(u);
P1=C/2;% No. of signal sources
M1=C;% No. of antennas
% f=1e9;
% c=3e8;
% l=c/f;
% k=(2*pi)/l;
N=8;% No. of antennas. This will become as M1 from above in this code
n=0:N-1;
phi_n=2*pi*n/N;
phi=-pi:pi/18:pi;
theta=0:pi/18:pi/2;
M=length(theta);
P=length(phi);
% d_circular=l/2;
% a=(N*d_circular)/(2*pi);% Radius of circle
for p=1:P
for m=1:M
% AF(m,p)=sum(exp(-i*k*a*sin(theta(m))*(cos(phi(p)-phi_n))));
AF(m,p)=sum(exp(-1i*(N/2)*sin(theta(m))*(cos(phi(p)-phi_n))));%----------------------------------(1)
x(m,p)=abs(AF(m,p))*sin(theta(m))*cos(phi(p));% received observed signal on x-axis %-------------(2)
y(m,p)=abs(AF(m,p))*sin(theta(m))*sin(phi(p));% received observed signal on y-axis %-------------(3)
z(m,p)=abs(AF(m,p))*cos(theta(m));% received observed signal on z-axis %-------------------------(4)
end
end
Answers (1)
William Rose
on 8 Jan 2024
Edited: William Rose
on 8 Jan 2024
@Sadiq Akbar,
[edit: I submitted the answer prematurely.]
Code 2 runs without error. Why do you think code 2 needs fixing?
%% code 2 by Sadiq Akbar
u=[30 40 50 80]; %i.e. u=[theta1 theta2 phi1 phi2]% Desired vector
[~,C]=size(u);
P1=C/2;% No. of signal sources
M1=C;% No. of antennas
% f=1e9;
% c=3e8;
% l=c/f;
% k=(2*pi)/l;
N=8;% No. of antennas. This will become as M1 from above in this code
n=0:N-1;
phi_n=2*pi*n/N;
phi=-pi:pi/18:pi;
theta=0:pi/18:pi/2;
M=length(theta);
P=length(phi);
% d_circular=l/2;
% a=(N*d_circular)/(2*pi);% Radius of circle
for p=1:P
for m=1:M
% AF(m,p)=sum(exp(-i*k*a*sin(theta(m))*(cos(phi(p)-phi_n))));
AF(m,p)=sum(exp(-1i*(N/2)*sin(theta(m))*(cos(phi(p)-phi_n))));%----------------------------------(1)
x(m,p)=abs(AF(m,p))*sin(theta(m))*cos(phi(p));% received observed signal on x-axis %-------------(2)
y(m,p)=abs(AF(m,p))*sin(theta(m))*sin(phi(p));% received observed signal on y-axis %-------------(3)
z(m,p)=abs(AF(m,p))*cos(theta(m));% received observed signal on z-axis %-------------------------(4)
end
end
fprintf('M=%d, P=%d.\n',M,P);
figure; mesh(1:P,1:M,x);
xlabel('p'); ylabel('m'); zlabel('x'); grid on
figure; surf(x,y,z);
xlabel('x'); ylabel('y'); zlabel('z');
title('abs(AF)'); axis equal; grid on
You can delete P1 and M1, since they are not used. The rest of the code works. If you are unhappy with the output of code 2, please explain why.
In code 1, x0 is a row vector with M=4 columns. Each element of x0 is computed by adding up P=2 complex components. y0 and z0 are similar.
In code 2, AF(m,p) is a 10x37 complex array. The values of AF() span a hemisphere, at 10 degree increments, from -180 to +180 in azimuth, and from 0 to 90 in elevation. Arrays x,y,z are the x,y,z, components of abs(AF), by a standard spherical-to-Cartesian coordinate transformation, assuming
points along the z-axis, and
points along the x-y plane. I have added a plot of x(m,p) and a plot of the abs(AF) surface.
20 Comments
Sadiq Akbar
on 8 Jan 2024
William Rose
on 8 Jan 2024
@Sadiq Akbar, I'm sorry that I made a major edit to my answer, and that you responded in the intervening time. I hit "Submit" when I meant to press the green button to run the code.
Is phi is an azimuth angle? Is theta is an elevation angle?
Line 1 of code 1 and line 1 of code 2 is
u=[30 40 50 80]; %i.e. u=[theta1 theta2 phi1 phi2]% Desired vector
Do you want to compute AF and x,y,z at
and at
?
If the answer is yes, then you can simply extract the appropriate elemnts from aray AF and from arrays x, y, and z, after you have computed the AF and x, y, z at all angles on the hemisphere.
Sadiq Akbar
on 8 Jan 2024
Since u represents two direction vectors, I recommend that u() be a 2x2 array instead of a 1x4 vector. Let
. I have put theta before phi in the matrix, to match the ordering of the indices used for AF and x, y, z.
. I have put theta before phi in the matrix, to match the ordering of the indices used for AF and x, y, z.To compute AF and x,y,z at the two specified directions:
% In this script, AF and x,y,z are computed at C specified directions.
u=[50,30; 80,40]; % directions: u=[theta1 phi1; theta2 phi2] (degrees)
[C,~]=size(u); % number of directions
x=zeros(C,1); y=x; z=x; AF=x; % allocate arrays
N=8; % number of antennas
n=0:N-1;
phi_n=360*n/N; % antenna directions (degrees)
for i=1:C
AF(i)=sum(exp(-1i*(N/2)*sind(u(i,1))*(cosd(u(i,2)-phi_n))));
x(i)=abs(AF(i))*sind(u(i,1))*cosd(u(i,2)); % x component of signal
y(i)=abs(AF(i))*sind(u(i,1))*sind(u(i,2)); % y component of signal
z(i)=abs(AF(i))*cosd(u(i,1)); % z component of signal
end
%% Display results on console
for i=1:C
fprintf('Direction %d: theta=%d, phi=%d, |AF|=%.3f, AFx=%.3f, AFy=%.3f, AFz=%.3f\n',...
i,u(i,1),u(i,2),abs(AF(i)),x(i),y(i),z(i));
end
OK
To compute AF at all angles on the hemisphere, and x,y,z at C specified directions:
% In this script, AF is computed at all directions on the hemisphere.
% x,y,z are computed at C specified directions.
u=[50,30; 80,40]; % directions: u=[theta1 phi1; theta2 phi2] (degrees)
[C,~]=size(u); % number of directions
x=zeros(C,1); y=x; z=x; % allocate x, y, z arrays
theta=0:10:90;
phi=-180:10:+180;
M=length(theta);
P=length(phi);
AF=zeros(M,P); % allocate AF array
N=8; % number of antennas
n=0:N-1;
phi_n=360*n/N; % antenna directions (degrees)
%% Compute AF at all directions on the hemisphere
for m=1:M
for p=1:P
AF(m,p)=sum(exp(-1i*(N/2)*sind(theta(m))*(cosd(phi(p)-phi_n))));
end
end
%% Compute x,y,z at specific directions
for i=1:C
m=find(theta==u(i,1)); % index of specified theta
p=find(phi==u(i,2)); % index of specified phi
AF0=abs(AF(m,p)); % |AF| in specified direction
x(i)=AF0*sind(u(i,1))*cosd(u(i,2)); % x component of signal
y(i)=AF0*sind(u(i,1))*sind(u(i,2)); % y component of signal
z(i)=AF0*cosd(u(i,1)); % z component of signal
end
%% Display results on console
for i=1:C
fprintf('Direction %d: theta=%d, phi=%d, AFx=%.3f, AFy=%.3f, AFz=%.3f\n',...
i,u(i,1),u(i,2),x(i),y(i),z(i));
end
The values for AFx, AFy, AFz in this script match the values from the previous script.
Sadiq Akbar
on 9 Jan 2024
William Rose
on 11 Jan 2024
@Sadiq Akbar, Please explain your new goal. I know you have already explained it, but I do not understand.
Perhaps I will understand your new goal if you answer some questions I havee about what we have already done.
Question 1. Is the description below correct?
Eight antennae (N=8) are evenly spaced around a circle in the X-Y plane. The circle is centered at the origin. AF(phi,theta) is the length of a resultant field vector, due to all N antennae, at the origin, pointing in direction
, where theta=elevation and phi=azimuth. Your initial goal was to know the x,y,z components of AF, in 2 specific directions (C=2).
Question 2. Please explain the formula for AF. The formula for AF in the code is
AF(m,p)=sum(exp(-1i*(N/2)*sind(theta(m))*(cosd(phi(p)-phi_n))));
which is equal to
where N is the number of antennae (N=8) and
is the angle from the origin to antenna j. I do not understand the physics underlying the formula above. Why is N/2 there? The term
gives the projection of a unit vector that points in the X-Y plane toward antenna j onto a vector that points in direction
. Why is this term present inside the exponential?
Now let us consider your new goal. You write "I want to take another vector b of same dimension as is u and put that in place of u in the given formulas and then call those x,y and z as xe,ye and then find the absolute error between each dimension for all the 8 antennas. I have tried below but antennas are 8 and the index of x,y and z are only 2. I don't know why?"
Array u is C-by-2 array which specifies C directions of interest (
for each direction).
You wish to do calculations like the calculations with u, but using a new array b, which has the same dimensions as u.
Question 3. Is it true that b represents C directions, which are different than the C directions specified in u? If that is true, then why is b identical to u, in your code above? I would expect the values in b and u to differ.
You wrote "the index of x,y and z are only 2. I don't know why?" X, y, and z are vectors of length C=2 because array u has 2 rows. If you want to know x,y, and z in more directions, then increase the number of rows in u.
We will use the notation xo,yo,zo for the x,y,z components associated with the directions in u. We will use the notation xe,ye,ze for the x,y,z components associated with the directions in b.
Question 4. Is it true that you want to compute the mean squared error between (xo,yo,zo) and (xe,ye,ze), as shown in the formula below?
Sadiq Akbar
on 11 Jan 2024
William Rose
on 11 Jan 2024
I went to the web link you provided, but I did not find it helpful. It was a lot of code and no pictures. Perhaps I need a good picture. The site you provided did include a link to an image, which showed an array of 4 antennae in a line.
Is it true that you want to estimate the sensitivity of a circular antenna array, in the X-Y plane, to a source that is located at a large distance, in the direction
=inclination,azimuth? If so, then we need to esitmate the phase differences at the different antennae. For this we must know the ratio of the wavelength to the circle radius. Do you assume that the resultant signal, AF, is the simple sum of the signals from N antennae?
If I am understanding correctly, then there will be no phase differences for a source at the zenith (
), and the sensitivity AF is maximal in this case. If there are just 2 antennae, at
and
, and if the radius is a quarter wavelength, and the source is in the X-Y plane, aligned with the antennas (i.e.
), then there is a path length difference of half a wavelength, i.e. the two antennae are exactly out of phase, and the sensitivity AF=0.
Am I understanding correctly, now?
Sadiq Akbar
on 13 Jan 2024
William Rose
on 15 Jan 2024
Edited: William Rose
on 16 Jan 2024
[Edit: I asked a quesiton that you had already answered before. Sorry about that. I was a passenger in a car and was having trouble browsing our previous discussion.]
@Sadiq Akbar, I am glad you found that site helpful. You said “ the source is in a plane which is perpendicular to the plane of xy in which the array is there”.
No matter where in the sky the source is, there will be an infinite number of planes perpendicular to the XY plane which also pass through the source.
Does the diagram below represent your problem? It shows N=8 antennae in a circular array in the X-Y plane, and a source (green) with zenith angle theta and azimuth phi.

Let AF be the sum of the signals from the N antennae. It seems to me that AF varies as a function of theta and phi, as shown below:
where
is the vector from the circle center to antenna k, and
is a unit vector pointing from the circle center toward the source, and λ is the wavelength. This post is getting long so I will stop here, for your comments.
If my diagram above is not a correct representation of your problem, then perhaps you can post a better picture. Or you can rely on others on this site who, no doubt, understand your problem better than me.
William Rose
on 16 Jan 2024
My formula for AF is
This is different from the formula in your original post. The figure below shows the results when I use the formula above.
Example: Let N=4, and let the circle radius=
. Then we expect AF=4 when the source is at the zenith (i.e.
), since all four antennae will be in phase. We expect AF=0 when the source is on the horizon
, with azimuth=45, 135, 225, or 315, because at these azimuths, two antenna are exact one half wavelength out of phase with the other two, so they cancel out. The script produces the expected results. See plot below.

Sadiq Akbar
on 14 Feb 2024
William Rose
on 15 Feb 2024
Edited: William Rose
on 15 Feb 2024
[Edit: Correct typo in the last equation in this comment: I forot to include "/".]
I derived the formula for AF by looking at the image below (which I also posted above):

I assume that the source (green) is a large distance from the antenna array, and therefore the amplitude is the same at all antennas in the array. I assume the amplitude equals one. These assumptions simplify the calculations. We need the phase differences of the antenna from some rference phase. I choose the reference phase to be the center of the array. Then I need to know the phase of each antenna, relative to the center of the array.
The phase difference, in radians, is
where the negative sign is because a larger distance causes a phase lag, and where
The difference in the distance difference of antenna k to the source, compared to the distance of the center to the source, is
where
is the vector from the circle center to antenna k, and
is a unit vector pointing from the circle center toward the source. You can confirm that by analyzing the geometry of the figure above.
Put it all together:
Sadiq Akbar
on 15 Feb 2024
Angles are in radians. theta is the zenith angle of the source: theta=0 for a source at the zenith. phi is the source azimuth, increasing CCW from tyhe +x axis. Then
% nhat=[sin(theta)*cos(phi),sin(theta)*sin(phi),cos(theta)];
The code for AF, for a single source location, is
N=8; % number of antennae
Ar=0.50; % antenna circle radius, in wavelengths
theta=pi/3; % source zenith angle
phi=pi/4; % source azimuth
% compute values
alpha=2*pi/N; % angle between adjacent antennae
r=Ar*[cos((1:N)'*alpha),sin((1:N)'*alpha),zeros(N,1)]; % radial vectors to antennae
% Compute AF
AF=0;
nhat=[sin(theta)*cos(phi),sin(theta)*sin(phi),cos(theta)];
for k=1:N
AF=AF+exp(2*pi*1i*dot(r(k,:),nhat));
end
AF=abs(AF);
fprintf('AF(phi=%.1f,theta=%.1f)=%.3f.\n',[phi,theta]*180/pi,AF)
If you want multiple values for theta and phi, then make phi and theta vectors, and make AF a 2D array, instead of scalars. Make outer loops to compute AF(i,j) for different values of theta and phi.
Sadiq Akbar
on 16 Feb 2024
William Rose
on 16 Feb 2024
Now you are asking an entirely new quesiton: how to do estimation. Please post this as a new question on Matlab answers.
Sadiq Akbar
on 16 Feb 2024
William Rose
on 16 Feb 2024
I did not provide the code for the plots because you will learn more about Matlab and about coding by doing some of this work yourself. To make the plots, you need to compute AF for a range of theta values and for a range of phi values. I gave you instructions on how to do that. Specifically, I said "If you want multiple values for theta and phi, then make phi and theta vectors, and make AF a 2D array, instead of scalars. Make outer loops to compute AF(i,j) for different values of theta and phi."
I will give you some additional suggestions:
- If you want 4 values of theta (=zenith angle), and 12 values of phi (=azimuth), then let AF be 4 by 12.
- Once you have computed array AF(), using the outer for loops as I suggested, plot it with surf(phi,theta,AF)
- Read the help for surf(). Read the help for colorbar(), if you want a color bar.
- Multiply phi and theta by 180/pi, for plotting purposes, if you want the ploot to have angles in degrees, since both are in radians in my formulas above.
I could have chosen any value for the circle radius, r. I chose
because I knew that with 4 antennas, this r value would put adjacent antenna pairs exactly a half-wavelength apart when phi=45, 135, etc. I knew this by thinking about the geometry of the right triangles formed between the center and adjacent antenna pairs. This results in complete signal cancellation (i.e. AF=0) at phi=45, 135, etc., which is an interesting special case.
Categories
Find more on Phased Array Design and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

