Está en la página 1de 15

Introduction to Matlab

Reference Books:
MATLAB -- high-performance numeric computation and
visualization software
Contemporary Communication Systems Using MATLAB
Internet Web Resources:
http://www.mathworks.com/products/
http://www.math.utah.edu/lab/ms/matlab/matlab.html
http://www-h.eng.cam.ac.uk/help/tpl/programs/matlab.html
Dr. C. N.
Georghiades

ELEN 489: Digital Communication


Lab

Introduction To Matlab

Interpreted language
Efficient in manipulating vectors and matrices
Slow in handling loops
Case sensitive

To start Matlab in our Unix system,


eesun3/home1/somebody> matlab5
On-line assistance:
help
lookfor

type

Other useful commands:


whos size
clear
Dr. C. N.
Georghiades

dir

cd

ELEN 489: Digital Communication


Lab

Matlab: Vector & Matrices


Creation of variables:
>> v = zeros(1, 5)
Notation
>> m = ones(2, 2)
Notationand
andoperations
operations follow
follow
>> v = [1 2 4 8 16]
linear
linearalgebra
algebraconventions
conventions
>> w = [ 0: 0.5 : 3 ]
Transpose: v
>> m = [0.1 0.3 ; 0.2 3] Transpose: v

Call for values:


>> v ( or v(:) )
>> v(5)
>> w(2:4)
>> m(1,2)
>> m(:,1)
Dr. C. N.
Georghiades

Inverse:
inv(m)
Inverse:
inv(m)
Eigenvalue/vector
Eigenvalue/vector decomposition:
decomposition:
[[v,v,ee]]==eig(m)
eig(m)
Diagonal:
diag(m)
Diagonal:
diag(m)
Determinant:
det(m)
Determinant:
det(m)

ELEN 489: Digital Communication


Lab

Matlab: Vector & Matrices (cont)


Dimensionality must agree !
+ - * / \
Matrix operation B*A <> A*B, so Matlab has:
right division / : A/B = A * inv(B)
left division \ : A\B = inv(A) * B

* vs .*

/ vs

./

IfIfthe
thevector/matrixs
vector/matrixsdimensionality
dimensionalityisistoo
toolarge
large(say
(say100000),
100000),
cut
cutititinto
intosegments
segmentsto
toavoid
avoidmemory
memoryoverflow
overflowand
andwriting
writingto
todisk
disk
which
whichslows
slowsprocessing.
processing.
Dr. C. N.
Georghiades

ELEN 489: Digital Communication


Lab

Matlab: Math Functions


Built-in constants:
pi
eps -- smallest value Matlab can handle
inf -- largest value Matlab can handle
Built-in functions (small sample)
sqrt
sin
cos
exp
log2
log10 log
max
min
avg
abs
erfc
BesselJ
filter
freqz fft
ifft
rand
randn
find
sort
sum
prod
conv
Dr. C. N.
Georghiades

norm

Functions can
operate
on vectors

spectrum

ELEN 489: Digital Communication


Lab

Matlab: Plots and Figures


>> dB = 0 : 0.1 : 10;
>> SNR = 10.^ ( dB./10);
>> Pb = 0.5*erfc(sqrt(SNR));
>> semilogy (dB, Pb);
>> % same as: plot(dB, log10(Pb));
>> grid on;
>> xlabel( SNR (dB));
>> ylabel(Bit error rate);
>> title(Performance of antipodal signaling over AWGN channel);

Other
Othercommands:
commands:(check
(checkhelp)
help)
figure
figure axis
axis stem
stem bar
bar legend
legend hold
hold
Dr. C. N.
Georghiades

ELEN 489: Digital Communication


Lab

Matlab: Plots and Figures (cont)


Several plots in one figure:

One plot:

>> x= [0:0.05:2*pi];
>> figure(1);
>> subplot(1,2,1); plot(x,sin(x));
>> title(sin);grid on;axis([0 2*pi -1 1]);
>> subplot(1,2,2); plot(x,cos(x));
>> titel(cos);grid on;axis([0 2*pi -1 1]);

>> x= [0:0.05:2*pi];
>> figure(2);
>> plot(x,sin(x),--,x,cos(x),:);
>> legend(sin, cos);
>> grid on;
>> axis([0 2*pi -1 1]);

Dr. C. N.
Georghiades

ELEN 489: Digital Communication


Lab

Matlab: Loops (for)


>> B =[ [1 2 3] [3 2 1] [2 1 3]];
>> for j=2:3,
for i=j:3,
B(i,:) = B(i,:)-B(j-1,:)*B(i,j-1)/B(j-1,j-1);
end;
end;

B= 1 3 2
2 2 1
3 1 3
B= 1 3 2
0 -4 -3
0 0 3

Tips:
* Avoid loops.
Use reasonable vector operation instead if possible.
* Make operations inside loops as few as possible.
Dr. C. N.
Georghiades

ELEN 489: Digital Communication


Lab

Matlab: Loops (while)


errno=0;
% initialization
frameno=0;
N=1000;
% frame length
% simulate for at least 50 errors
while errno < 50,
frameno = frameno+1; % increase frameno
% transmit another frame,
x = zeros(1,N)>0.5;
..
% detect and count all the errors
..
errno = errno+xor(y,x); % accumulate errors
end;
Pe = errno/N/frameno; % error probability
Dr. C. N.
Georghiades

ELEN 489: Digital Communication


Lab

break command:
terminates
execution of
while or for
loop.
. loops,
In nested
break exits
from the
innermost
loop only.

Matlab: If Statement
>> % check for determinant
>> % before do inverse
>> if det(A) ~= 0,
B = inv(A);
end;

Dr. C. N.
Georghiades

if end;
if else end;
if elseif elseif else end;
allow nested if statements.

ELEN 489: Digital Communication


Lab

10

Matlab: Matlab Files


Matlab
MatlabScript
Script(executable)
(executable)files
files((M-file
M-file):):
edit
editititininany
anyplain
plaintext
texteditor
editor(pico,
(pico,vi,
vi,emacs,
emacs,notepad
notepad
))
save
saveititwith
withextention
extention.m
.m (say
(saymyfile.m
myfile.m))
type
typefile
filename
name(myfile)
(myfile)totostart
startrunning
running

..

Matlab
Matlabdata
datafile
file((mat-file
mat-file):):
>>
>>save
saveresult.mat
result.matdB,
dB,Pb
Pb
.
.
>>
>>load
loadresult.mat
result.mat
>>
>>whos
whos

..
%%save
savevariables
variablesdB
dBand
andPb
Pbininresult.mat
result.mat
%%load
loadfile
file
%%check
checkwhat
whatvariables
variablesare
areloaded
loaded

Note:

Matlab execution files must end with .m.


Likewise, data files end with .mat.
Be sure to be in the appropriate directory to access files.

Dr. C. N.
Georghiades

ELEN 489: Digital Communication


Lab

11

Matlab: Subroutines / Functions


function [x] = gaussElim(A,b)
% Gaussian Elimination
% A:matrix for left hand side
% b:vector for right hand side

Save as
N=max(size(A));
gaussElim.m
for j = 2:N,
for k = k:N,
m = A(k,j-1)/A(j-1,j-1);
A(k,:) = A(k,:)-A(j-1,:)*m;
b(k) = b(k)-m*b(j-1);
end;
end;
x=zeros(N,1);
x(N)=b(N)/A(N,N);
for k=N-1:-1:1,
x(k)=(b(k)A(k,k+1:N)*x(k+1:N)/A(k,k);
end;
Dr. C. N.
Georghiades

>>A = [1 2 3 6; 4 3 2 3;
9 9 1 -2; 4 2 2 1];
>>B = [1 2 1 4];
>>C = gaussElim(A,B);
A=1 2 3 6 B=1 C= 0.6809
433 3
2
-0.8936
9 9 1 -2
1
1.8085
422 1
4
-0.5532
When you want to repeat a
sequence of commands with
different variables as input
Too many subroutines may slow
down performance

ELEN 489: Digital Communication


Lab

12

Matlab: Simulation Example (I)


% Performance of Antipodal Signaling
n_sig=[1, 2, 3, 5, 8, 20, 40, 80, 200];
errNo=zeros(1,9); err=zeros(1,9);
for dB=0:8,
N=n_sig(dB+1)*1000; % No of sigs to be sent
a=rand(1,N)>0.5;
% random info seq. (1,0)
s=a*2-1;
% modulate to (1, -1)
v=1/sqrt(2*10^(dB/10));
awgn=randn(1,N)*v;
% Gaussian noise
r=s+awgn;
% received seq.
a_hat=r>0;
% detection
errNo(dB+1)=sum(xor(a_hat,a)); % count errors
end;
err=errNo ./ n_sig/1000; % err rate (simulated)
snr=10.^ ( [0:0.1:8]./10);
Pb = 0.5*erfc(sqrt(snr)); % err rate (analytical)
semilogy([0:8],err,*, [0:0.1:8], Pb,- );
% plot
grid on; xlabel(dB); ylabel(err rate);
title(Simulation of antipodal signaling);
Dr. C. N.
Georghiades

For a given SNR, we usually


fix the signal energy and change
the noise level.
Simulate for at least 30 errors
to draw reliable conclusions

ELEN 489: Digital Communication


Lab

13

Matlab: Simulation Example (II)


% Matched filter detection
dB=6;
% 100 samples / signal
fs=100;
% sinc signal
tt=linspace(-2, 2, fs);
s=sin(pi*tt)./tt;
% energy normalization
s=s/sqrt(sum(s.^2));
% antipodal signaling
x0=s;
x1=-s;
% matched filter
H=s;

Dr. C. N.
Georghiades

% Gaussian noise
v= 1/sqrt(2*10^(dB/10);
AWGN=randn(1,fs)*v;
% add noise
y0=x0+AWGN;
y1=x1+AWGN;
% pass through matched filter
z0=filter(y0,1,H);
z1=filter(y1,1,H);
% reconstruct original signal
x0_hat=sign(z0(fs))*s;
x1_hat=sign(z1(fs))*s;

ELEN 489: Digital Communication


Lab

14

Matlab: Simulation Example (II) (cont)


0.2

0.2

0.15

0.15

x0

0.1

-0.05

x1

-0.05

x0_hat

0.1

-0.1
0.05

-0.15

-0.15

-0.2
-2

-1.5

-1

-0.5

0.5

1.5

-2

-0.2
-1.5

-1

-0.5

0.5

1.5

-2

-1.5

-1

-0.5

AWGN

0.5

1.5

-2

0.8

1.2

0.4

0.6

0.2

y0

y1

0.4

-1.5

-1

-0.5

0.5

1.5

Reconstruct signal

0.8
0.6

x1_hat

-0.1

0.05

0.8
0

0.4

0.6

0.2

-0.2

0.2

z0

0.4

0
0

0.2

-0.2

-0.4

-0.2

z1

-0.6
0

-0.4

-0.4

-0.8

-0.2

-0.6

-0.6
-0.8
-2

-1.5

-1

-0.5

0.5

1.5

-0.8
-2

-0.4
-2

-1.5

-1

-0.5

0.5

1.5

-1.5

-1

-0.5

0.5

1.5

-1
-2

-1.5

-1

-0.5

0.5

1.5

0.2

0. 15

0.1

Matched Filter
0. 05

-2

Dr. C. N.
Georghiades

-1.5

-1

-0.5

0.5

1.5

ELEN 489: Digital Communication


Lab

15

También podría gustarte