Professional Documents
Culture Documents
This exercise will guide you to realize the basic operating environment. Some
useful instructions will also be introduced. As MATLAB is executed, the
MATLAB desktop window will be shown as figure 1.
i j pi eps inf
nan realmax realmin length reshape
conj ones zeros tic、toc clock
profile plot stem awgn sin
sum mean fft clc clear
close figure for while switch
if size fliplr conv randn
rand
1
Part II: Constellation
0110 0010
0111 0011
1
0 0101 0001
0100 0000
Real Real Real
1100 1000
1101 1001
1 1111
0 1011
1110 1010
Part III:
Math Function
¾ Built-in constants:
pi
eps --- the smallest value MATLAB can handle
inf --- the largest value MATLAB can handle
¾ Built- in functions:
sqrt, sin, cos, sort, find, ....
2
Plots & 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 signal over AWGN channel’);
For loops
>> 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
3
MATLAB Files
¾ MATLAB script (executable) files (M-file):
Edit it in any text editor.
Save it with extension ‘.m’ (say ‘hello.m’)
Type file name (‘hello’) to start running.
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.
Exercise
Due Date: June 5, 2006.
Lab: EC634
You have to hand in the report on time. In your report, you should answer the
following three questions.
(a) Execute the attached MATLAB programs to perform estimate and plot the error
probability performance (bit error rate vs SNR(dB)) of a binary antipodal (BPSK)
communication system. In your figure, you should show 2 curves:
(i)The result by simulation.
(ii)The results by analysis.
Note your curve is (log scale in Y axis for BER and X axis for SNR).
(b) Understand the programs and explain why “sgma=E/sqrt(2*SNR)” is used in
subroutine bpsk_sim.m
(c) Modify the code to plot the error probability performance of a QPSK
communication system. In your figure, you should show the result by simulation.
4
Source code
BPSK.m
% MATLAB script for BPSK Performance in AWGN Channel (Simulation
and Theoretical Analysis)
SNRindB1=0:1:10;
SNRindB2=0:0.1:10;
echo on;
for i=1:length(SNRindB1),
% simulated error rate
smld_err_prb(i)=bpsk_sim(SNRindB1(i));
echo off;
end;
echo on;
for i=1:length(SNRindB2),
SNR=exp(SNRindB2(i)*log(10)/10);
% theoretical error rate
theo_err_prb(i)=Qfunct(sqrt(2*SNR));
echo off;
end;
echo on;
% Plotting commands follow
semilogy(SNRindB1,smld_err_prb,'*');
hold
semilogy(SNRindB2,theo_err_prb);
xlabel('Eb/N0');
ylabel('Pb');
bpsk_sim.m
function [p]=bpsk_sim(snr_in_dB)
%[p]=bpsk_sim(snr_in_dB)
%simulates the probability of error for the particular
%value of snr_in_dB, signal to noise ratio in dB.
E=1;
SNR=exp(snr_in_dB*log(10)/10); % signal to noise ratio
sgma=E/sqrt(2*SNR); % sigma, standard deviation of noise
N=10000;
% generation of the binary data source follows
for i=1:N,
5
temp=rand; % a uniform random variable over
(0,1)
if (temp<0.5),
dsource(i)=0; % with probability 1/2, source
output is 0
else
dsource(i)=1; % with probability 1/2, source output is
1
end
end;
% the detection, and probability of error calculation
for i=1:N,
% The matched filter outputs
if (dsource(i)==0),
r=-E+sgma*randn(1,1); % if the source output is "0"
else
r=E+sgma*randn(1,1); % if the source output is "1"
end;
% detector follows
if (r<0),
decis=0; % decision is "0"
else
decis=1; % decision is "1"
end;
if (decis~=dsource(i)), % if it is an error, increase the
error counter
numoferr=numoferr+1;
end;
end;
p=numoferr/N; % probability of error estimate
Qfunct.m
function [y]=Qfunct(x)
%[y]=Qfunct(x)
%QFUNCT evaluates the Q-function.
%y = 1/sqrt(2*pi) * integral from x to inf of exp(-t^2/2) dt.
%y = (1/2) * erfc(x/sqrt(2)).
y=(1/2)*erfc(x/sqrt(2));