MATLAB code

Document Sample
MATLAB code Powered By Docstoc
					48771 Communication Theory
Assignment 2 - 2000 Autumn
Homework #2A


                                 Homework #2A
    Page 226 Contemporary Communication Systems using MATLAB by Proakis and Salehi


1. PROBLEM 5.10
1.1.    MATLAB code
1.1.1. ct510.m

%   HW #2A
%   Question 5.10
%   file name: ct510.m
%   source: bdt_f527.m
%   modified by Steve Ma on 25/05/2000

%   assume E=1,   var=N/2 thus E/N=SNR=E/(2*var)
%   at var=0.1,   SNR=5    ie +7 dB
%   at var=1.0,   SNR=0.5 ie -3 dB
%   at var=2.0,   SNR=0.25 ie -6 dB

% MATLAB script that generates the theoretical bit-error probability
% versus the signal to noise ratio from the Monte Carlo simulation

clear all;
close all;
initial_snr=-7;
final_snr=8;
snr_step=1;
tolerance=1e-7;            % tolerance used for the integration
minus_inf=-20;             % this is practically -infinity
plus_inf=20;               % this is practically +infinity
snr_in_dB=initial_snr:snr_step:final_snr;

for i=1:length(snr_in_dB),
   snr=10^(snr_in_dB(i)/10);
   theo_err_prb(i)=(2/3)*quad8('bdt_int',minus_inf,plus_inf,tolerance,[],snr,4);
end;

% Plotting commands follow
figure(1)
title('P_e vs SNR in dB'), xlabel('SNR (dB)'), ylabel('P_e');
semilogy(snr_in_dB,theo_err_prb),axis([-7,8,10^-4,10^0]);
hold on;

% find the theoretical bit-error probability at different variance
theo_var0 = theo_err_prb(15)
theo_var1 = theo_err_prb(5)
theo_var2 = theo_err_prb(2)




By Steve Ma 97074506
Printed: 28/09/2012
                                          Page 7
48771 Communication Theory
Assignment 2 - 2000 Autumn
Homework #2A

% MATLAB script that generates the estimate of bit-error probability
% versus the signal to noise ratio from the Monte Carlo simulation
% source: ip_05_10.m

SNRindB=[+7,-3,-6];

for i=1:length(SNRindB),
   % simulated error rate from smldp510.m for 10000 symbols
   % bit error probability estimate
   smld_err_prb(i)=smldP510(SNRindB(i));
end;

% Plotting commands follow
semilogy(SNRindB,smld_err_prb,'*'),axis([-7,8,10^-4,10^0]);

% find the experimental bit-error probability at different variance
smld_var0 = smld_err_prb(1)
smld_var1 = smld_err_prb(2)
smld_var2 = smld_err_prb(3)

% errors between theoretical and experimental error
error_var0 = theo_var0 - smld_var0
error_var1 = theo_var1 - smld_var1
error_var2 = theo_var2 - smld_var2



1.2. Associated .m files
1.2.1. smldp510.m

function [p]=smldP510(snr_in_dB)
% [p]=smldP510(snr_in_dB)
%           SMLDP510 simulates the probability of error for the given
%           snr_in_dB, signal to noise ratio in dB.
M=4;                                % quarternary orthogonal signalling
E=1;

SNR=exp(snr_in_dB*log(10)/10);      % signal to noise ratio per bit
sgma=sqrt(E^2/(4*SNR));             % sigma, standard deviation of noise
N=10000;                            % number of symbols being simulated
% generation of the quarternary data source
for i=1:N,
  temp=rand;                        % a uniform random variable over (0,1)
  if (temp<0.25),
    dsource1(i)=0;
    dsource2(i)=0;
  elseif (temp<0.5),
    dsource1(i)=0;
    dsource2(i)=1;
  elseif (temp<0.75),
    dsource1(i)=1;
    dsource2(i)=0;



By Steve Ma 97074506
Printed: 28/09/2012
                                      Page 8
48771 Communication Theory
Assignment 2 - 2000 Autumn
Homework #2A

  else
     dsource1(i)=1;
     dsource2(i)=1;
  end
end;
% detection, and probability of error calculation
numoferr=0;
for i=1:N,
  % matched filter outputs
  if ((dsource1(i)==0) & (dsource2(i)==0)),
     r0=sqrt(E)+gngauss(sgma);
     r1=gngauss(sgma);
     r2=gngauss(sgma);
     r3=gngauss(sgma);
  elseif ((dsource1(i)==0) & (dsource2(i)==1)),
     r0=gngauss(sgma);
     r1=sqrt(E)+gngauss(sgma);
     r2=gngauss(sgma);
     r3=gngauss(sgma);
  elseif ((dsource1(i)==1) & (dsource2(i)==0)),
     r0=gngauss(sgma);
     r1=gngauss(sgma);
     r2=sqrt(E)+gngauss(sgma);
     r3=gngauss(sgma);
  else
     r0=gngauss(sgma);
     r1=gngauss(sgma);
     r2=gngauss(sgma);
     r3=sqrt(E)+gngauss(sgma);
  end;
  % the detector
  max_r=max([r0 r1 r2 r3]);
  if (r0==max_r),
     decis1=0;
     decis2=0;
  elseif (r1==max_r),
     decis1=0;
     decis2=1;
  elseif (r2==max_r),
     decis1=1;
     decis2=0;
  else
     decis1=1;
     decis2=1;
  end;
  % count the number of bit errors made in this decision
  if (decis1~=dsource1(i)),    % if it is an error, increase the error counter
     numoferr=numoferr+1;
  end;
  if (decis2~=dsource2(i)),    % if it is an error, increase the error counter
     numoferr=numoferr+1;
  end;
end;
p=numoferr/(2*N);              % bit error probability estimate


By Steve Ma 97074506
Printed: 28/09/2012
                                      Page 9
48771 Communication Theory
Assignment 2 - 2000 Autumn
Homework #2A


1.2.2. bdt_int.m

function [y] = bdt_int(x,snr_per_bit,M)
% [y]=bdt_int(x,snr_per_bit,M)
%           BDT_INT is used to compute the integrand needed in the
%           computation of the error probability of orthogonal signals

N=length(x);
for i=1:N,
   y(i)=(1/sqrt(2*pi))*(1-(1-Qfunct(x(i)))^(M-1))*exp(-(x(i)-
sqrt(2*log2(M)*snr_per_bit))^2/2);
end;



1.2.3. gngauss.m

function [gsrv1,gsrv2]=gngauss(m,sgma)
% [gsrv1,gsrv2]=gngauss(m,sgma)
% [gsrv1,gsrv2]=gngauss(sgma)
% [gsrv1,gsrv2]=gngauss
%     GNGAUSS generates two independent Gaussian random variables with mean
%     m and standard deviation sgma.
%     If one of the input arguments is missing
%     it takes the mean as 0, and the standard deviation as the given parameter.
%     If neither mean nor the variance is given,
%     it generates two standard Gaussian random variables.
if nargin == 0,
  m=0; sgma=1;
elseif nargin == 1,
  sgma=m; m=0;
end;
u=rand;                              % a uniform random variable in (0,1)
z=sgma*(sqrt(2*log(1/(1-u))));       % a Rayleigh distributed random variable
u=rand;                              % another uniform random variable in (0,1)
gsrv1=m+z*cos(2*pi*u);
gsrv2=m+z*sin(2*pi*u);



1.2.4. 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));




By Steve Ma 97074506
Printed: 28/09/2012
                                     Page 10
48771 Communication Theory
Assignment 2 - 2000 Autumn
Homework #2A

1.3. MATLAB outputs

» ct510


theo_var0 =

     0.0015


theo_var1 =

     0.2145


theo_var2 =

     0.2981


smld_var0 =

     0.0016


smld_var1 =

     0.2180


smld_var2 =

     0.2972


error_var0 =

 -7.6670e-005


error_var1 =

   -0.0035


error_var2 =

  8.9455e-004




By Steve Ma 97074506
Printed: 28/09/2012
                             Page 11
          48771 Communication Theory
          Assignment 2 - 2000 Autumn
          Homework #2A

          The plot of theoretical and experimental bit-error probability (Pe) versus signal to noise ratio (SNR
          in dB) from Monte Carlo simulation of a digital communication system that employs M=4
          orthogonal signals are shown below:


                                                       Pe vs SNR in dB
     0
 10




     -1
 10




     -2
Pe




 10




     -3
 10




     -4
 10
               -6             -4             -2           0              2             4               6         8
                                                          SNR (dB)

                              Bit-error probability for M=4 orthogonal signals from a Monte Carlo
                                     simulation compared with theoretical error probability

          1.4. Comment

               Variance 
                          2
                                   Theoretical value   Experimental value         Error             Error in %
                   0.1                 0.0015                0.0016           -7.6670e-05            0.5111%
                   1.0                 0.2145                0.2180             -0.0035             -1.6317%
                   2.0                 0.2981                0.2972           8.9455e-04             0.3001%




          By Steve Ma 97074506
          Printed: 28/09/2012
                                                             Page 12
48771 Communication Theory
Assignment 2 - 2000 Autumn
Homework #2A


2. PROBLEM 5.12
2.1. MATLAB code
2.1.1. ct512.m

%   HW #2A
%   Question 5.12
%   file name: ct512.m
%   source: bdt_f531.m
%   modified by Steve Ma on 25/05/2000

%   assume E=1,   var=N/2 thus E/N=SNR=E/(2*var)
%   at var=0.1,   SNR=5    ie +7 dB
%   at var=1.0,   SNR=0.5 ie -3 dB
%   at var=2.0,   SNR=0.25 ie -6 dB

% MATLAB script that generates the theoretical symbol-error probability
% versus the signal to noise ratio from the Monte Carlo simulation

clear all;
close all;
initial_snr=-7;
final_snr=8;

initial_snr=-7;
final_snr=8;
snr_step=1;
tolerance=eps;             % tolerance used for the integration
plus_inf=20;               % this is practically +infinity
snr_in_dB=initial_snr:snr_step:final_snr;

for i=1:length(snr_in_dB),
   snr=10^(snr_in_dB(i)/10);
   theo_err_prb(i)=1-quad8('bdt_int2',0,plus_inf,tolerance,[],snr,4);
end;

% Plotting commands follow
figure(1)
title('P_e vs SNR in dB'), xlabel('SNR (dB)'), ylabel('P_e');
semilogy(snr_in_dB,theo_err_prb),axis([-7,8,10^-4,10^0]);
hold on;

% find the theoretical symbol-error probability at different variance
theo_var0 = theo_err_prb(15)
theo_var1 = theo_err_prb(5)
theo_var2 = theo_err_prb(2)




By Steve Ma 97074506
Printed: 28/09/2012
                                         Page 13
48771 Communication Theory
Assignment 2 - 2000 Autumn
Homework #2A

% MATLAB script that generates the estimate of symbol-error probability
% versus the signal to noise ratio from the Monte Carlo simulation
% source: ip_05_11.m

SNRindB=[+7,-3,-6];

for i=1:length(SNRindB),
   % simulated error rate from smldp511.m for 10000 symbols
   % symbol- error probability estimate
   smld_err_prb(i)=smldP511(SNRindB(i));
end;

% Plotting commands follow
semilogy(SNRindB,smld_err_prb,'*'), axis([-7,8,10^-4,10^0]);

% find the experimental symbol-error probability at different variance
smld_var0 = smld_err_prb(1)
smld_var1 = smld_err_prb(2)
smld_var2 = smld_err_prb(3)

% errors between theoretical and experimental error
error_var0 = theo_var0 - smld_var0
error_var1 = theo_var1 - smld_var1
error_var2 = theo_var2 - smld_var2



2.2. Associated .m files:
2.2.1. smldp511.m

function [p]=smldP511(snr_in_dB)
% [p]=smldP511(snr_in_dB)
%      SMLDP511 simulates the probability of error for the given
%      snr_in_dB, signal to noise ratio in dB, for the system
%      described in illustrated problem 11, Chapter 5.
M=4;                                 % quarternary orthogonal signalling
E=1;
SNR=exp(snr_in_dB*log(10)/10);       % signal to noise ratio per bit
sgma=sqrt(E^2/(4*SNR));              % sigma, standard deviation of noise
N=10000;                             % number of symbols being simulated
% generation of the quarternary data source
for i=1:N,
   temp=rand;                        % uniform random variable over (0,1)
   if (temp<0.25),
       dsource(i)=0;
   elseif (temp<0.5),
       dsource(i)=1;
   elseif (temp<0.75),
       dsource(i)=2;
   else
       dsource(i)=3;
   end
end;

By Steve Ma 97074506
Printed: 28/09/2012
                                      Page 14
48771 Communication Theory
Assignment 2 - 2000 Autumn
Homework #2A

% detection, and error probability computation
numoferr=0;
for i=1:N,
   % The matched filter outputs
   if (dsource(i)==0)
      r0=sqrt(E)+gngauss(sgma);
      r1=gngauss(sgma);
   elseif (dsource(i)==1)
      r0=gngauss(sgma);
      r1=sqrt(E)+gngauss(sgma);
   elseif (dsource(i)==2)
      r0=-sqrt(E)+gngauss(sgma);
      r1=gngauss(sgma);
   else
      r0=gngauss(sgma);
      r1=-sqrt(E)+gngauss(sgma);
   end;
   % detector follows
   if (r0>abs(r1)),
      decis=0;
   elseif (r1>abs(r0)),
      decis=1;
   elseif (r0<-abs(r1)),
      decis=2;
   else
      decis=3;
   end;
   if (decis~=dsource(i)),    % if it is an error, increase to the error counter
      numoferr=numoferr+1;
   end;
end;
p=numoferr/N;                             % bit error probability estimate



2.2.2. bdt_int2.m

function [y] = bdt_int2(x,snr_per_bit,M)
% [y]=bdt_int2(x,snr_per_bit,M)
%           BDT_INT2   is used to compute the integrand in the expression for
%           the error probability of biorthogonal signals.

E=1;
Eb=E/log2(M);
sgma=Eb*sqrt(log2(M)/(2*snr_per_bit));
N=length(x);
for i=1:N,
   y(i)=(1-2*Qfunct(x(i)/sgma))^(M/2-1)*(1/(sqrt(2*pi)*sgma))*exp(-(x(i)-
E)^2/(2*sgma^2));
end;




By Steve Ma 97074506
Printed: 28/09/2012
                                     Page 15
48771 Communication Theory
Assignment 2 - 2000 Autumn
Homework #2A


2.2.3. gngauss.m

function [gsrv1,gsrv2]=gngauss(m,sgma)
% [gsrv1,gsrv2]=gngauss(m,sgma)
% [gsrv1,gsrv2]=gngauss(sgma)
% [gsrv1,gsrv2]=gngauss
%     GNGAUSS generates two independent Gaussian random variables with mean
%               m and standard deviation sgma.
%     If one of the input arguments is missing
%     it takes the mean as 0, and the standard deviation as the given parameter.
%     If neither mean nor the variance is given,
%     it generates two standard Gaussian random variables.
if nargin == 0,
  m=0; sgma=1;
elseif nargin == 1,
  sgma=m; m=0;
end;
u=rand;                              % a uniform random variable in (0,1)
z=sgma*(sqrt(2*log(1/(1-u))));       % a Rayleigh distributed random variable
u=rand;                              % another uniform random variable in (0,1)
gsrv1=m+z*cos(2*pi*u);
gsrv2=m+z*sin(2*pi*u);



2.2.4. 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));




By Steve Ma 97074506
Printed: 28/09/2012
                                     Page 16
48771 Communication Theory
Assignment 2 - 2000 Autumn
Homework #2A

2.3.     MATLAB outputs

» ct512

theo_var0 =

       0.0015


theo_var1 =

       0.2917


theo_var2 =

       0.4212


smld_var0 =

       0.0012


smld_var1 =

       0.2832


smld_var2 =

       0.4228


error_var0 =

  3.4475e-004


error_var1 =

       0.0085


error_var2 =

   -0.0016




By Steve Ma 97074506
Printed: 28/09/2012
                             Page 17
          48771 Communication Theory
          Assignment 2 - 2000 Autumn
          Homework #2A

          The plot of theoretical and experimental symbol-error probability (Pe) versus signal to noise ratio
          (SNR in dB) from Monte Carlo simulation of a digital communication system that employs M=4
          biorthogonal signals are shown below:

                                                        Pe vs SNR in dB
     0
 10




     -1
 10




     -2
Pe




 10




     -3
 10




     -4
 10
               -6              -4           -2             0              2           4             6       8
                                                           SNR (dB)


                               Symbol-error probability for M=4 biorthogonal signals from a Monte
                                  Carlo simulation compared with theoretical error probability



          2.4. Comment

                 Variance 
                           2
                                    Theoretical value    Experimental value      Error         Error in %
                     0.1                0.0015                 0.0012         3.4475e-04       22.3177%
                     1.0                0.2917                 0.2832           0.0085          2.8993%
                     2.0                0.4212                 0.4228           -0.0016        -0.3734%




          By Steve Ma 97074506
          Printed: 28/09/2012
                                                             Page 18
48771 Communication Theory
Assignment 2 - 2000 Autumn
Homework #2A


3. PROBLEM 5.12 (2ND PART)

Plot of 1000 received signal-plus-noise samples at the input to the detector for each value of 2.

3.1. MATLAB code
3.1.1. ct512r.m

%   HW #2A
%   Question 5.12 (2nd part)
%   file name: ct512r.m
%   source: smldP511.m
%   modified by Steve Ma on 25/05/2000
%   generates 1000 received signal-plus-noise samples
%   at the input to the detector for variance=0.1,1.0,2.0

clear all;
close all;
M=4;                                               % quarternary orthogonal signalling
E=1;
% at var=0.1, SNR=5    ie +7 dB
% at var=1.0, SNR=0.5 ie -3 dB
% at var=2.0, SNR=0.25 ie -6 dB

figure(1)
snr_in_dB=[7,-3,-6];

for i=1:length(snr_in_dB),
   subplot(3,1,i);
   xlabel('samples'), ylabel('energy');
   SNR(i)=exp(snr_in_dB(i)*log(10)/10); % signal to noise ratio per bit
   sgma=sqrt(E^2/(4*SNR(i)));           % sigma, standard deviation of noise
   N=1000;                              % number of rx signal+noise samples

    % generation of the quarternary data source
    for i=1:N,
       temp=rand;                        % uniform random variable over (0,1)
       if (temp<0.25),
          dsource(i)=0;
       elseif (temp<0.5),
          dsource(i)=1;
       elseif (temp<0.75),
          dsource(i)=2;
       else
          dsource(i)=3;
       end;
    end;




By Steve Ma 97074506
Printed: 28/09/2012
                                                Page 19
48771 Communication Theory
Assignment 2 - 2000 Autumn
Homework #2A

  for i=1:N,
      hold on;
      % plot using '+' to make the result significant
      % the matched filter outputs
      if (dsource(i)==0)
         r0=sqrt(E)+gngauss(sgma);
         r1=gngauss(sgma);
         plot(i,r0,'+');
      elseif (dsource(i)==1)
         r0=gngauss(sgma);
         r1=sqrt(E)+gngauss(sgma);
         plot(i,r1,'+');
      elseif (dsource(i)==2)
         r0=-sqrt(E)+gngauss(sgma);
         r1=gngauss(sgma);
         plot(i,r0,'+');
      else
         r0=gngauss(sgma);
         r1=-sqrt(E)+gngauss(sgma);
         plot(i,r1,'+');
      end;
   end;

end;



The distribution of the 1000 received signal-plus-noise samples at the input to the detector for each
value of 2 are shown on the next page.

It is interesting to realise that lower the variance (eg 2=0.1), that is higher the Signal to Noise
Ratio, forms distinct pattern on 1 on the energy axis; whilst higher the variance the distribution
scatter and has higher energy spread.




By Steve Ma 97074506
Printed: 28/09/2012
                                                Page 20
48771 Communication Theory
Assignment 2 - 2000 Autumn
Homework #2A


3.2. MATLAB output


                                                                                      = 0.1
                                                                                       2




                                                                                       = 1.0
                                                                                       2




                                                                                       = 2.0
                                                                                       2




4. COMMENTS

The Signal to Noise Ratio in dB (snr_in_dB) are calculated and rounded off manually, using:
                   SNR = E / N = E / 22         SNR (in dB) = 10 log10 (E / 22)
To obtain a more accurate results of the Monte Carlo simulation of the problems, a small section of
code is required to re-write, which has not been carried out due to the limitation of time.

Also the Monte Carlo simulation requires a long processing time, processor consumption and
computer memory, which executes the loop 10,000 times when performing the simulation for
10,000 symbols (20,000 bits). It takes, in average, over 15 minutes on a Pentium 200 MMX with 64
Mb RAM.


By Steve Ma 97074506
Printed: 28/09/2012
                                               Page 21

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:900
posted:9/29/2012
language:Unknown
pages:15