Code covered by the BSD License  

Highlights from
Stop and Wait ARQ with CRC 4

Stop and Wait ARQ with CRC 4

by

 

Automatic Repeat Request using Cyclic Redundancy Check-4 as error detection mechanism

snwwithcrc4.m
%stop n wait ARQ with CRC-4 detection

clc;
close all;

%exp=input('Enter the number of experiments');
%m=input('Enter no. of frames : ');
m=10;
%n=input('Enter frame length : ');
n=7;
%p=input('Enter probability of error of the tx channel');
%tpt=randi([0,1],m,n);
%delay=randi([0,1],m,n);
%p=input('Enter probability of error of the tx channel');
%timeout=input('Enter max no. of transmissions per frame:');
timeout=12;
%rtd=input('Enter round trip delay(in sec):');
rtd=.125;
%rate1=input('Enter data rate of transmission medium(in Mbps):');
rate1=2;
rate=rate1*1000000;

%fprintf('The message is:');msg;
exp=input('Enter the no. of times exp to be done: ');

 
 error=0;
 
for x=1:200
    p(x)=x*.0005;
msg=randi([0,1],m,n);
%Cyclic Redundancy Check coding of the data packets

div=[1 0 0 1];         %predetermined divisor
for i=1:m
    [q,r]=deconv(msg(i,:),div);
   y(i,:)=[msg(i,:),zeros(1,3)];
   for k=1:n
           r(k)=mod(r(k),2);
   end
   fcs=[zeros(1,3),r];     %frame check sequence
   
   pac(i,:)=bitxor(y(i,:),fcs);
   clear r;clear rem;
end


%fec encoding 

%fprintf('The transmitted message with parity bits is:');msgtx
xtra=0;
for c=1:exp
%stop n wait protocol
extra=0;
pass=0;

%msgrx=msgtx;
j=1;
time=0;

while(j<=m)
    pass=pass+1;
        
        msgrx(j,:)=bsc(pac(j,:),p(x));
        %fprintf('Pass %d :',pass);
        %fprintf('Transmitted message:');msgtx(j,:)
        %fprintf('Received message:');msgrx(j,:)
        
        [q2,r2]=deconv(msgrx(j,:),div);
           r2(1,:)=mod(r2(1,:),2);
    if r2==0
       temp1(1,j)=0;
    else
        temp1(1,j)=1;
    end
        if(temp1(1,j)==0)
            j=j+1;
            time=0;
        elseif(time<timeout)
           extra=extra+1;
           time=time+1;
        %else
            %j=j+1;
            %extra=1000;
        end
end
xtra=xtra+extra;
%error calculation
for d=1:m
    if(msgrx(d,:)==pac(d,:))
    else
        error=error+1;
    end
end

end
xtra1=xtra/exp;    
%Throughput efficiency calculation based on extra
Tp=rtd/2;
Ts=(n+3)/rate;
pdr=Tp/Ts;
factor=1/(1+2*pdr);

tpt(x)=((m/(m+xtra1))*n*factor)/(n+3);
tpt1(x)=(((1-p(x))^n)*n*factor)/(n+1);
Tt=(2*Tp)+Ts;
delay(x)=((m+xtra1)/m)*Tt;
delay1(x)=Tt/((1-p(x))^n);


end

subplot(2,2,1)   
plot(p,tpt)
xlabel('BER');
ylabel('Throughput');


subplot(2,2,2)   
plot(p,tpt1)
xlabel('BER');
ylabel('Theoritical Throughput');

subplot(2,2,3)
plot(p,delay)
xlabel('BER');
ylabel('Delay');

subplot(2,2,4)
plot(p,delay1)
xlabel('BER');
ylabel('Theoritical Delay');
%fprintf('Frames erroneously received=%d/%d \n',error,exp*m);







Contact us