# How to modify the 2nd code like the 1st one?

6 vues (au cours des 30 derniers jours)
Sadiq Akbar le 8 Jan 2024
Commenté : William Rose le 16 Fév 2024
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;
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
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponses (1)

William Rose le 8 Jan 2024
Modifié(e) : William Rose le 8 Jan 2024

[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;
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);
M=10, P=37.
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 commentairesAfficher 18 commentaires plus anciensMasquer 18 commentaires plus anciens
Sadiq Akbar le 16 Fév 2024
Thanks a lot fpor your prompt response. Yes, you are right but I thought I have told you already. However it's my mistake. You are right because I went through the above discussion but I didn't find what I asked for now.
However, one question still remains that you have given the code upto:
AF=abs(AF);
fprintf('AF(phi=%.1f,theta=%.1f)=%.3f.\n',[phi,theta]*180/pi,AF)
you have not given the code how did you have got the plots? Also how did you take the radius as lambda/(2*(2)^0.2)?
Further, if we want to do the same process for 2 sources, then how will we get the plots?
Regards,
William Rose le 16 Fév 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.

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Antennas, Microphones, and Sonar Transducers dans Help Center et File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by