You are on page 1of 2

# function exercise;

clear;

## EbN0dB = 0:0.5:8; % Eb/N0 range in dB for simulation

packet=rand(1,100000)>0.5;
c=CRC8(packet');
packet=[packet c'];
bpskModulated = 2*packet-1; %Mapping 0->-1 and 1->1
M=2; %Number of Constellation points M=2^k for BPSK k=1
Rm=log2(M); %Rm=log2(M) for BPSK M=2

BER = zeros(1,length(EbN0dB)); %Place holder for BER values for each Eb/N0
index=1;
for k=0:0.5:8;,
%-------------------------------------------
%Adding Channel Noise for various Eb/N0
%-------------------------------------------
%Adding noise with variance according to the required Eb/N0
EbN0 = 10.^(k/10); %Converting Eb/N0 dB value to linear scale
noiseSigma = sqrt(1./(2*Rm*EbN0)); %Standard deviation for AWGN Noise
noise = noiseSigma*randn(1,length(bpskModulated));
%Bit Error rate Calculation
BER(index) = sum(xor(packet,bitswithnoise))/length(packet);
index=index+1;
end
BER
%Plot commands
figure(1);
plotHandle=plot(EbN0dB,BER,'m--');
set(plotHandle,'LineWidth',1.5);
title('Bit error probability curve for BPSK modulation');
xlabel('SNR (Eb/N0) in dB');
ylabel('Bit Error Rate (BER) ');
grid on;
%Handling values lower than .001
%if BER <.001
% BER=.001;
%end
%BER=max(BER,.00001)

figure(2);
plotHandle=semilogy(EbN0dB,BER,'m--');
set(plotHandle,'LineWidth',1.5);
title('Bit error probability curve for BPSK modulation');
xlabel('SNR (Eb/N0) in dB');
ylabel('Bit Error Rate (BER)');
grid on;

%-----------------------------------------------------
% CRC Function
%-----------------------------------------------------
function check=CRC8(xa);
% xa is array of bits to be transmitted (column vector)
% Generates 8-bit CRC check with g(x) = x^8 + x^2 +x + 1
xae = [xa;0;0;0;0;0;0;0;0]; % Append 8 zeros to bit-stream
g8x = [1;0;0;0;0;0;1;1;1] ; % Generator polynomial
xsa=xae(1:9);
for i=1:length(xa)
if xsa(1) == g8x(1), xsa = xor(xsa,g8x); end;
xsa(1:8)=xsa(2:9);
if i<length(xa) xsa(9)=xae(i+9); end;
end;
check = xsa(1:8); % 8 bit CRC column vector
return;
% -------------------------------------------------------