% Simulate the performance of binary modulation (BPSK) and compare it with
% the theoretical expression of the probability of a bit error. (This script
% needs the file 'Q.m' which is also available in the web site.)
%
% This program is complementary material for the book: The Art of Error
% Correcting Coding, R.H. Morelos-Zaragoza, 2nd ed., Wiley, 2006.
% ISBN: 0470 01558 6
%
% This and other programs are available at http://the-art-of-ecc.com
%
% You may use this program for academic and personal purposes only.
% If this program is used to perform simulations whose results are
% published in a journal or book, please refer to the book above.
%
% The use of this program in a commercial product requires explicit
% written permission from the author. The author is not responsible or
% liable for damage or loss that may be caused by the use of this program.
%
% Copyright (c) 2006. Robert H. Morelos-Zaragoza. All rights reserved.
clear % Clear all variables in Matlab's workspace
seed = 75291; % This allows to reproduce results
rand('state',seed)
randn('state',seed)
Nsim = 1000000; % Number of simulated bits
i = 1; % Index for array of results
P0 = 1/2; % Probability of bit = "0"
for EsNo=0:1:10 % Loop over energy-to-noise ratio
error = 0; % Error counter
for n = 1:Nsim
No = 10^(-EsNo/10);
sigma= sqrt(No/2); % Standard deviation of the AWGN
if (rand < P0), M = 0; % Generate a random bit
else M = 1; end
A = (-1)^M; % Mapping: "0" --> 1, "1" --> -1
Y = A + sigma*randn; % Add noise to transmitted symbol
if Y > 0, Mhat = 0; % Decision device
else Mhat = 1; end
if Mhat ~= M, error = error+1; end % Count errors, if any
end % Nsim
snr(i) = EsNo;
ber(i) = error/Nsim;
fprintf('%3.1f \t %10.7e\n', snr(i), ber(i));
i = i + 1;
end % EsNo
% Theoretical bir error probability of BPSK modulation
perr = Q(sqrt(2*10.^(snr/10)));
% Plotting commands
figure(1)
semilogy(snr,ber,'-*r')
hold on
semilogy(snr,perr,'-ob')
legend('Simulated','Theory');
title('Error performance of binary transimission over an AWGN channel');
xlabel('E_b/N_0 (dB)');
ylabel('Bit error rate');
grid on
hold off