Beijing Institute of Technology | Ming-Jian Li
The following MATLAB code is a companion code for the course on Artificial Intelligence and Simulation Science. It functions to calculate the beam scanning process of a phased array antenna.
1clear;clc;
2freq=300e6; omega=2*pi*freq; c=3e8;
3lambda=c/freq; k=2*pi/lambda;
4d=lambda*0.5;
5M=6
6N=6
7R=(M+1)*d/2; f=1*R*2;
8xm=-R+d:d:R-d; yn=-R+d:d:R-d;
9[x y]=meshgrid(xm,yn);
10rf=[0 0 f]; u0hat=[0 0 1];
11qe=1;
12theta=linspace(0,pi/2,200);phi=linspace(0,pi*2,200);
13[THETA,PHI]=meshgrid(theta,phi);
14nImages = 24;
15fig = figure;
16thetabeam=-pi/6;
17phibeam=0;
18for idx=1:nImages
19 if idx<nImages/2
20 thetabeam=thetabeam+pi/6*4/nImages;
21 else
22 thetabeam=thetabeam-pi/6*4/nImages;
23 end
24 % phibeam=phibeam+2*pi/nImages;
25 E_total=zeros(200,200);
26 for im = 1:M
27 for in = 1:N
28 rmn=[x(im,in) y(im,in) 0];
29 rm_rf=rmn-rf;
30 mag_rmn_rf=norm(rm_rf,2);
31 phimn(im,in)=k*(mag_rmn_rf ...
32 -(x(im,in)*cos(phibeam)+y(im,in)*sin(phibeam))*sin(thetabeam) );
33 reflect_amp(im,in)=1;
34 E_total=E_total+reflect_amp(im,in).*exp(1i.*(phimn(im,in)+k.*d.*((in-1/2).*cos(PHI)+(im-1/2).*sin(PHI)).*sin(THETA)));
35 end
36 end
37 Ex=abs(E_total).*sin(THETA).*cos(PHI);
38 Ey=abs(E_total).*sin(THETA).*sin(PHI);
39 Ez=abs(E_total).*cos(THETA);
40 set(gcf,'Position',[100 100 640 480])
41 surf(Ex,Ey,Ez,'EdgeColor','none');
42 shading interp
43 colormap jet
44 axis([-20 20 -20 20 0 40]);
45 drawnow
46 frame = getframe(fig);
47 img{idx}=frame2im(frame);
48end
49timedelay=0.2;
50filename = 'scan.gif';
51for idx = 1:nImages
52 [A,map] = rgb2ind(img{idx},256);
53 if idx == 1
54 imwrite(A,map,filename,'gif','LoopCount',Inf,'DelayTime',timedelay);
55 else
56 imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',timedelay);
57 end
58end
The result is as follows.