Cm_sm52.m
Upload User: loeagle
Upload Date: 2013-03-02
Package Size: 1236k
Code Size: 1k
Development Platform:

Matlab

  1. function [p]=cm_sm52(snr_in_dB)
  2. % [p]=cm_sm52(snr_in_dB)
  3. % CM_SM52  Returns the probability of error for the given
  4. %    value of snr_in_dB, signal-to-noise ratio in dB.
  5. N=10000;
  6. Eb=1;
  7. d=1;   
  8. snr=10^(snr_in_dB/10);      % signal-to-noise ratio per bit
  9. sgma=sqrt(Eb/(2*snr));       % noise variance
  10. phi=0;
  11. % Generation of the data source follows.
  12. for i=1:N,
  13.   temp=rand;    % a uniform random variable between 0 and 1
  14.   if (temp<0.5),
  15.     dsource(i)=0;
  16.   else
  17.     dsource(i)=1;
  18.   end;
  19. end;
  20. % detection and the probability of error calculation
  21. numoferr=0;
  22. for i=1:N,
  23.   % demodulator output
  24.   if (dsource(i)==0),
  25.     r0c=sqrt(Eb)*cos(phi)+gngauss(sgma);
  26.     r0s=sqrt(Eb)*sin(phi)+gngauss(sgma);
  27.     r1c=gngauss(sgma);
  28.     r1s=gngauss(sgma);
  29.   else
  30.     r0c=gngauss(sgma);
  31.     r0s=gngauss(sgma);
  32.     r1c=sqrt(Eb)*cos(phi)+gngauss(sgma);
  33.     r1s=sqrt(Eb)*sin(phi)+gngauss(sgma);
  34.   end;
  35.   % square-law detector outputs
  36.   r0=r0c^2+r0s^2;
  37.   r1=r1c^2+r1s^2;
  38.   % Decision is made next.
  39.   if (r0>r1),
  40.     decis=0;
  41.   else
  42.     decis=1;
  43.   end;
  44.   % If the decision is not correct the error counter is increased.
  45.   if (decis~=dsource(i)),
  46.     numoferr=numoferr+1;
  47.   end;
  48. end;
  49. p=numoferr/(N);