Anda di halaman 1dari 7

Laboratory Manual

ELECTRICAL ENGINEERING / ELECTRONICS

Created
pao: Sep-02

181E - Matlab Introduction For


Telecommunication
Revised

The laboration gives an introduction to the following subjects:

• The MATLAB Command Window


• MATLAB Scripts
• MATLAB Functions

To be returned:
Name Date: Comments

Group nr. Period Year

Course Course-Code

Teaching Assistant

Approved Signed

University of Gävle – IBT/Electronics


Purpose
The purpose of this laboratory exercise is twofold – first of all it intends to give an
introduction to MATLAB and secondly the results are a couple of tools useful in
telecommunication studies.

Matlab is an extremely useful tool for the telecommunication engineer. The name MATLAB
stands for matrix laboratory. It was developed in the seventies and has been refined and
extended over the years. The syntax is easy to learn and it’s closely related to Pascal, C etc.
The major advantage over its cousins is the number of prewritten functions, called tools.
These tools are kept in toolboxes, such as the “Signal Processing Toolbox”.

The main drawback with Matlab is that the source code is interpreted during execution, so
you need a fast computer and, sometimes, lots of time. In order to improve your source code,
try to minimize e.g. for-loops. Instead, matrix algebra should be used when it’s possible.
This is an exercise that, hopefully, will make you curious on the program’s utilities and an
example of what can be done.

General instructions
– The experiments should be performed in groups of 2 or 3 students.
– The exercise is fulfilled and approved after a well-performed demonstration of
functional pieces of code that are well structured and commented.
Preparations
The student must have a working computer account before entering the laboratory.
References
For the antenna discussions below the book Antenna Theory (Analysis and Design) by
Constantine A Balanis may be of interest.
Instructions

1 GETTING STARTED

1. Start Matlab. Windows that you want to have open are the Command window and Workspace.
2. Choose a directory for you files: File>Set Path… Create and choose a directory for your own
files.
3. You will now try three examples of how Matlab programming can be performed, that is:
a. Coding directly in the command window.
b. Entering your code as script.
c. Creating functions in Matlab.

2 USING THE COMMAND WINDOW


The Command window is used during evaluations, often as an advanced graphic calculator.
We start with an exercise were you will create data for an antenna pattern.

This will output values of theta and phi for a dipole antenna from 0 to pi. If you know the
mathematical description of another pattern, this script can easily be adapted to other antenna
types. You can use copy and paste between MS WordPad and Matlab’s Command window.
(The text written in the Courier typeface is supposed to be typed into the command
window.)

Create a 101-long theta vector of equal spacing from 0 to п:

theta=0:pi/100:pi;

Note that a vector called theta can be seen in the Workspace window.
181E - Matlab Introduction For Telecommunication 2002-09-19
1
The normalized theta pattern of a dipole has these components (Balanis, Eq. 4-86):

wave=(cos((pi/2).*cos(theta))).^2;
nomi=sin(theta).^2;

(Note the dot “.” in front of the operator in the above expressions. The rule is: when an
operation is to be performed on element basis; use the dot. Only using e.g. the multiplication
sign “*” implies that a matrix multiplication is to be performed, which is not always the
intention. For a matrix that consists of only one element the dot can be ignored.)

In order to correct for zeroes in the denominator, this for-loop is used:

for n=1:length(theta)
if nomi(1,n)<0.00001
nomi(1,n)=1;
end
end

The complete theta pattern will be:


wave=wave./nomi;

Convert into dB:


thetaval=10*log10(wave); %elevation value vector (change into
measured vector, thetaval=[e1 e2...])

Assign a variable of the same length as thetaval:


vec=length(thetaval);
Create a phi vector for an omni directional antenna:
phival=ones(1,vec); %azimuth value vector (change into measured
vector, phival=[a1 a2...])
Convert into dB:
phival=10*log10(phival);
You can now analyze your created data by the plot function:
plot(theta,thetaval);
And:
plot(theta,phival);

181E - Matlab Introduction For Telecommunication 2002-09-19


2
3 MATLAB SCRIPT
The example you now have tried is a clumsy way to use Matlab. To create a Matlab script is
a much better solution, and even easier to use!

Type ‘clear’ in the Command window. The Workspace is cleared, i.e. the memory
allocated by variables is cleared. Note that the command window is not affected.

Start in the main window, by clicking the New M-file button. A clean page is opened, where
you can enter your code. First, save your file in the name ‘dipole’ in your own folder. Enter
the code below into your file (use copy and paste):

%This m-file outputs values of theta and phi for a dipole antenna
%from 0 to pi
theta=0:pi/100:pi;
wave=(cos((pi/2).*cos(theta)).^2;
nomi=sin(theta);
for n=1:length(theta)
if nomi(1,n)<0.00001
nomi(1,n)=1;
end
end
wave=wave./nomi;
thetaval=wave; %elevation value vector (change into measured vector,
thetaval=[e1 e2...])
thetaval=10*log10(thetaval);
vec=length(thetaval);
phival=ones(1,vec); %azimuth value vector (change into measured
vector, phival=[a1 a2...])
phival=10*log10(phival);

Evaluate your code by clicking Debug>Save and Run.


Does it work? If you return to your Command window you will se a message in red text
similar to the sample below:

??? Error: File: c:\windows\skrivbord\dipole.m Line: 4 Column:


33 ")" expected, ";" found.

Return to your file and correct the error. It’s as, stated by the debugger, a missing closing
bracket.
Mark the first line with wave=… and right click. Select ‘Evaluate selection’ and check in the
command window if there still is an error.

Try again to save and run. Did it work better? When a file has been successfully executed,
the contents of a global matrix, vector or variable can be controlled by resting the cursor on
it (in the source code).

In the Command window, type:


help dipole

Note that the first lines in your file are treated as an informative help text. Text after the
%-sign is skipped during execution. The first %-lines in the file, followed by an empty line,
will be displayed in the Command window if you write help ‘filename’.

181E - Matlab Introduction For Telecommunication 2002-09-19


3
4 CREATE A FUNCTION

Having global variables, as in a script, means that you have to keep a close check of all
variables you declare. Instead, local variables are to prefer. Therefore, most tools in Matlab
are written as functions. The different tools are stored in toolboxes, with different fields of
application (and often a bit expensive).
We will now make a function that will be called in the Command window, that you can save
in your own folder. This specific code converts a 2D antenna measurement to a 3D-pattern
and plots it.

Start by creating a new M-file (as you just did). Save it as ‘antplot’.
A function must contain the first word ‘function’, the name that you call the function with
and its argument(s). Copy and paste the sections below:

function h=antplot(thetavaldb,phivaldb)
%antplot(thetaval,phival);
%
%ANTPLOT plots a 3D radiation pattern from 2D data
%for antennas that follow a symmetrical pattern.
%
%Instructions: measure the theta and phi patterns in dB.
%Define the thetaval and phival vectors: thetaval=[d1 d2 ...] etc
%
%thetaval=the measured theta values, equally spaced from 0 to pi (in
a row)
%phival=the measured phi values, equally spaced from 0 to pi (in a
row)
%
%Note that thetaval and phival must be of equal length.
%Also note that theta and phi are defined as in antenna
measurements,
%i.e. theta=0 is the same as the z-axis.

Note that the arguments of antplot can have any names; it is only locally the variables are
named thetavaldb and phivaldb. First, we convert to the pattern to linear again:

thetaval=exp(thetavaldb/10);
phival=exp(phivaldb/10);

Assuming that the patterns are measured from 0 to п, with equal spacing the following will
do:

vec=length(thetaval);
phimin=0; phimax=pi;
theta=phimin:(phimax-phimin)/(vec-1):phimax; %equally spaced values
(radians)
phi=theta; %if the phi scale=theta scale

(theta and phi, or phimin and phimax could instead be treated as input arguments when the
function is called.)
It is now time to create the complete pattern. In order to understand this, some basic
knowledge about matrix manipulation is required.
First, the matrix pattern is built from the (linear) vectors thetaval and phival (containing the
measured values):

thetaval=thetaval';
pattern=thetaval*phival; %construct (theta row and phi column)
matrix

We also need to construct the corresponding theta and phi matrices (radians):

thetamat=theta'*ones(1,length(phi));
phimat=phi'*ones(1,length(theta));

181E - Matlab Introduction For Telecommunication 2002-09-19


4
And to flip the phi matrix:

phimat=phimat';

Converting spherical to rectangular coordinates (Balanis Eq. VII-11):

x=pattern.*sin(thetamat).*cos(phimat);
y=pattern.*sin(thetamat).*sin(phimat);
z=pattern.*cos(thetamat);

Below follows a couple of plotting functions that should be included in the function:

colormap(jet);
mesh(x,y,z);
xlabel('x');
ylabel('y');
zlabel('z');
axis vis3d
grid on

Remember to save, and then it’s time to test the function! If you can see that thetaval and
phival are still in the Workspace, type:

antplot(thetaval, phival)

and press <enter>.

After some time, a Figure-window with a 3D-plot of the linear dipole’s pattern should
appear. A nice feature of the 3D-plot is that the viewing angle can be controlled. Click
Tools>Rotate 3D and click in the figure. A transparent box is used to control the viewing
angle.

About the local variables: as you can see no other variables, matrices etc. than that were
defined in the script ‘dipole’ can be seen in the Workspace. You can try by typing the
variable ‘x’ in the Command window, or by resting the cursor on any of the local variables in
‘antplot’. After the function has been executed, they cease to exist!

This part of the exercise is finished, and hopefully you will find your code useful when
taking the course Antenna Engineering. You can refine the ‘antplot’ function and also create
patterns for other antenna types.

181E - Matlab Introduction For Telecommunication 2002-09-19


5
5 LINK BUDGET
Create a MATLAB function that can calculate any unknown parameter in a link budget.

The equations of the link budget are:

Pt − L ft + Gt − L p + Gr − L fr − SNR − Br − Fr − N 0 = 0
where
EIRP = Pt − L ft + G and Pr = N 0 + Br + Fr + SNR

The variables represent:

Pt or Pr = Transmitted or Received Power


Lft or Lfr = Loss in transmitter or receiver antenna feeder cables
Gt or Gr = Antenna Gain (relative to an isotropic antenna)
Lp = Path loss (including the fading margin)
SNR = the required Signal to Noise Ratio
Br = Receiver Bandwidth
Fr = Noise Figure of the receiver
No = Background Noise Floor

All calculations shall be performed in logarithmic numbers and all power levels should be
represented in dBm.

Suggested function:

1. Choose unknown (i.e. ask which parameter to calculate)


2. Ask for the known variables
3. Present the result

If you want to make the function more advanced you can allow for linear and logarithmic
input data and let the function do the calculations.

181E - Matlab Introduction For Telecommunication 2002-09-19


6

Anda mungkin juga menyukai