%rayleighdemo.m
DATA=round(rand(1,10000));
TX=[];
a=1;
%Let the bandwidth of the complex base band signal be W/2
W=2;
N0=0.01;
N0W=(N0/2)*W;
for i=1:1:length(DATA)
if(DATA(i)==0)
 TX=[TX a 0];
else
TX=[TX 0 a];
end
end
%Gaussian noise with variance 0.01
g=sqrt(N0W)*randn(1,20000)+j*sqrt(N0W)*randn(1,20000);
%Flat-fading rayleigh channel with impulse response r
r=sqrt(0.1)*randn(1,20000)+j*sqrt(0.1)*randn(1,20000);
RX=r.*TX+g;
figure
subplot(2,2,1)
stem(TX(1:1:200))
title('Typical transmitted samples')
subplot(2,2,2)
[a,b]=hist(sqrt(abs(r)),100)
plot(b,a)
title(’histogram of the rayleigh distributed noise’)
subplot(2,2,3)
plot(real(RX(1:1:200)))
title(’Real part of the received samples’)
 subplot(2,2,4)
plot(imag(RX(1:1:200)))
title(’Imaginary part of the received samples’)
%detection
DETDATA=[];
for i=1:2:200
temp=[RX(i) RX(i+1)];
O=abs(temp(1))-abs(temp(2));
if(O>0)
DETDATA=[DETDATA 0];
else
DETDATA=[DETDATA 1];
end
end
figure
subplot(2,1,1)
stem(DATA(1:1:100))
title(’Transmitted data’)
subplot(2,1,2)
stem(DETDATA,’r’)
  title(’Corresponding detected data’)
