0% found this document useful (0 votes)
25 views

Code As of Now

The document describes a simulation of wireless communication between nodes using CSMA/CA. Random node positions are generated and the source and destination nodes are defined. Audio data is segmented and transmitted from the source node to the destination node using RTS, CTS and ACK frames while avoiding collisions through backoff timers.

Uploaded by

Shreyas Shreyas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Code As of Now

The document describes a simulation of wireless communication between nodes using CSMA/CA. Random node positions are generated and the source and destination nodes are defined. Audio data is segmented and transmitted from the source node to the destination node using RTS, CTS and ACK frames while avoiding collisions through backoff timers.

Uploaded by

Shreyas Shreyas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 104

clc;

clear all;
close all;
%Get the time when we start computations:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Number of nodes
numnodes = 20;
%Field Dimensions - x and y maximum (in meters)
xm=250;
ym=250;
%Assign the coordinates for each node randomly
for i=1:1:numnodes
XR(i)=rand(1,1)*xm;
YR(i)=rand(1,1)*ym;
End
% this part is replaced
%Plot the coordinates on an X-Y Plane
figure,
hold on
for i=1:numnodes
plot(XR(i),YR(i),'kx',...
'MarkerSize',9,'LineWidth',2);
txt=[' Node: ' num2str(i) ]; %To insert the node number adjacent to the
node
text(XR(i),YR(i),txt);
end
% Figure Specifications
xlim([0 250]);
ylim([0 250]);
xlabel('x-range in metres -->');
ylabel('y-range in metres -->');
title('Positions of the Nodes');
grid on;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Define the source and destination node
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

global srcnode;
global destnode;
srcnode=input('Enter the Source Node (1-20): ')
destnode=input('Enter the Destination Node (1-20): ')

bandwidth=20*10^6; % Bandwidth Supported by 802.11b - 20 MHz

% Defining the location of all the nodes to specific variables


loc1=[XR(1),YR(1)];
loc2=[XR(2),YR(2)];
loc3=[XR(3),YR(3)];
loc4=[XR(4),YR(4)];
loc5=[XR(5),YR(5)];
loc6=[XR(6),YR(6)];
loc7=[XR(7),YR(7)];
loc8=[XR(8),YR(8)];
loc9=[XR(9),YR(9)];
loc10=[XR(10),YR(10)];
loc11=[XR(11),YR(11)];
loc12=[XR(12),YR(12)];
loc13=[XR(13),YR(13)];
loc14=[XR(14),YR(14)];
loc15=[XR(15),YR(15)];
loc16=[XR(16),YR(16)];
loc17=[XR(17),YR(17)];
loc18=[XR(18),YR(18)];
loc19=[XR(19),YR(19)];
loc20=[XR(20),YR(20)];
locsrc=[XR(srcnode),YR(srcnode)];
locdest=[XR(destnode),YR(destnode)];

p1 = [XR(srcnode) YR(srcnode)]; % Source Point


p2 = [XR(destnode) YR(destnode)]; % Destination Point
dp = p2-p1; % Difference/Distance
figure(1);
hold on;
for i=1:numnodes % Plot the arrow mark from source to destination node
plot(XR(i),YR(i),'kx',...
'MarkerSize',9,'LineWidth',2);
end
xlim([0 250]);
ylim([0 250]);
xlabel('x-range in metres -->');
ylabel('y-range in metres -->');
title('Positions of the Nodes');
quiver(p1(1),p1(2),dp(1),dp(2),0)
grid on;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Accepting Audio at the Source Application Layer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

disp('Please select the input Audio File that is in .wav format');


[file,path]=uigetfile('*.wav','Select Audio file');
[Z,Fs] = audioread([path,file],'native');
figure;
plot(Z);
xlabel('Time');
ylabel('Amplitude');
grid on;
disp('Playing the input audio file');
load handel.mat;
player=audioplayer(Z,5.5*Fs);
play(player);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CSMA/CA
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
channelstate=0; % Channel State is initially Idle
K=0; % No. of attempts to be made for channel access by a particular node
R=randi([1 31]); % Random value to between 1 to 2^k-1
RTS=0; % Initial value of RTS
CTS=0; % Initial value of CTS
RTF=0; % Initial value of RTF

% Initializing other required variables


Tfr=1;
n=0;
w=1;
relaynodenum=0;
srcbtimer=2;
dict=0;
RRTTFF=0;
notmyRTF=0;
rts_framebin=0;
rtf_framebin=0;
cts_framebin=0;

% Algorithm to set a Node that will collide with the source node for channel
access
sensenode=srcnode+1;
if(sensenode==destnode)
sensenode=destnode+1;
end
if(sensenode>20)
sensenode=srcnode-1;
end
sensebtimer=1; % Backoff timer of Sense Node
K=K+1;
d=1;

channel_state(srcnode,destnode,K,R,Tfr,srcbtimer,sensebtimer,sensenode,channel
state,rts_framebin,rtf_framebin,cts_framebin,XR(1),YR(1),XR(2),YR(2),XR(3),YR(
3),XR(4),YR(4),XR(5),YR(5),XR(6),YR(6),XR(7),YR(7),XR(8),YR(8),XR(9),YR(9),XR(
10),YR(10),XR(11),YR(11),XR(12),YR(12),XR(13),YR(13),XR(14),YR(14),XR(15),YR(1
5),XR(16),YR(16),XR(17),YR(17),XR(18),YR(18),XR(19),YR(19),XR(20),YR(20),XR(sr
cnode),YR(srcnode),XR(destnode),YR(destnode),locsrc,locdest,RRTTFF,notmyRTF,w,
n,Z,relaynodenum,dict,d)

function
channel_state(srcnode,destnode,K,R,Tfr,srcbtimer,sensebtimer,sensenode,channel
state,rts_framebin,rtf_framebin,cts_framebin,XR1,YR1,XR2,YR2,XR3,YR3,XR4,YR4,X
R5,YR5,XR6,YR6,XR7,YR7,XR8,YR8,XR9,YR9,XR10,YR10,XR11,YR11,XR12,YR12,XR13,YR13
,XR14,YR14,XR15,YR15,XR16,YR16,XR17,YR17,XR18,YR18,XR19,YR19,XR20,YR20,XRsrc,Y
Rsrc,XRdest,YRdest,locsrc,locdest,RRTTFF,notmyRTF,w,n,Z,relaynodenum,dict,d)

if(sensebtimer>srcbtimer)
channelstate=0;
disp('Collision Avoided!! Channel Access gained by Source Node');
else
channelstate=1;
disp('Collision Occured');

channeldenied(srcnode,destnode,K,R,Tfr,srcbtimer,sensebtimer,sensenode,channel
state,rts_framebin,rtf_framebin,cts_framebin,XR1,YR1,XR2,YR2,XR3,YR3,XR4,YR4,X
R5,YR5,XR6,YR6,XR7,YR7,XR8,YR8,XR9,YR9,XR10,YR10,XR11,YR11,XR12,YR12,XR13,YR13
,XR14,YR14,XR15,YR15,XR16,YR16,XR17,YR17,XR18,YR18,XR19,YR19,XR20,YR20,XRsrc,Y
Rsrc,XRdest,YRdest,locsrc,locdest,RRTTFF,notmyRTF,w,n,Z,relaynodenum,dict,d)
end

if(channelstate==0)
disp('Channel is IDLE');
% Timer for sec to check if channel is idle
disp('Channel is IDLE');
disp('Channel Occupied by the Source Node')
disp('Wait for SIFS');
% SIFS timer
channelstate=1;
testno=1;
data_out=0;
N=2048*8;
z=1;
r=(length(Z)/N);
rr=round(r,0);
if (r>rr)
rr=rr+1;
end
TimeTCP=0
NoPacketsDropped=0;
FinalAudio=0;
% Since source node has gained access, control given to source node from
now
while(testno<=rr)
data_out =
sourcenode(srcnode,destnode,rts_framebin,rtf_framebin,cts_framebin,XR1,YR1,XR2
,YR2,XR3,YR3,XR4,YR4,XR5,YR5,XR6,YR6,XR7,YR7,XR8,YR8,XR9,YR9,XR10,YR10,XR11,YR
11,XR12,YR12,XR13,YR13,XR14,YR14,XR15,YR15,XR16,YR16,XR17,YR17,XR18,YR18,XR19,
YR19,XR20,YR20,XRsrc,YRsrc,XRdest,YRdest,locsrc,locdest,RRTTFF,notmyRTF,w,n,Z,
relaynodenum,dict,0,d,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr);
data_out=data_out;
% for i=1:1:2048*8+127+104
% FinalData(1,z)=data_out(1,i);
% z=z+1;
% end
testno=testno+1;
end

end
end
function
channeldenied(srcnode,destnode,K,R,Tfr,srcbtimer,sensebtimer,sensenode,channel
state,rts_framebin,rtf_framebin,cts_framebin,XR1,YR1,XR2,YR2,XR3,YR3,XR4,YR4,X
R5,YR5,XR6,YR6,XR7,YR7,XR8,YR8,XR9,YR9,XR10,YR10,XR11,YR11,XR12,YR12,XR13,YR13
,XR14,YR14,XR15,YR15,XR16,YR16,XR17,YR17,XR18,YR18,XR19,YR19,XR20,YR20,XRsrc,Y
Rsrc,XRdest,YRdest,locsrc,locdest,RRTTFF,notmyRTF,w,n,Z,relaynodenum,dict,d)
if(K<5)
TB=R*Tfr; % Backoff timer
pause(TB*10^-6);
K=K+1;
sensebtimer=4;

channel_state(srcnode,destnode,K,R,Tfr,srcbtimer,sensebtimer,sensenode,channel
state,rts_framebin,rtf_framebin,cts_framebin,XR1,YR1,XR2,YR2,XR3,YR3,XR4,YR4,X
R5,YR5,XR6,YR6,XR7,YR7,XR8,YR8,XR9,YR9,XR10,YR10,XR11,YR11,XR12,YR12,XR13,YR13
,XR14,YR14,XR15,YR15,XR16,YR16,XR17,YR17,XR18,YR18,XR19,YR19,XR20,YR20,XRsrc,Y
Rsrc,XRdest,YRdest,locsrc,locdest,RRTTFF,notmyRTF,w,n,Z,relaynodenum,dict,d)
end

end
function
data_out=sourcenode(srcnode,destnode,RTS_Frame,rtf_framebin,cts_framebin,XR1,Y
R1,XR2,YR2,XR3,YR3,XR4,YR4,XR5,YR5,XR6,YR6,XR7,YR7,XR8,YR8,XR9,YR9,XR10,YR10,X
R11,YR11,XR12,YR12,XR13,YR13,XR14,YR14,XR15,YR15,XR16,YR16,XR17,YR17,XR18,YR18
,XR19,YR19,XR20,YR20,XRsrc,YRsrc,XRdest,YRdest,locsrc,locdest,RRTTFF,notmyRTF,
w,n,Z,relaynodenum,dict,IP_Packet,ACK_Frame,testno,data_out,NoPacketsDropped,T
imeTCP,FinalAudio,rr)
data_out=data_out;
if (srcnode~=0)
d=0;
snodenum=srcnode;
dnodenum=destnode;
CTS_Frame=0;
RTF_Frame=0;
Mod=0;
IP_Packet=0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Segmentation of Data at TCP Layer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

N=2048*8;
r=(length(Z)/N);
rr=round(r,0);
if (r>rr)
rr=rr+1;
end
t=1;
TCP_Segment=zeros(rr,2048*8+127);
for q=1:1:rr
n=n+N;
if (q==rr||q==rr)
S=[];
temp=Z(w:length(Z),1);
L=Z(w:length(Z),1)';
s=2048*8-length(L);
S=[L zeros(1,s)];
Z(w:n,1)=S';
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% TCP SEGMENT FRAME FORMAT AT SOURCE:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
t_srcpaddrln=2*8; %Source port address length
t_srcpaddrb=dec2bin(52100,t_srcpaddrln)-'0'; %Source port address

t_destpaddrln=2*8; %Destination port address length


t_destpaddrb=dec2bin(1036,t_destpaddrln)-'0'; %Destination port
address in bin

t_seqnoln=4*8; %Sequence number length


t_seqnob=dec2bin(w,t_seqnoln)-'0'; %Sequence number of the byte in Bin

t_acknoln=4*8; %Acknowledgement number length


t_acknob=dec2bin(0,t_acknoln)-'0'; %Acknowledgement number at the
Source in bin

t_hlenln=4; %HLEN length


t_HLENb=dec2bin(5,t_hlenln)-'0'; %Header length in binary = 5*4=20

t_reservedln=6; %Reserved bit length


t_reservedb=dec2bin(0,t_reservedln)-'0'; %Reserved bits value in bin

t_ackfln=1; %ACK flag length (Is ACK valid?)


t_ackfb=dec2bin(0,t_ackfln)-'0'; %ACK flag at source in bin

t_pshfln=1; %Request for Push flag length


t_pshfb=dec2bin(0,t_pshfln)-'0'; %Request for Push flag in bin

t_rstfln=1; %Reset the connection flag length


t_rstfb=dec2bin(0,t_rstfln)-'0'; %Reset the connection flag in bin

t_synfln=1; %SYN flag length


t_synfb=dec2bin(0,t_synfln)-'0'; %SYN flag in bin

t_finfln=1; %Terminate the connection flag length


t_finfb=dec2bin(0,t_finfln)-'0'; %Terminate the connection flag in bin

t_windsizeln=2*8; %Window size length


t_windsizeb=dec2bin(4,t_windsizeln)-'0'; %Window size in bin

% TCP Header in binary format


TCP_Header=[t_srcpaddrb t_destpaddrb t_seqnob t_acknob t_HLENb
t_reservedb t_ackfb t_pshfb t_rstfb t_synfb t_finfb t_windsizeb];

% TCP Segment with it's header attached


l=w;
for i=1:1:2048*8+127
if (i<128)
TCP_Segment(t,i)=TCP_Header(1,i);
end
if(i>=128 && l<=n+127)
TCP_Segment(t,i)=Z(l,1);
l=l+1;
end
end
t=t+1;
w=w+2048*8;
end

NoPacketSent=rr;
PacketLen=256;
TimeTCP=clock;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% IP Datagram Format At Source:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ip_verln=4; %IP Version number length
ip_verb=dec2bin(4,ip_verln)-'0'; %IP Version Number in bin

ip_hlenln=4; %IP header length


ip_hlenb=dec2bin(9,ip_hlenln)-'0'; %IP header length in bin

ip_totallnln=2*8; %Total length of the header+data length


ip_totallnb=dec2bin(104+127+2048,ip_totallnln)-'0'; %Total length in bin

ip_ttlln=1*8; %time to live length


ip_ttlb=dec2bin(2,ip_ttlln)-'0'; %Time to live in bin

ip_protocolln=1*8; %protocol length


ip_protocolb=dec2bin(6,ip_protocolln)-'0'; %protocol TCP 06 in bin

ip_srcaddrln=4*8; %Source IP address length

srcipaddress=[0;0;0;0;0;0;0;0;0;0;0;0;0;1;0;0;0;0;0;0;0;1;0;1;1;1;1;1;1;1;1;1]
;
srcipaddressbin=srcipaddress';

ip_destaddrln=4*8; %Destination IP Address length

destipaddress=[0;0;0;0;0;0;0;0;0;0;0;0;0;1;0;0;0;0;0;0;0;1;0;1;1;1;1;0;1;1;0;0
];
destipaddressbin=destipaddress';

IP_Header=[ip_verb ip_hlenb ip_totallnb ip_ttlb ip_protocolb


srcipaddressbin destipaddressbin];

t=1;
l=w;
IP_Packet=zeros(rr,2048*8+127+104);
TxRate1=clock;
for i=1:1:rr
for j=1:1:104
IP_Packet(i,j)=IP_Header(1,j);
end
end
for i=1:1:rr
for j=1:1:2048*8+127
IP_Packet(i,j+104)=TCP_Segment(i,j);
end
end
TxRate2=clock;

Transmission_rate=(2048*8+127+104)/(TxRate2(6)-TxRate1(6));

PacketLength=2048*8+127+104;
NoPacketsTx=rr;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MAC Layer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

m_fcln=2*8; %Frame control of MAC length

m_fcpvln=2; %Protocol version in FC length


m_fcpvb=dec2bin(0,m_fcpvln)-'0'; %Protocol version

m_fctypeln=2; %Type of FC length


m_fctypeb=dec2bin(2,m_fctypeln); %The Type of FC is data here hence 10

% m_fcsubtypeln=4; %Subtype of FC length


% m_fcsubtype=dec2bin(11,m_fcsubtypeln); %Here Subtype of FC is RT

m_Dln=2*8; %Duration of NAV length


TxDelay=PacketLength/Transmission_rate; %Transmission Delay
EucDist=sqrt((XRdest-XRsrc)^2+(YRdest-YRsrc)^2); %Distance between the
source node and destination node
PropDelay=EucDist/(3*10^8); %Propagation delay
ProcessDelay=0.1*10^-6*PacketLength; %Processing delay
m_D=2*(TxDelay+PropDelay+ProcessDelay)/10^-3; %Total NAV time or the total
round trip time
m_D=dec2bin(m_D,m_Dln)-'0'; %Duration of NAV in milliseconds

m_srcpaddrln=2*8; %Source port address length


srcmacaddrhex=strcat('A2',':','34',':','45',':','11',':','92',':','F1'); %
Source MAC address assumed in hex
srcmacaddrascii=double(srcmacaddrhex); %Source MAC address in ascii
srcmacaddrb=dec2bin(srcmacaddrascii)-'0'; %in bin
srcmacaddrbin=reshape(srcmacaddrb,1,119);
m_srcmacaddrbin=[zeros(1,9) srcmacaddrbin];

m_destpaddrln=2*8; %Destination port address length


destmacaddrhex=strcat('A2',':','34',':','45',':','11',':','92',':','F3');
% Source MAC address assumed in hex
destmacaddrascii=double(destmacaddrhex); %Source MAC address in ascii
destmacaddrb=dec2bin(destmacaddrascii)-'0'; %in bin
destmacaddrbin=reshape(destmacaddrb,1,119);
m_destmacaddrbin=[zeros(1,9) destmacaddrbin];

% MAC_Header=[m_fcpvb m_fctypeb m_D m_srcmacaddrbin m_destmacaddrbin];

% t=1;
% MAC_Frame=zeros(rr,2048+127+104+);
% for i=1:1:rr
% for j=1:1:
% MAC_Frame(i,j)=MAC_Header(1,j);
% end
% end
% for i=1:1:rr
% for j=1:1:2048+127+104
% MAC_Frame(i,j+)=IP_Packet(i,j);
% end
% end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Source Node Defining RTS Frame format at MAC Layer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rts_fcln=2*8; %Frame control of RTS length

rts_fcpvln=2; %Protocol version in FC length


rts_fcpvb=dec2bin(0,rts_fcpvln); %Protocol version

rts_fctypeln=2; %Type of FC length


rts_fctypeb=dec2bin(2,rts_fctypeln); %The Type of FC is data here hence 10

rts_fcsubtypeln=4; %Subtype of FC length


rts_fcsubtype=dec2bin(11,rts_fcsubtypeln); %Here Subtype of FC is RTS

rts_fcretryln=1; %Retransmission at FC length


rts_fcretryb=dec2bin(0,rts_fcretryln); %Retransmission at FC

rts_Dln=2*8; %Duration of NAV length


rts_D=2*(TxDelay+PropDelay+ProcessDelay)/10^-3; %Total NAV time or the
total round trip time
rts_D=dec2bin(rts_D,rts_Dln);

rts_srcaddrln=6*8; %Source address length


rts_srcaddrhex=strcat('A2','34','45','11','92','F1'); %Source MAC address
assumed in hex
rts_srcaddrascii=double(rts_srcaddrhex); %Source MAC address in ascii
rts_srcaddrb=dec2bin(rts_srcaddrascii); %in bin
rts_srcaddr=mat2str(rts_srcaddrb); %in bin
rts_addrbin=(regexprep(rts_srcaddr,'[,;]''',''));
rts_srcaddrbin=strrep(rts_addrbin,'''','');

rts_destaddrln=6*8; %destination address length


rts_destaddrhex=strcat('B2',':','34',':','55',':','10',':','22',':','10');
%Destination MAC address assumed in hex
rts_destaddrascii=double(rts_destaddrhex); %Destination MAC address in
ascii
rts_destaddrb=dec2bin(rts_destaddrascii); %in bin
rts_destaddr=mat2str(rts_destaddrb); %in bin
rts_addrbin=(regexprep(rts_destaddr,'[,;]''',''));
rts_destaddrbin=strrep(rts_addrbin,'''','');

rts_locaddrln=4*8; %Location address length


rts_locaddr=[XRsrc,YRsrc]; %Location address in the x-y plane
rts_locaddrbin=[dec2bin((XRsrc),2*8),dec2bin((YRsrc),2*8)]; %Location
address in the x-y plane

%Final definition of the RTS Frame

RTS_Frame=strcat(rts_fcpvb,rts_fctypeb,rts_fcsubtype,rts_fcretryb,rts_D,rts_sr
caddrbin,rts_destaddrbin,rts_locaddrbin);

% Source Node Broadcasting RTS to all other Nodes in the Network


testno=testno;
disp('Broadcasting RTS to all the Nodes in the Network');

node1(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR1,YR1,XRsrc,YRsrc,IP_P
acket,Mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr
);

node2(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR2,YR2,XRsrc,YRsrc,IP_P
acket,Mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr
);

node3(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR3,YR3,XRsrc,YRsrc,IP_P
acket,Mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr
);

node4(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR4,YR4,XRsrc,YRsrc,IP_P
acket,Mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr
);

node5(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR5,YR5,XRsrc,YRsrc,IP_P
acket,Mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr
);

node6(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR6,YR6,XRsrc,YRsrc,IP_P
acket,Mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr
);

node7(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR7,YR7,XRsrc,YRsrc,IP_P
acket,Mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr
);

node8(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR8,YR8,XRsrc,YRsrc,IP_P
acket,Mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr
);

node9(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR9,YR9,XRsrc,YRsrc,IP_P
acket,Mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr
);

node10(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR10,YR10,XRsrc,YRsrc,I
P_Packet,Mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio
,rr);

node11(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR11,YR11,XRsrc,YRsrc,I
P_Packet,Mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio
,rr);

node12(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR12,YR12,XRsrc,YRsrc,I
P_Packet,Mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio
,rr);

node13(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR13,YR13,XRsrc,YRsrc,I
P_Packet,Mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio
,rr);

node14(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR14,YR14,XRsrc,YRsrc,I
P_Packet,Mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio
,rr);

node15(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR15,YR15,XRsrc,YRsrc,I
P_Packet,Mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio
,rr);

node16(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR16,YR16,XRsrc,YRsrc,I
P_Packet,Mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio
,rr);

node17(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR17,YR17,XRsrc,YRsrc,I
P_Packet,Mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio
,rr);

node18(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR18,YR18,XRsrc,YRsrc,I
P_Packet,Mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio
,rr);

node19(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR19,YR19,XRsrc,YRsrc,I
P_Packet,Mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio
,rr);

node20(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR20,YR20,XRsrc,YRsrc,I
P_Packet,Mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio
,rr);

destinationnode(srcnode,destnode,RTS_Frame,CTS_Frame,RTF_Frame,XR1,YR1,XR2,YR2
,XR3,YR3,XR4,YR4,XR5,YR5,XR6,YR6,XR7,YR7,XR8,YR8,XR9,YR9,XR10,YR10,XR11,YR11,X
R12,YR12,XR13,YR13,XR14,YR14,XR15,YR15,XR16,YR16,XR17,YR17,XR18,YR18,XR19,YR19
,XR20,YR20,XRsrc,YRsrc,XRdest,YRdest,locsrc,locdest,IP_Packet,Mod,dict,notmyRT
F,relaynodenum,d,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr);

elseif(relaynodenum~=0)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Physical Layer Operations
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
DataToTx=zeros(1,2048*8+127+104);
for i=1:1:2048*8+127+104
DataToTx(1,i)=IP_Packet(testno,i);
end
disp('Encoding Data at the Source using Huffman Encoding');
adpc = audio_encoder(DataToTx); % Additive Differential Pulse Code
[DataToTx,dict]=encoding(adpc(:));
l=length(DataToTx);
indata(1:l)= DataToTx;
DataToTx=indata;
M=16;

disp('Modulating the Data Fragment at Source');


Mod=qammod(DataToTx,M);

disp('Rayleigh Fading From Source to Relay');


z=length(Mod);
for k=1:z
% noise_sr = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% h_sr = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% y_sr(k) = h_sr(k).*Mod(k)+noise_sr(k);
%
end
% Mod=y_sr;
IP_Packet=IP_Packet;
srcmod=Mod;
disp('Broadcasting Data to the Relay Node');

data_out=destinationnode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,IP_Packet,Mod,dict,notmyRTF,r
elaynodenum,srcmod,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr);
if (relaynodenum==1)

node1(0,0,0,0,0,0,0,0,0,IP_Packet,Mod,dict,0,testno,data_out,NoPacketsDropped,
TimeTCP,FinalAudio,rr);
elseif (relaynodenum==2)

node2(0,0,0,0,0,0,0,0,0,IP_Packet,Mod,dict,0,testno,data_out,NoPacketsDropped,
TimeTCP,FinalAudio,rr);
elseif (relaynodenum==3)

node3(0,0,0,0,0,0,0,0,0,IP_Packet,Mod,dict,0,testno,data_out,NoPacketsDropped,
TimeTCP,FinalAudio,rr);
elseif (relaynodenum==4)

node4(0,0,0,0,0,0,0,0,0,IP_Packet,Mod,dict,0,testno,data_out,NoPacketsDropped,
TimeTCP,FinalAudio,rr);
elseif (relaynodenum==5)

node5(0,0,0,0,0,0,0,0,0,IP_Packet,Mod,dict,0,testno,data_out,NoPacketsDropped,
TimeTCP,FinalAudio,rr);
elseif (relaynodenum==6)

node6(0,0,0,0,0,0,0,0,0,IP_Packet,Mod,dict,0,testno,data_out,NoPacketsDropped,
TimeTCP,FinalAudio,rr);
elseif (relaynodenum==7)
node7(0,0,0,0,0,0,0,0,0,IP_Packet,Mod,dict,0,testno,data_out,NoPacketsDropped,
TimeTCP,FinalAudio,rr);
elseif (relaynodenum==8)

node8(0,0,0,0,0,0,0,0,0,IP_Packet,Mod,dict,0,testno,data_out,NoPacketsDropped,
TimeTCP,FinalAudio,rr);
elseif (relaynodenum==9)

node9(0,0,0,0,0,0,0,0,0,IP_Packet,Mod,dict,0,testno,data_out,NoPacketsDropped,
TimeTCP,FinalAudio,rr);
elseif (relaynodenum==10)

node10(0,0,0,0,0,0,0,0,0,IP_Packet,Mod,dict,0,testno,data_out,NoPacketsDropped
,TimeTCP,FinalAudio,rr);
elseif (relaynodenum==11)

node11(0,0,0,0,0,0,0,0,0,IP_Packet,Mod,dict,0,testno,data_out,NoPacketsDropped
,TimeTCP,FinalAudio,rr);
elseif (relaynodenum==12)

node12(0,0,0,0,0,0,0,0,0,IP_Packet,Mod,dict,0,testno,data_out,NoPacketsDropped
,TimeTCP,FinalAudio,rr);
elseif (relaynodenum==13)

node13(0,0,0,0,0,0,0,0,0,IP_Packet,Mod,dict,0,testno,data_out,NoPacketsDropped
,TimeTCP,FinalAudio,rr);
elseif (relaynodenum==14)

node14(0,0,0,0,0,0,0,0,0,IP_Packet,Mod,dict,0,testno,data_out,NoPacketsDropped
,TimeTCP,FinalAudio,rr);
elseif (relaynodenum==15)

node15(0,0,0,0,0,0,0,0,0,IP_Packet,Mod,dict,0,testno,data_out,NoPacketsDropped
,TimeTCP,FinalAudio,rr);
elseif (relaynodenum==16)

node16(0,0,0,0,0,0,0,0,0,IP_Packet,Mod,dict,0,testno,data_out,NoPacketsDropped
,TimeTCP,FinalAudio,rr);
elseif (relaynodenum==17)

node17(0,0,0,0,0,0,0,0,0,IP_Packet,Mod,dict,0,testno,data_out,NoPacketsDropped
,TimeTCP,FinalAudio,rr);
elseif (relaynodenum==18)

node18(0,0,0,0,0,0,0,0,0,IP_Packet,Mod,dict,0,testno,data_out,NoPacketsDropped
,TimeTCP,FinalAudio,rr);
elseif (relaynodenum==19)

node19(0,0,0,0,0,0,0,0,0,IP_Packet,Mod,dict,0,testno,data_out,NoPacketsDropped
,TimeTCP,FinalAudio,rr);
elseif (relaynodenum==20)
node20(0,0,0,0,0,0,0,0,0,IP_Packet,Mod,dict,0,testno,data_out,NoPacketsDropped
,TimeTCP,FinalAudio,rr);
end

end

end
function
data_out=destinationnode(snodenum,dnodenum,RTS_Frame,CTS_Frame,RTF_Frame,XR1,Y
R1,XR2,YR2,XR3,YR3,XR4,YR4,XR5,YR5,XR6,YR6,XR7,YR7,XR8,YR8,XR9,YR9,XR10,YR10,X
R11,YR11,XR12,YR12,XR13,YR13,XR14,YR14,XR15,YR15,XR16,YR16,XR17,YR17,XR18,YR18
,XR19,YR19,XR20,YR20,XRsrc,YRsrc,XRdest,YRdest,locsrc,locdest,IP_Packet,mod,di
ct,notmyRTF,relaynodenum,srcmod,testno,data_out,NoPacketsDropped,TimeTCP,Final
Audio,rr)
destaddrhex=strcat('B2',':','34',':','55',':','10',':','22',':','10');
%Destination MAC address assumed in hex
destaddrascii=double(destaddrhex); %Destination MAC address in ascii
destaddrb=dec2bin(destaddrascii); %in bin
destaddr=mat2str(destaddrb); %in bin
addrbin=(regexprep(destaddr,'[,;]''',''));
destaddrbin=strrep(addrbin,'''','');

% To check if this is the destination node mentioned by the source node


b=0;
if(RTS_Frame~=0)
d=0;
i=112;
j=1;
n=121;
for k=1:n
if(RTS_Frame(i)==destaddrbin(j))
b=b+1;
end
i=i+1;
j=j+1;
end
if(b==121)
disp('RTS Recieved at the Destination Node From Source Node');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Defining CTS Frame Format at Destination MAC Layer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

cts_fcln=2*8; %Frame control of CTS length

cts_fcpvln=2; %Protocol version in FC length


cts_fcpv=dec2bin(0,cts_fcpvln); %Protocol version in FC

cts_fctypeln=2; %Type of FC length


cts_fctypeb=dec2bin(2,cts_fctypeln); %The Type of FC is data here hence 10

cts_fcsubtypeln=4; %Subtype of FC length


cts_fcsubtype=dec2bin(12,cts_fcsubtypeln); %Here Subtype of FC is CTS
hence 1100

cts_fcretryln=1; %Retransmission at FC length


cts_fcretryb=dec2bin(0,cts_fcretryln); %Retransmission at FC

cts_Dln=2*8; %Duration of NAV length


g=1;
for len=10:1:25
cts_D(g)=RTS_Frame(len); %Duration of NAV
g=g+1;
end

cts_destaddrln=6*8; %destination address length


cts_destaddr=destaddrbin; %in bin

cts_datarateln=4*8; %Data rate length

Pt=10; %Transmission Power in dB


alpha=3; %Define the value of alpha used in the signal power calculation
eucdist=sqrt((XRdest-XRsrc)^2+(YRdest-YRsrc)^2);
So=Pt*eucdist^(-alpha); %Signal Power at the node in
No=10^-6; %Noise power in
SNR=So/No;
datarate=20*10^6*log2(1+So/No); %Compute the Data Rate of the Destination
node by using the Shannon's Channel Capacity Theorem
dataratebin=dec2bin(datarate,cts_datarateln); %Data rate in binary

%Final definition of the CTS Frame

CTS_Frame=strcat(cts_fcpv,cts_fctypeb,cts_fcsubtype,cts_fcretryb,cts_D,cts_des
taddr,dataratebin);
RTS_Frame=0;

disp('Broadcasting CTS Frame to all Near-by Nodes in the Network from


Destination Node');

backoff1=node1(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR1,YR1,XRdest,
YRdest,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,Fi
nalAudio,rr);

backoff2=node2(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR2,YR2,XRdest,
YRdest,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,Fi
nalAudio,rr);

backoff3=node3(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR3,YR3,XRdest,
YRdest,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,Fi
nalAudio,rr);

backoff4=node4(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR4,YR4,XRdest,
YRdest,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,Fi
nalAudio,rr);

backoff5=node5(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR5,YR5,XRdest,
YRdest,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,Fi
nalAudio,rr);

backoff6=node6(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR6,YR6,XRdest,
YRdest,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,Fi
nalAudio,rr);

backoff7=node7(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR7,YR7,XRdest,
YRdest,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,Fi
nalAudio,rr);

backoff8=node8(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR8,YR8,XRdest,
YRdest,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,Fi
nalAudio,rr);

backoff9=node9(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR9,YR9,XRdest,
YRdest,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,Fi
nalAudio,rr);

backoff10=node10(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR10,YR10,XRd
est,YRdest,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTC
P,FinalAudio,rr);

backoff11=node11(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR11,YR11,XRd
est,YRdest,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTC
P,FinalAudio,rr);

backoff12=node12(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR12,YR12,XRd
est,YRdest,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTC
P,FinalAudio,rr);

backoff13=node13(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR13,YR13,XRd
est,YRdest,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTC
P,FinalAudio,rr);

backoff14=node14(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR14,YR14,XRd
est,YRdest,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTC
P,FinalAudio,rr);

backoff15=node15(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR15,YR15,XRd
est,YRdest,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTC
P,FinalAudio,rr);

backoff16=node16(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR16,YR16,XRd
est,YRdest,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTC
P,FinalAudio,rr);

backoff17=node17(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR17,YR17,XRd
est,YRdest,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTC
P,FinalAudio,rr);

backoff18=node18(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR18,YR18,XRd
est,YRdest,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTC
P,FinalAudio,rr);
backoff19=node19(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR19,YR19,XRd
est,YRdest,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTC
P,FinalAudio,rr);

backoff20=node20(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR20,YR20,XRd
est,YRdest,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTC
P,FinalAudio,rr);

X=[backoff1 backoff2 backoff3 backoff4 backoff5 backoff6 backoff7 backoff8


backoff9 backoff10 backoff11 backoff12 backoff13 backoff14 backoff15 backoff16
backoff17 backoff18 backoff19 backoff20];

backoffnode=max(X);

CTS_Frame=0;
RTF_Frame=1;
if (backoffnode==backoff1)

node1(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR1,YR1,XRdest,YRdest,IP
_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,
rr);
elseif (backoffnode==backoff2)

node2(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR2,YR2,XRdest,YRdest,IP
_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,
rr);
elseif (backoffnode==backoff3)

node3(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR3,YR3,XRdest,YRdest,IP
_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,
rr);
elseif (backoffnode==backoff4)

node4(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR4,YR4,XRdest,YRdest,IP
_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,
rr);
elseif (backoffnode==backoff5)

node5(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR5,YR5,XRdest,YRdest,IP
_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,
rr);
elseif (backoffnode==backoff6)

node6(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR6,YR6,XRdest,YRdest,IP
_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,
rr);
elseif (backoffnode==backoff7)

node7(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR7,YR7,XRdest,YRdest,IP
_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,
rr);
elseif (backoffnode==backoff8)
node8(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR8,YR8,XRdest,YRdest,IP
_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,
rr);
elseif (backoffnode==backoff9)

node9(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR9,YR9,XRdest,YRdest,IP
_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,
rr);
elseif (backoffnode==backoff10)

node10(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR10,YR10,XRdest,YRdest
,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAud
io,rr);
elseif (backoffnode==backoff11)

node11(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR11,YR11,XRdest,YRdest
,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAud
io,rr);
elseif (backoffnode==backoff12)

node12(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR12,YR12,XRdest,YRdest
,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAud
io,rr);
elseif (backoffnode==backoff13)

node13(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR13,YR13,XRdest,YRdest
,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAud
io,rr);
elseif (backoffnode==backoff14)

node14(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR14,YR14,XRdest,YRdest
,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAud
io,rr);
elseif (backoffnode==backoff15)

node15(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR15,YR15,XRdest,YRdest
,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAud
io,rr);
elseif (backoffnode==backoff16)

node16(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR16,YR16,XRdest,YRdest
,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAud
io,rr);
elseif (backoffnode==backoff17)

node17(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR17,YR17,XRdest,YRdest
,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAud
io,rr);
elseif (backoffnode==backoff18)

node18(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR18,YR18,XRdest,YRdest
,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAud
io,rr);
elseif (backoffnode==backoff19)
node19(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR19,YR19,XRdest,YRdest
,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAud
io,rr);
elseif (backoffnode==backoff20)

node20(RTS_Frame,CTS_Frame,RTF_Frame,snodenum,dnodenum,XR20,YR20,XRdest,YRdest
,IP_Packet,mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAud
io,rr);
end
end
if(mod~=0)
M=16;
if(srcmod~=0)
SrcMod=srcmod;
demodSrc=qamdemod(SrcMod,M);
data_out=0;
else
tx_p=150e-3;
P0=(100e-3/500);
P1=(100e-3/500);
N0=10e-2;
z=length(mod);
M=16;
demod=qamdemod(mod,M);
xR=huffmandeco(demod,dict);
data_out = audio_decoder(xR);

for i=1:1:2048*8+127+104
DataToTx(1,i)=IP_Packet(testno,i);
end
adpc = audio_encoder(DataToTx); % Additive Differential Pulse Code
[DataToTx,dict]=encoding(adpc(:));
DataReceived=data_out';

[err_relay,ber] = biterr(DataToTx,demod);
if err_relay>525
data_out=0;
NoFramesDropped=NoPacketsDropped+1;
NoPacketsDropped=NoFramesDropped;
NoSegmentsDropped=NoPacketsDropped;
return;
else
NoFramesDropped=NoPacketsDropped;
NoPacketsDropped=NoFramesDropped;
NoSegmentsDropped=NoPacketsDropped;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Final Audio
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for i=1:1:2048*8
FinalAudio(i,testno)=DataReceived(i+127+104,1);
Final(i,testno)=DataReceived(i+127+104,1);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculations
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
TCPTime2=clock;
CommTime=TCPTime2(6)-TimeTCP(6);
if (testno==rr)
Throughput =
((testno-NoPacketsDropped)*(2048*8+127+104))/(CommTime*1000);
PacketLossRate = 1-(NoPacketsDropped)/rr;
disp(' ');
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%');
disp('Results: ');
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%');
disp(' ');
disp('1. The Throughput(kbps) of the final communication is: ')
display(Throughput);
disp('2. The number of frames with error is: ');
display(NoFramesDropped);
disp('3. The number of Packets Dropped is: ');
display(NoPacketsDropped);
disp('4. The Packet Loss Rate is: ');
display(PacketLossRate);
disp('5. The number of Segments Dropped is: ');
display(NoSegmentsDropped);
end

w=length(DataReceived);
n=2048*8+104+127;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%
% ACK at Destination Node
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%

ack_fcln=2*8; %Frame control of ACK length

ack_fcpvln=2; %Protocol version in FC length


ack_fcpv=dec2bin(0,ack_fcpvln); %Protocol version in FC

ack_fctypeln=2; %Type of FC length


ack_fctypeb=dec2bin(2,ack_fctypeln); %The Type of FC is data here
hence 10

ack_fcsubtypeln=4; %Subtype of FC length


ack_fcsubtype=dec2bin(13,ack_fcsubtypeln); %Here Subtype of FC is ACK
hence 1101

ack_fcretryln=1; %Retransmission at FC length


ack_fcretryb=dec2bin(0,ack_fcretryln); %Retransmission at FC
ack_fcwln=7;
ack_fcw=dec2bin(w+1,ack_fcwln);

ack_destaddrln=6*8; %destination address length

ack_destaddrhex=strcat('B2',':','34',':','55',':','10',':','22',':','10');
%Destination MAC address assumed in hex
ack_destaddrascii=double(ack_destaddrhex); %Destination MAC address in
ascii
ack_destaddrb=dec2bin(ack_destaddrascii); %in bin
ack_destaddr=mat2str(ack_destaddrb); %in bin
ack_addrbin=(regexprep(ack_destaddr,'[,;]''',''));
ack_destaddrbin=strrep(ack_addrbin,'''','');

ack_sequencenoln=4*8; % Next Sequence Number length


ack_seqno=dec2bin(n+1,ack_sequencenoln);

ACK_Frame=strcat(ack_fcpv,ack_fctypeb,ack_fcsubtype,ack_fcretryb,ack_fcw,ack_d
estaddrbin,ack_seqno);
disp('ACK Received at the Source Node from Destination Node');
IP_Packet=IP_Packet;
a=1;

sourcenode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,notmyRTF,0,0,0,relaynodenum,0,IP_Packet,A
CK_Frame,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr);
end

end

end
function
backoff1=node1(RTS,CTS,RTF,srcnode,destnode,XR1,YR1,XRsrc,YRsrc,IP_Packet,mod,
dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr)
nodenum1=1;
backoff1=0;
if nodenum1==srcnode || nodenum1==destnode %to avoid overlap of nodes
return;
end

if(RTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2); %Distance between the source
node and destination node
if (eucdist>100)
display('Node 1 is not in Range with Source Node');
return
else
%
node1macaddrhex=strcat('A4',':','34',':','55',':','10',':','22',':','10');
%Node MAC address assumed in hexnode1macaddrascii=double(node1macaddrhex);
%Source MAC address in ascii
% node1macaddrb=dec2bin(node1macaddrascii); %in bin
% node1macaddrbin=mat2str(node1macaddrb); %in bin
% node1loc=loc1; %location of the node
RTSframe=RTS;
return;
end
elseif(CTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2);
if (eucdist>100)
display('Node 1 is not in Range with Destination Node');
return
else
So=10*eucdist^-3; %Signal Power at the node in
No=10^-6; %Noise power in
SNR=So/No;
cc1=20*10^6*log2(1+So/No);
cc1bin=dec2bin(cc1);
j=1;
for i=147:1:178
ccdest(j)=CTS(i); %get the data rate at dest in bin
j=j+1;
end
ccdestdec=bin2dec(ccdest);
if(ccdestdec<cc1)
backoff1=cc1-ccdestdec;
else
return;
end
end
elseif (RTF~=0) % This means that, this node is the selected relay node and is
responsible for further communication th the network

display('Relay Node selected is Node1');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RTF at MAC layer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rtf_fcln=2*8; %Frame control of RTF length

rtf_fcpvln=2; %Protocol version in FC length


rtf_fcpv=dec2bin(0,rtf_fcpvln); %Protocol version in FC

rtf_fctypeln=2; %Type of FC length


rtf_fctypeb=dec2bin(2,rtf_fctypeln); %The Type of FC is data here hence 12

rtf_fcsubtypeln=4; %Subtype of FC length


rtf_fcsubtype=dec2bin(10,rtf_fcsubtypeln); %Here Subtype of FC is RTF
hence 1010

rtf_fcretryln=1; %Retransmission at FC length


rtf_fcretryb=dec2bin(0,rtf_fcretryln); %Retransmission at FC

rtf_Dln=2*8; %Duration of NAV length


TxDelay=2048/(3.2557*10^5); %Transmission Delay
EucDist=sqrt((XR1-XRsrc)^2+(YR1-YRsrc)^2); %Distance between the source
node and destination node
PropDelay=EucDist/(3*10^8); %Propagation delay
ProcessDelay=0.1*10^-6*2048; %Processing delay
rtf_D=4*(TxDelay+PropDelay+ProcessDelay)/10^-3; %Total NAV time or the
total round trip time
rtf_D=dec2bin(rtf_D,rtf_Dln); %Duration of NAV in milliseconds

rtf_ownaddrln=6*8; %Own address length


rtf_ownaddrhex=strcat('A2',':','34',':','46',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_ownaddrascii=double(rtf_ownaddrhex); %Destination MAC address in ascii
rtf_ownaddrb=dec2bin(rtf_ownaddrascii); %in bin
rtf_ownaddr=mat2str(rtf_ownaddrb); %in bin
rtf_addrbin=(regexprep(rtf_ownaddr,'[,;]''',''));
rtf_ownaddrbin=strrep(rtf_addrbin,'''','');

rtf_helpaddrln=6*8; %Helping node address length


rtf_helpaddrhex=strcat('A2',':','34',':','45',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_helpaddrascii=double(rtf_helpaddrhex); %Source MAC address in ascii
rtf_helpaddr=dec2bin(rtf_helpaddrascii); %in bin
rtf_helpaddrb=mat2str(rtf_helpaddr); %in bin
rtf_helperaddrbin=(regexprep(rtf_helpaddrb,'[,;]''',''));
rtf_helpaddrbin=strrep(rtf_helperaddrbin,'''','');

rtf_ackln=4*8; %Acknowledgement length


rtf_ack=dec2bin(0,rtf_ackln);

RTF_Frame=strcat(rtf_fcpv,rtf_fctypeb,rtf_fcsubtype,rtf_fcretryb,rtf_D,rtf_own
addrbin,rtf_helpaddrbin,rtf_ack);
notmyRTF=RTF_Frame;

disp('Broadcasting RTF to all the nodes in the range with the Relay
Node');

node1(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node2(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node3(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node4(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node5(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node6(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);
node7(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node8(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node9(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node10(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node11(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node12(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node13(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node14(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node15(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node16(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node17(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node18(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node19(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node20(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);
disp('RTF Received at the Source Node from Relay Node');
IP_Packet=IP_Packet;
data_out=data_out;

sourcenode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,notmyRTF,0,0,0,nodenum1,0,IP_Packet,0,tes
tno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr);
end

if(notmyRTF~=0)
return
end
if (mod~=0)
d=1;
M=16;
disp('Decoding at Relay Node')
demod=qamdemod(mod,M);
disp('Encoding at Relay Node')
mod=qammod(demod,M);
disp('Forwarding from Relay to Destination');
tx_p=150e-3;
P0=(100e-3/500);
P1=(100e-3/500);
d1=50;
d= 40;
N0=10e-2;
disp('Rayleigh Fading during Forwarding')
z=length(mod);
i=sqrt(-1);
% for k=1:z
% noise_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% h_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% end
IP_Packet=IP_Packet;

destinationnode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,IP_Packet,mod,dict,0,0,0,testno,data_o
ut,NoPacketsDropped,TimeTCP,FinalAudio,rr);

end
end
function
backoff2=node2(RTS,CTS,RTF,srcnode,destnode,XR1,YR1,XRsrc,YRsrc,IP_Packet,mod,
dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr)
nodenum2=2;
backoff2=0;
if nodenum2==srcnode || nodenum2==destnode % To avoid overlap of source and
destination nodes
return;
end

if(RTS~=0) % This means node 1 has received the Request to send from the
source node
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2); %Distance between the source
node and destination node
if (eucdist>100)
display('Node is 2 not in Range with Source Node');
return
else
%
node1macaddrhex=strcat('A4',':','34',':','55',':','10',':','22',':','10');
%Node MAC address assumed in hexnode1macaddrascii=double(node1macaddrhex);
%Source MAC address in ascii
% node1macaddrb=dec2bin(node1macaddrascii); %in bin
% node1macaddrbin=mat2str(node1macaddrb); %in bin
% node1loc=loc1; %location of the node
RTSframe=RTS;
return;
end
elseif(CTS~=0) % This means node 1 has received the Clear to send from the
destination node
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2);
if (eucdist>100)
display('Node is 2 not in Range with Destination Node');
return
else
So=1*eucdist^-3; %Signal Power at the node in
No=10^-17; %Noise power in
SNR=So/No;
cc2=20*10^6*log2(1+So/No);
cc2bin=dec2bin(cc2);
j=1;
for i=147:1:178
ccdest(j)=CTS(i); %get the data rate at dest in bin
j=j+1;
end
ccdestdec=bin2dec(ccdest);

if(ccdestdec<cc2)
backoff2=cc2-ccdestdec;

else
return;
end
end
elseif (RTF~=0)
display('Relay Node selected is Node 2');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RTF at MAC layer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rtf_fcln=2*8; %Frame control of RTF length

rtf_fcpvln=2; %Protocol version in FC length


rtf_fcpv=dec2bin(0,rtf_fcpvln); %Protocol version in FC

rtf_fctypeln=2; %Type of FC length


rtf_fctypeb=dec2bin(2,rtf_fctypeln); %The Type of FC is data here hence 12

rtf_fcsubtypeln=4; %Subtype of FC length


rtf_fcsubtype=dec2bin(10,rtf_fcsubtypeln); %Here Subtype of FC is RTF
hence 1010

rtf_fcretryln=1; %Retransmission at FC length


rtf_fcretryb=dec2bin(0,rtf_fcretryln); %Retransmission at FC

rtf_Dln=2*8; %Duration of NAV length


TxDelay=2048/(3.2557*10^5); %Transmission Delay
EucDist=sqrt((XR1-XRsrc)^2+(YR1-YRsrc)^2); %Distance between the source
node and destination node
PropDelay=EucDist/(3*10^8); %Propagation delay
ProcessDelay=0.1*10^-6*2048; %Processing delay
rtf_D=4*(TxDelay+PropDelay+ProcessDelay)/10^-3; %Total NAV time or the
total round trip time
rtf_D=dec2bin(rtf_D,rtf_Dln); %Duration of NAV in milliseconds

rtf_ownaddrln=6*8; %Own address length


rtf_ownaddrhex=strcat('A2',':','34',':','46',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_ownaddrascii=double(rtf_ownaddrhex); %Destination MAC address in ascii
rtf_ownaddrb=dec2bin(rtf_ownaddrascii); %in bin
rtf_ownaddr=mat2str(rtf_ownaddrb); %in bin
rtf_addrbin=(regexprep(rtf_ownaddr,'[,;]''',''));
rtf_ownaddrbin=strrep(rtf_addrbin,'''','');

rtf_helpaddrln=6*8; %Helping node address length


rtf_helpaddrhex=strcat('A2',':','34',':','45',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_helpaddrascii=double(rtf_helpaddrhex); %Source MAC address in ascii
rtf_helpaddr=dec2bin(rtf_helpaddrascii); %in bin
rtf_helpaddrb=mat2str(rtf_helpaddr); %in bin
rtf_helperaddrbin=(regexprep(rtf_helpaddrb,'[,;]''',''));
rtf_helpaddrbin=strrep(rtf_helperaddrbin,'''','');

rtf_ackln=4*8; %Acknowledgement length


rtf_ack=dec2bin(0,rtf_ackln);

RTF_Frame=strcat(rtf_fcpv,rtf_fctypeb,rtf_fcsubtype,rtf_fcretryb,rtf_D,rtf_own
addrbin,rtf_helpaddrbin,rtf_ack);
notmyRTF=RTF_Frame;

disp('Broadcasting RTF to all the nodes in the range with the Relay
Node');

node1(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node2(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node3(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node4(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node5(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node6(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);
node7(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node8(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node9(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node10(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node11(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node12(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node13(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node14(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node15(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node16(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node17(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node18(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node19(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node20(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);
disp('RTF Received at the Source Node from Relay Node');
IP_Packet=IP_Packet;
data_out=data_out;

sourcenode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,notmyRTF,0,0,0,nodenum2,0,IP_Packet,0,tes
tno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr);
end

if(notmyRTF~=0)
return
end
if (mod~=0)
d=1;
M=16;
disp('Decoding at Relay Node')
demod=qamdemod(mod,M);
disp('Encoding at Relay Node')
mod=qammod(demod,M);
disp('Forwarding from Relay to Destination');
tx_p=150e-3;
P0=(100e-3/500);
P1=(100e-3/500);
d1=50;
d= 40;
N0=10e-2;
disp('Rayleigh Fading during Forwarding')
z=length(mod);
i=sqrt(-1);
% for k=1:z
% noise_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% h_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% end
IP_Packet=IP_Packet;

destinationnode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,IP_Packet,mod,dict,0,0,0,testno,data_o
ut,NoPacketsDropped,TimeTCP,FinalAudio,rr)

end
end
function
backoff3=node3(RTS,CTS,RTF,srcnode,destnode,XR1,YR1,XRsrc,YRsrc,IP_Packet,mod,
dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr)
nodenum3=3;
backoff3=0;
if nodenum3==srcnode || nodenum3==destnode %to avoid overlap of nodes
return;
end

if(RTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2); %Distance between the source
node and destination node
if (eucdist>100)
display('Node 3 is not in Range with Source Node');
return
else
%
node1macaddrhex=strcat('A4',':','34',':','55',':','10',':','22',':','10');
%Node MAC address assumed in hexnode1macaddrascii=double(node1macaddrhex);
%Source MAC address in ascii
% node1macaddrb=dec2bin(node1macaddrascii); %in bin
% node1macaddrbin=mat2str(node1macaddrb); %in bin
% node1loc=loc1; %location of the node
RTSframe=RTS;
return;
end
elseif(CTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2);
if (eucdist>100)
display('Node 3 is not in Range with Destination Node');
return
else
So=1*eucdist^-3; %Signal Power at the node in
No=10^-17; %Noise power in
SNR=So/No;
cc3=20*10^6*log2(1+So/No);
cc3bin=dec2bin(cc3);
j=1;
for i=147:1:178
ccdest(j)=CTS(i); %get the data rate at dest in bin
j=j+1;
end
ccdestdec=bin2dec(ccdest);

if(ccdestdec<cc3)
backoff3=cc3-ccdestdec;

else
return;
end
end
elseif (RTF~=0)
display('Relay Node selected is Node3');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RTF at MAC layer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rtf_fcln=2*8; %Frame control of RTF length

rtf_fcpvln=2; %Protocol version in FC length


rtf_fcpv=dec2bin(0,rtf_fcpvln); %Protocol version in FC

rtf_fctypeln=2; %Type of FC length


rtf_fctypeb=dec2bin(2,rtf_fctypeln); %The Type of FC is data here hence 12

rtf_fcsubtypeln=4; %Subtype of FC length


rtf_fcsubtype=dec2bin(10,rtf_fcsubtypeln); %Here Subtype of FC is RTF
hence 1010

rtf_fcretryln=1; %Retransmission at FC length


rtf_fcretryb=dec2bin(0,rtf_fcretryln); %Retransmission at FC

rtf_Dln=2*8; %Duration of NAV length


TxDelay=2048/(3.2557*10^5); %Transmission Delay
EucDist=sqrt((XR1-XRsrc)^2+(YR1-YRsrc)^2); %Distance between the source
node and destination node
PropDelay=EucDist/(3*10^8); %Propagation delay
ProcessDelay=0.1*10^-6*2048; %Processing delay
rtf_D=4*(TxDelay+PropDelay+ProcessDelay)/10^-3; %Total NAV time or the
total round trip time
rtf_D=dec2bin(rtf_D,rtf_Dln); %Duration of NAV in milliseconds

rtf_ownaddrln=6*8; %Own address length


rtf_ownaddrhex=strcat('A2',':','34',':','46',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_ownaddrascii=double(rtf_ownaddrhex); %Destination MAC address in ascii
rtf_ownaddrb=dec2bin(rtf_ownaddrascii); %in bin
rtf_ownaddr=mat2str(rtf_ownaddrb); %in bin
rtf_addrbin=(regexprep(rtf_ownaddr,'[,;]''',''));
rtf_ownaddrbin=strrep(rtf_addrbin,'''','');

rtf_helpaddrln=6*8; %Helping node address length


rtf_helpaddrhex=strcat('A2',':','34',':','45',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_helpaddrascii=double(rtf_helpaddrhex); %Source MAC address in ascii
rtf_helpaddr=dec2bin(rtf_helpaddrascii); %in bin
rtf_helpaddrb=mat2str(rtf_helpaddr); %in bin
rtf_helperaddrbin=(regexprep(rtf_helpaddrb,'[,;]''',''));
rtf_helpaddrbin=strrep(rtf_helperaddrbin,'''','');

rtf_ackln=4*8; %Acknowledgement length


rtf_ack=dec2bin(0,rtf_ackln);

RTF_Frame=strcat(rtf_fcpv,rtf_fctypeb,rtf_fcsubtype,rtf_fcretryb,rtf_D,rtf_own
addrbin,rtf_helpaddrbin,rtf_ack);
notmyRTF=RTF_Frame;

disp('Broadcasting RTF to all the nodes in the range with the Relay
Node');

node1(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node2(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node3(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node4(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node5(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node6(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node7(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);
node8(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node9(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node10(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node11(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node12(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node13(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node14(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node15(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node16(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node17(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node18(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node19(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node20(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);
disp('RTF Received at the Source Node from Relay Node');
IP_Packet=IP_Packet;
data_out=data_out;

sourcenode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,notmyRTF,0,0,0,nodenum3,0,IP_Packet,0,tes
tno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr);
end

if(notmyRTF~=0)
return
end

if (mod~=0)
d=1;
M=16;
disp('Decoding at Relay Node')
demod=qamdemod(mod,M);
disp('Encoding at Relay Node')
mod=qammod(demod,M);
disp('Forwarding from Relay to Destination');
tx_p=150e-3;
P0=(100e-3/500);
P1=(100e-3/500);
d1=50;
d= 40;
N0=10e-2;
disp('Rayleigh Fading during Forwarding')
z=length(mod);
i=sqrt(-1);
% for k=1:z
% noise_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% h_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% end
IP_Packet=IP_Packet;

destinationnode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,IP_Packet,mod,dict,0,0,0,testno,data_o
ut,NoPacketsDropped,TimeTCP,FinalAudio,rr)

end
end
function
backoff4=node4(RTS,CTS,RTF,srcnode,destnode,XR1,YR1,XRsrc,YRsrc,IP_Packet,mod,
dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr)
nodenum4=4;
backoff4=0;
if nodenum4==srcnode || nodenum4==destnode %to avoid overlap of nodes
return;
end

if(RTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2); %Distance between the source
node and destination node
if (eucdist>100)
display('Node 4 is not in Range with Source Node');
return;
else
%
node1macaddrhex=strcat('A4',':','34',':','55',':','10',':','22',':','10');
%Node MAC address assumed in hexnode1macaddrascii=double(node1macaddrhex);
%Source MAC address in ascii
% node1macaddrb=dec2bin(node1macaddrascii); %in bin
% node1macaddrbin=mat2str(node1macaddrb); %in bin
% node1loc=loc1; %location of the node
RTSframe=RTS;
propdelay=eucdist/(3*10^8); %Propagation delay

return;
end
elseif(CTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2);
if (eucdist>100)
display('Node 4 is not in Range with Destination Node');
return;
else
So=1*eucdist^-3; %Signal Power at the node in
No=10^-17; %Noise power in
SNR=So/No;
cc4=20*10^6*log2(1+So/No);
cc4bin=dec2bin(cc4);
j=1;
for i=147:1:178
ccdest(j)=CTS(i); %get the data rate at dest in bin
j=j+1;
end
ccdestdec=bin2dec(ccdest);

if(ccdestdec<cc4)
backoff4=cc4-ccdestdec;

else
return;
end
end
elseif (RTF~=0)
display('Relay Node selected is Node4');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RTF at MAC layer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rtf_fcln=2*8; %Frame control of RTF length

rtf_fcpvln=2; %Protocol version in FC length


rtf_fcpv=dec2bin(0,rtf_fcpvln); %Protocol version in FC

rtf_fctypeln=2; %Type of FC length


rtf_fctypeb=dec2bin(2,rtf_fctypeln); %The Type of FC is data here hence 12

rtf_fcsubtypeln=4; %Subtype of FC length


rtf_fcsubtype=dec2bin(10,rtf_fcsubtypeln); %Here Subtype of FC is RTF
hence 1010

rtf_fcretryln=1; %Retransmission at FC length


rtf_fcretryb=dec2bin(0,rtf_fcretryln); %Retransmission at FC

rtf_Dln=2*8; %Duration of NAV length


TxDelay=2048/(3.2557*10^5); %Transmission Delay
EucDist=sqrt((XR1-XRsrc)^2+(YR1-YRsrc)^2); %Distance between the source
node and destination node
PropDelay=EucDist/(3*10^8); %Propagation delay
ProcessDelay=0.1*10^-6*2048; %Processing delay
rtf_D=4*(TxDelay+PropDelay+ProcessDelay)/10^-3; %Total NAV time or the
total round trip time
rtf_D=dec2bin(rtf_D,rtf_Dln); %Duration of NAV in milliseconds
rtf_ownaddrln=6*8; %Own address length
rtf_ownaddrhex=strcat('A2',':','34',':','46',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_ownaddrascii=double(rtf_ownaddrhex); %Destination MAC address in ascii
rtf_ownaddrb=dec2bin(rtf_ownaddrascii); %in bin
rtf_ownaddr=mat2str(rtf_ownaddrb); %in bin
rtf_addrbin=(regexprep(rtf_ownaddr,'[,;]''',''));
rtf_ownaddrbin=strrep(rtf_addrbin,'''','');

rtf_helpaddrln=6*8; %Helping node address length


rtf_helpaddrhex=strcat('A2',':','34',':','45',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_helpaddrascii=double(rtf_helpaddrhex); %Source MAC address in ascii
rtf_helpaddr=dec2bin(rtf_helpaddrascii); %in bin
rtf_helpaddrb=mat2str(rtf_helpaddr); %in bin
rtf_helperaddrbin=(regexprep(rtf_helpaddrb,'[,;]''',''));
rtf_helpaddrbin=strrep(rtf_helperaddrbin,'''','');

rtf_ackln=4*8; %Acknowledgement length


rtf_ack=dec2bin(0,rtf_ackln);

RTF_Frame=strcat(rtf_fcpv,rtf_fctypeb,rtf_fcsubtype,rtf_fcretryb,rtf_D,rtf_own
addrbin,rtf_helpaddrbin,rtf_ack);
notmyRTF=RTF_Frame;

disp('Broadcasting RTF to all the nodes in the range with the Relay
Node');

node1(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node2(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node3(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node4(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node5(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node6(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node7(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);
node8(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node9(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node10(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node11(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node12(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node13(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node14(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node15(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node16(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node17(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node18(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node19(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node20(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);
disp('RTF Received at the Source Node from Relay Node');
IP_Packet=IP_Packet;
data_out=data_out;

sourcenode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,notmyRTF,0,0,0,nodenum4,0,IP_Packet,0,tes
tno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr);
end

if(notmyRTF~=0)
return
end

if (mod~=0)
d=1;
M=16;
disp('Decoding at Relay Node')
demod=qamdemod(mod,M);
disp('Encoding at Relay Node')
mod=qammod(demod,M);
disp('Forwarding from Relay to Destination');
tx_p=150e-3;
P0=(100e-3/500);
P1=(100e-3/500);
d1=50;
d= 40;
N0=10e-2;
disp('Rayleigh Fading during Forwarding')
z=length(mod);
i=sqrt(-1);
% for k=1:z
% noise_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% h_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% end
IP_Packet=IP_Packet;

destinationnode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,IP_Packet,mod,dict,0,0,0,testno,data_o
ut,NoPacketsDropped,TimeTCP,FinalAudio,rr)

end
end
function
backoff5=node5(RTS,CTS,RTF,srcnode,destnode,XR1,YR1,XRsrc,YRsrc,IP_Packet,mod,
dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr)
nodenum5=5;
backoff5=0;
if nodenum5==srcnode || nodenum5==destnode %to avoid overlap of nodes
return;
end

if(RTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2); %Distance between the source
node and destination node
if (eucdist>100)
display('Node 5 is not in Range with Source Node');
return
else
%
node1macaddrhex=strcat('A4',':','34',':','55',':','10',':','22',':','10');
%Node MAC address assumed in hexnode1macaddrascii=double(node1macaddrhex);
%Source MAC address in ascii
% node1macaddrb=dec2bin(node1macaddrascii); %in bin
% node1macaddrbin=mat2str(node1macaddrb); %in bin
% node1loc=loc1; %location of the node
RTSframe=RTS;
propdelay=eucdist/(3*10^8); %Propagation delay

return;
end
elseif(CTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2);
if (eucdist>100)
display('Node 5 is not in Range with Destination Node');
return
else
So=1*eucdist^-3; %Signal Power at the node in
No=10^-17; %Noise power in
SNR=So/No;
cc5=20*10^6*log2(1+So/No);
cc5bin=dec2bin(cc5);
j=1;
for i=147:1:178
ccdest(j)=CTS(i); %get the data rate at dest in bin
j=j+1;
end
ccdestdec=bin2dec(ccdest);

if(ccdestdec<cc5)
backoff5=cc5-ccdestdec;

else
return;
end
end
elseif (RTF~=0)
display('Relay Node selected is Node5');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RTF at MAC layer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rtf_fcln=2*8; %Frame control of RTF length

rtf_fcpvln=2; %Protocol version in FC length


rtf_fcpv=dec2bin(0,rtf_fcpvln); %Protocol version in FC

rtf_fctypeln=2; %Type of FC length


rtf_fctypeb=dec2bin(2,rtf_fctypeln); %The Type of FC is data here hence 12

rtf_fcsubtypeln=4; %Subtype of FC length


rtf_fcsubtype=dec2bin(10,rtf_fcsubtypeln); %Here Subtype of FC is RTF
hence 1010

rtf_fcretryln=1; %Retransmission at FC length


rtf_fcretryb=dec2bin(0,rtf_fcretryln); %Retransmission at FC

rtf_Dln=2*8; %Duration of NAV length


TxDelay=2048/(3.2557*10^5); %Transmission Delay
EucDist=sqrt((XR1-XRsrc)^2+(YR1-YRsrc)^2); %Distance between the source
node and destination node
PropDelay=EucDist/(3*10^8); %Propagation delay
ProcessDelay=0.1*10^-6*2048; %Processing delay
rtf_D=4*(TxDelay+PropDelay+ProcessDelay)/10^-3; %Total NAV time or the
total round trip time
rtf_D=dec2bin(rtf_D,rtf_Dln); %Duration of NAV in milliseconds
rtf_ownaddrln=6*8; %Own address length
rtf_ownaddrhex=strcat('A2',':','34',':','46',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_ownaddrascii=double(rtf_ownaddrhex); %Destination MAC address in ascii
rtf_ownaddrb=dec2bin(rtf_ownaddrascii); %in bin
rtf_ownaddr=mat2str(rtf_ownaddrb); %in bin
rtf_addrbin=(regexprep(rtf_ownaddr,'[,;]''',''));
rtf_ownaddrbin=strrep(rtf_addrbin,'''','');

rtf_helpaddrln=6*8; %Helping node address length


rtf_helpaddrhex=strcat('A2',':','34',':','45',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_helpaddrascii=double(rtf_helpaddrhex); %Source MAC address in ascii
rtf_helpaddr=dec2bin(rtf_helpaddrascii); %in bin
rtf_helpaddrb=mat2str(rtf_helpaddr); %in bin
rtf_helperaddrbin=(regexprep(rtf_helpaddrb,'[,;]''',''));
rtf_helpaddrbin=strrep(rtf_helperaddrbin,'''','');

rtf_ackln=4*8; %Acknowledgement length


rtf_ack=dec2bin(0,rtf_ackln);

RTF_Frame=strcat(rtf_fcpv,rtf_fctypeb,rtf_fcsubtype,rtf_fcretryb,rtf_D,rtf_own
addrbin,rtf_helpaddrbin,rtf_ack);
notmyRTF=RTF_Frame;

disp('Broadcasting RTF to all the nodes in the range with the Relay
Node');

node1(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node2(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node3(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node4(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node5(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node6(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node7(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);
node8(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node9(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node10(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node11(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node12(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node13(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node14(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node15(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node16(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node17(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node18(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node19(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node20(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);
disp('RTF Received at the Source Node from Relay Node');
IP_Packet=IP_Packet;
data_out=data_out;

sourcenode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,notmyRTF,0,0,0,nodenum5,0,IP_Packet,0,tes
tno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr);
end

if(notmyRTF~=0)
return
end

if (mod~=0)
d=1;
M=16;
disp('Decoding at Relay Node')
demod=qamdemod(mod,M);
disp('Encoding at Relay Node')
mod=qammod(demod,M);
disp('Forwarding from Relay to Destination');
tx_p=150e-3;
P0=(100e-3/500);
P1=(100e-3/500);
d1=50;
d= 40;
N0=10e-2;
disp('Rayleigh Fading during Forwarding')
z=length(mod);
i=sqrt(-1);
% for k=1:z
% noise_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% h_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% end
IP_Packet=IP_Packet;

destinationnode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,IP_Packet,mod,dict,0,0,0,testno,data_o
ut,NoPacketsDropped,TimeTCP,FinalAudio,rr)

end
end
function
backoff6=node6(RTS,CTS,RTF,srcnode,destnode,XR1,YR1,XRsrc,YRsrc,IP_Packet,mod,
dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr)
nodenum6=6;
backoff6=0;
if nodenum6==srcnode || nodenum6==destnode %to avoid overlap of nodes
return;
end

if(RTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2); %Distance between the source
node and destination node
if (eucdist>100)
display('Node 6 is not in Range with Source Node');
return
else
%
node1macaddrhex=strcat('A4',':','34',':','55',':','10',':','22',':','10');
%Node MAC address assumed in hexnode1macaddrascii=double(node1macaddrhex);
%Source MAC address in ascii
% node1macaddrb=dec2bin(node1macaddrascii); %in bin
% node1macaddrbin=mat2str(node1macaddrb); %in bin
% node1loc=loc1; %location of the node
RTSframe=RTS;
propdelay=eucdist/(3*10^8); %Propagation delay

return;
end
elseif(CTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2);
if (eucdist>100)
display('Node 6 is not in Range with Destination Node');
return
else
So=1*eucdist^-3; %Signal Power at the node in
No=10^-17; %Noise power in
SNR=So/No;
cc6=20*10^6*log2(1+So/No);
cc6bin=dec2bin(cc6);
j=1;
for i=147:1:178
ccdest(j)=CTS(i); %get the data rate at dest in bin
j=j+1;
end
ccdestdec=bin2dec(ccdest);

if(ccdestdec<cc6)
backoff6=cc6-ccdestdec;

else
return;
end
end
elseif (RTF~=0)
display('Relay Node selected is Node6');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RTF at MAC layer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rtf_fcln=2*8; %Frame control of RTF length

rtf_fcpvln=2; %Protocol version in FC length


rtf_fcpv=dec2bin(0,rtf_fcpvln); %Protocol version in FC

rtf_fctypeln=2; %Type of FC length


rtf_fctypeb=dec2bin(2,rtf_fctypeln); %The Type of FC is data here hence 12

rtf_fcsubtypeln=4; %Subtype of FC length


rtf_fcsubtype=dec2bin(10,rtf_fcsubtypeln); %Here Subtype of FC is RTF
hence 1010

rtf_fcretryln=1; %Retransmission at FC length


rtf_fcretryb=dec2bin(0,rtf_fcretryln); %Retransmission at FC

rtf_Dln=2*8; %Duration of NAV length


TxDelay=2048/(3.2557*10^5); %Transmission Delay
EucDist=sqrt((XR1-XRsrc)^2+(YR1-YRsrc)^2); %Distance between the source
node and destination node
PropDelay=EucDist/(3*10^8); %Propagation delay
ProcessDelay=0.1*10^-6*2048; %Processing delay
rtf_D=4*(TxDelay+PropDelay+ProcessDelay)/10^-3; %Total NAV time or the
total round trip time
rtf_D=dec2bin(rtf_D,rtf_Dln); %Duration of NAV in milliseconds
rtf_ownaddrln=6*8; %Own address length
rtf_ownaddrhex=strcat('A2',':','34',':','46',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_ownaddrascii=double(rtf_ownaddrhex); %Destination MAC address in ascii
rtf_ownaddrb=dec2bin(rtf_ownaddrascii); %in bin
rtf_ownaddr=mat2str(rtf_ownaddrb); %in bin
rtf_addrbin=(regexprep(rtf_ownaddr,'[,;]''',''));
rtf_ownaddrbin=strrep(rtf_addrbin,'''','');

rtf_helpaddrln=6*8; %Helping node address length


rtf_helpaddrhex=strcat('A2',':','34',':','45',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_helpaddrascii=double(rtf_helpaddrhex); %Source MAC address in ascii
rtf_helpaddr=dec2bin(rtf_helpaddrascii); %in bin
rtf_helpaddrb=mat2str(rtf_helpaddr); %in bin
rtf_helperaddrbin=(regexprep(rtf_helpaddrb,'[,;]''',''));
rtf_helpaddrbin=strrep(rtf_helperaddrbin,'''','');

rtf_ackln=4*8; %Acknowledgement length


rtf_ack=dec2bin(0,rtf_ackln);

RTF_Frame=strcat(rtf_fcpv,rtf_fctypeb,rtf_fcsubtype,rtf_fcretryb,rtf_D,rtf_own
addrbin,rtf_helpaddrbin,rtf_ack);
notmyRTF=RTF_Frame;

disp('Broadcasting RTF to all the nodes in the range with the Relay
Node');

node1(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node2(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node3(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node4(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node5(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node6(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node7(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);
node8(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node9(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node10(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node11(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node12(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node13(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node14(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node15(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node16(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node17(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node18(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node19(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node20(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);
disp('RTF Received at the Source Node from Relay Node');
IP_Packet=IP_Packet;
data_out=data_out;

sourcenode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,notmyRTF,0,0,0,nodenum6,0,IP_Packet,0,tes
tno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr);
end

if(notmyRTF~=0)
return
end

if (mod~=0)
d=1;
M=16;
disp('Decoding at Relay Node')
demod=qamdemod(mod,M);
disp('Encoding at Relay Node')
mod=qammod(demod,M);
disp('Forwarding from Relay to Destination');
tx_p=150e-3;
P0=(100e-3/500);
P1=(100e-3/500);
d1=50;
d= 40;
N0=10e-2;
disp('Rayleigh Fading during Forwarding')
z=length(mod);
i=sqrt(-1);
% for k=1:z
% noise_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% h_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% end
IP_Packet=IP_Packet;

destinationnode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,IP_Packet,mod,dict,0,0,0,testno,data_o
ut,NoPacketsDropped,TimeTCP,FinalAudio,rr)

end
end
function
backoff7=node7(RTS,CTS,RTF,srcnode,destnode,XR1,YR1,XRsrc,YRsrc,IP_Packet,mod,
dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr)
nodenum7=7;
backoff7=0;
if nodenum7==srcnode || nodenum7==destnode %to avoid overlap of nodes
return;
end

if(RTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2); %Distance between the source
node and destination node
if (eucdist>100)
display('Node 7 is not in Range with Source Node');
return
else
%
node1macaddrhex=strcat('A4',':','34',':','55',':','10',':','22',':','10');
%Node MAC address assumed in hexnode1macaddrascii=double(node1macaddrhex);
%Source MAC address in ascii
% node1macaddrb=dec2bin(node1macaddrascii); %in bin
% node1macaddrbin=mat2str(node1macaddrb); %in bin
% node1loc=loc1; %location of the node
RTSframe=RTS;
propdelay=eucdist/(3*10^8); %Propagation delay

return;
end
elseif(CTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2);
if (eucdist>100)
display('Node 7 is not in Range with Destination Node');
return
else
So=1*eucdist^-3; %Signal Power at the node in
No=10^-17; %Noise power in
SNR=So/No;
cc7=20*10^6*log2(1+So/No);
cc7bin=dec2bin(cc7);
j=1;
for i=147:1:178
ccdest(j)=CTS(i); %get the data rate at dest in bin
j=j+1;
end
ccdestdec=bin2dec(ccdest);

if(ccdestdec<cc7)
backoff7=cc7-ccdestdec;

else
return;
end
end
elseif (RTF~=0)
display('Relay Node selected is Node7');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RTF at MAC layer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rtf_fcln=2*8; %Frame control of RTF length

rtf_fcpvln=2; %Protocol version in FC length


rtf_fcpv=dec2bin(0,rtf_fcpvln); %Protocol version in FC

rtf_fctypeln=2; %Type of FC length


rtf_fctypeb=dec2bin(2,rtf_fctypeln); %The Type of FC is data here hence 12

rtf_fcsubtypeln=4; %Subtype of FC length


rtf_fcsubtype=dec2bin(10,rtf_fcsubtypeln); %Here Subtype of FC is RTF
hence 1010

rtf_fcretryln=1; %Retransmission at FC length


rtf_fcretryb=dec2bin(0,rtf_fcretryln); %Retransmission at FC

rtf_Dln=2*8; %Duration of NAV length


TxDelay=2048/(3.2557*10^5); %Transmission Delay
EucDist=sqrt((XR1-XRsrc)^2+(YR1-YRsrc)^2); %Distance between the source
node and destination node
PropDelay=EucDist/(3*10^8); %Propagation delay
ProcessDelay=0.1*10^-6*2048; %Processing delay
rtf_D=4*(TxDelay+PropDelay+ProcessDelay)/10^-3; %Total NAV time or the
total round trip time
rtf_D=dec2bin(rtf_D,rtf_Dln); %Duration of NAV in milliseconds
rtf_ownaddrln=6*8; %Own address length
rtf_ownaddrhex=strcat('A2',':','34',':','46',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_ownaddrascii=double(rtf_ownaddrhex); %Destination MAC address in ascii
rtf_ownaddrb=dec2bin(rtf_ownaddrascii); %in bin
rtf_ownaddr=mat2str(rtf_ownaddrb); %in bin
rtf_addrbin=(regexprep(rtf_ownaddr,'[,;]''',''));
rtf_ownaddrbin=strrep(rtf_addrbin,'''','');

rtf_helpaddrln=6*8; %Helping node address length


rtf_helpaddrhex=strcat('A2',':','34',':','45',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_helpaddrascii=double(rtf_helpaddrhex); %Source MAC address in ascii
rtf_helpaddr=dec2bin(rtf_helpaddrascii); %in bin
rtf_helpaddrb=mat2str(rtf_helpaddr); %in bin
rtf_helperaddrbin=(regexprep(rtf_helpaddrb,'[,;]''',''));
rtf_helpaddrbin=strrep(rtf_helperaddrbin,'''','');

rtf_ackln=4*8; %Acknowledgement length


rtf_ack=dec2bin(0,rtf_ackln);

RTF_Frame=strcat(rtf_fcpv,rtf_fctypeb,rtf_fcsubtype,rtf_fcretryb,rtf_D,rtf_own
addrbin,rtf_helpaddrbin,rtf_ack);
notmyRTF=RTF_Frame;

disp('Broadcasting RTF to all the nodes in the range with the Relay
Node');

node1(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node2(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node3(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node4(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node5(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node6(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node7(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);
node8(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node9(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node10(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node11(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node12(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node13(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node14(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node15(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node16(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node17(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node18(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node19(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node20(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);
disp('RTF Received at the Source Node from Relay Node');
IP_Packet=IP_Packet;
data_out=data_out;

sourcenode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,notmyRTF,0,0,0,nodenum7,0,IP_Packet,0,tes
tno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr);
end

if(notmyRTF~=0)
return
end

if (mod~=0)
d=1;
M=16;
disp('Decoding at Relay Node')
demod=qamdemod(mod,M);
disp('Encoding at Relay Node')
mod=qammod(demod,M);
disp('Forwarding from Relay to Destination');
tx_p=150e-3;
P0=(100e-3/500);
P1=(100e-3/500);
d1=50;
d= 40;
N0=10e-2;
disp('Rayleigh Fading during Forwarding')
z=length(mod);
i=sqrt(-1);
% for k=1:z
% noise_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% h_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% end
IP_Packet=IP_Packet;

destinationnode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,IP_Packet,mod,dict,0,0,0,testno,data_o
ut,NoPacketsDropped,TimeTCP,FinalAudio,rr)

end
end
function
backoff8=node8(RTS,CTS,RTF,srcnode,destnode,XR1,YR1,XRsrc,YRsrc,IP_Packet,mod,
dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr)
nodenum8=8;
backoff8=0;
if nodenum8==srcnode || nodenum8==destnode %to avoid overlap of nodes
return;
end

if(RTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2); %Distance between the source
node and destination node
if (eucdist>100)
display('Node 8 is not in Range with Source Node');
return;
else
%
node1macaddrhex=strcat('A4',':','34',':','55',':','10',':','22',':','10');
%Node MAC address assumed in hexnode1macaddrascii=double(node1macaddrhex);
%Source MAC address in ascii
% node1macaddrb=dec2bin(node1macaddrascii); %in bin
% node1macaddrbin=mat2str(node1macaddrb); %in bin
% node1loc=loc1; %location of the node
RTSframe=RTS;
propdelay=eucdist/(3*10^8); %Propagation delay

return;
end
elseif(CTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2);
if (eucdist>100)
display('Node 8 is not in Range with Destination Node');
return
else
So=1*eucdist^-3; %Signal Power at the node in
No=10^-17; %Noise power in
SNR=So/No;
cc8=20*10^6*log2(1+So/No);
cc8bin=dec2bin(cc8);
j=1;
for i=147:1:178
ccdest(j)=CTS(i); %get the data rate at dest in bin
j=j+1;
end
ccdestdec=bin2dec(ccdest);

if(ccdestdec<cc8)
backoff8=cc8-ccdestdec;

else
return;
end
end
elseif (RTF~=0)
display('Relay Node selected is Node8');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RTF at MAC layer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rtf_fcln=2*8; %Frame control of RTF length

rtf_fcpvln=2; %Protocol version in FC length


rtf_fcpv=dec2bin(0,rtf_fcpvln); %Protocol version in FC

rtf_fctypeln=2; %Type of FC length


rtf_fctypeb=dec2bin(2,rtf_fctypeln); %The Type of FC is data here hence 12

rtf_fcsubtypeln=4; %Subtype of FC length


rtf_fcsubtype=dec2bin(10,rtf_fcsubtypeln); %Here Subtype of FC is RTF
hence 1010

rtf_fcretryln=1; %Retransmission at FC length


rtf_fcretryb=dec2bin(0,rtf_fcretryln); %Retransmission at FC

rtf_Dln=2*8; %Duration of NAV length


TxDelay=2048/(3.2557*10^5); %Transmission Delay
EucDist=sqrt((XR1-XRsrc)^2+(YR1-YRsrc)^2); %Distance between the source
node and destination node
PropDelay=EucDist/(3*10^8); %Propagation delay
ProcessDelay=0.1*10^-6*2048; %Processing delay
rtf_D=4*(TxDelay+PropDelay+ProcessDelay)/10^-3; %Total NAV time or the
total round trip time
rtf_D=dec2bin(rtf_D,rtf_Dln); %Duration of NAV in milliseconds
rtf_ownaddrln=6*8; %Own address length
rtf_ownaddrhex=strcat('A2',':','34',':','46',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_ownaddrascii=double(rtf_ownaddrhex); %Destination MAC address in ascii
rtf_ownaddrb=dec2bin(rtf_ownaddrascii); %in bin
rtf_ownaddr=mat2str(rtf_ownaddrb); %in bin
rtf_addrbin=(regexprep(rtf_ownaddr,'[,;]''',''));
rtf_ownaddrbin=strrep(rtf_addrbin,'''','');

rtf_helpaddrln=6*8; %Helping node address length


rtf_helpaddrhex=strcat('A2',':','34',':','45',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_helpaddrascii=double(rtf_helpaddrhex); %Source MAC address in ascii
rtf_helpaddr=dec2bin(rtf_helpaddrascii); %in bin
rtf_helpaddrb=mat2str(rtf_helpaddr); %in bin
rtf_helperaddrbin=(regexprep(rtf_helpaddrb,'[,;]''',''));
rtf_helpaddrbin=strrep(rtf_helperaddrbin,'''','');

rtf_ackln=4*8; %Acknowledgement length


rtf_ack=dec2bin(0,rtf_ackln);

RTF_Frame=strcat(rtf_fcpv,rtf_fctypeb,rtf_fcsubtype,rtf_fcretryb,rtf_D,rtf_own
addrbin,rtf_helpaddrbin,rtf_ack);
notmyRTF=RTF_Frame;

disp('Broadcasting RTF to all the nodes in the range with the Relay
Node');

node1(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node2(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node3(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node4(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node5(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node6(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node7(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);
node8(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node9(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node10(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node11(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node12(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node13(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node14(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node15(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node16(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node17(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node18(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node19(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node20(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);
disp('RTF Received at the Source Node from Relay Node');
IP_Packet=IP_Packet;
data_out=data_out;

sourcenode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,notmyRTF,0,0,0,nodenum8,0,IP_Packet,0,tes
tno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr);
end

if(notmyRTF~=0)
return
end

if (mod~=0)
d=1;
M=16;
disp('Decoding at Relay Node')
demod=qamdemod(mod,M);
disp('Encoding at Relay Node')
mod=qammod(demod,M);
disp('Forwarding from Relay to Destination');
tx_p=150e-3;
P0=(100e-3/500);
P1=(100e-3/500);
d1=50;
d= 40;
N0=10e-2;
disp('Rayleigh Fading during Forwarding')
z=length(mod);
i=sqrt(-1);
% for k=1:z
% noise_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% h_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% end
IP_Packet=IP_Packet;

destinationnode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,IP_Packet,mod,dict,0,0,0,testno,data_o
ut,NoPacketsDropped,TimeTCP,FinalAudio,rr)

end
end
function
backoff9=node9(RTS,CTS,RTF,srcnode,destnode,XR1,YR1,XRsrc,YRsrc,IP_Packet,mod,
dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr)
nodenum9=9;
backoff9=0;
if nodenum9==srcnode || nodenum9==destnode %to avoid overlap of nodes
return;
end

if(RTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2); %Distance between the source
node and destination node
if (eucdist>100)
display('Node 9 is not in Range with Source Node');
return
else
%
node1macaddrhex=strcat('A4',':','34',':','55',':','10',':','22',':','10');
%Node MAC address assumed in hexnode1macaddrascii=double(node1macaddrhex);
%Source MAC address in ascii
% node1macaddrb=dec2bin(node1macaddrascii); %in bin
% node1macaddrbin=mat2str(node1macaddrb); %in bin
% node1loc=loc1; %location of the node
RTSframe=RTS;
propdelay=eucdist/(3*10^8); %Propagation delay

return;
end
elseif(CTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2);
if (eucdist>100)
display('Node 9 is not in Range with Destination Node');
return
else
So=1*eucdist^-3; %Signal Power at the node in
No=10^-17; %Noise power in
SNR=So/No;
cc9=20*10^6*log2(1+So/No);
cc9bin=dec2bin(cc9);
j=1;
for i=147:1:178
ccdest(j)=CTS(i); %get the data rate at dest in bin
j=j+1;
end
ccdestdec=bin2dec(ccdest);

if(ccdestdec<cc9)
backoff9=cc9-ccdestdec;

else
return;
end
end
elseif (RTF~=0)
display('Relay Node selected is Node9');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RTF at MAC layer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rtf_fcln=2*8; %Frame control of RTF length

rtf_fcpvln=2; %Protocol version in FC length


rtf_fcpv=dec2bin(0,rtf_fcpvln); %Protocol version in FC

rtf_fctypeln=2; %Type of FC length


rtf_fctypeb=dec2bin(2,rtf_fctypeln); %The Type of FC is data here hence 12

rtf_fcsubtypeln=4; %Subtype of FC length


rtf_fcsubtype=dec2bin(10,rtf_fcsubtypeln); %Here Subtype of FC is RTF
hence 1010

rtf_fcretryln=1; %Retransmission at FC length


rtf_fcretryb=dec2bin(0,rtf_fcretryln); %Retransmission at FC

rtf_Dln=2*8; %Duration of NAV length


TxDelay=2048/(3.2557*10^5); %Transmission Delay
EucDist=sqrt((XR1-XRsrc)^2+(YR1-YRsrc)^2); %Distance between the source
node and destination node
PropDelay=EucDist/(3*10^8); %Propagation delay
ProcessDelay=0.1*10^-6*2048; %Processing delay
rtf_D=4*(TxDelay+PropDelay+ProcessDelay)/10^-3; %Total NAV time or the
total round trip time
rtf_D=dec2bin(rtf_D,rtf_Dln); %Duration of NAV in milliseconds
rtf_ownaddrln=6*8; %Own address length
rtf_ownaddrhex=strcat('A2',':','34',':','46',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_ownaddrascii=double(rtf_ownaddrhex); %Destination MAC address in ascii
rtf_ownaddrb=dec2bin(rtf_ownaddrascii); %in bin
rtf_ownaddr=mat2str(rtf_ownaddrb); %in bin
rtf_addrbin=(regexprep(rtf_ownaddr,'[,;]''',''));
rtf_ownaddrbin=strrep(rtf_addrbin,'''','');

rtf_helpaddrln=6*8; %Helping node address length


rtf_helpaddrhex=strcat('A2',':','34',':','45',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_helpaddrascii=double(rtf_helpaddrhex); %Source MAC address in ascii
rtf_helpaddr=dec2bin(rtf_helpaddrascii); %in bin
rtf_helpaddrb=mat2str(rtf_helpaddr); %in bin
rtf_helperaddrbin=(regexprep(rtf_helpaddrb,'[,;]''',''));
rtf_helpaddrbin=strrep(rtf_helperaddrbin,'''','');

rtf_ackln=4*8; %Acknowledgement length


rtf_ack=dec2bin(0,rtf_ackln);

RTF_Frame=strcat(rtf_fcpv,rtf_fctypeb,rtf_fcsubtype,rtf_fcretryb,rtf_D,rtf_own
addrbin,rtf_helpaddrbin,rtf_ack);
notmyRTF=RTF_Frame;

disp('Broadcasting RTF to all the nodes in the range with the Relay
Node');

node1(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node2(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node3(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node4(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node5(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node6(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node7(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);
node8(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node9(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node10(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node11(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node12(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node13(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node14(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node15(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node16(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node17(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node18(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node19(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node20(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);
disp('RTF Received at the Source Node from Relay Node');
IP_Packet=IP_Packet;
data_out=data_out;

sourcenode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,notmyRTF,0,0,0,nodenum9,0,IP_Packet,0,tes
tno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr);
end

if(notmyRTF~=0)
return
end

if (mod~=0)
d=1;
M=16;
disp('Decoding at Relay Node')
demod=qamdemod(mod,M);
disp('Encoding at Relay Node')
mod=qammod(demod,M);
disp('Forwarding from Relay to Destination');
tx_p=150e-3;
P0=(100e-3/500);
P1=(100e-3/500);
d1=50;
d= 40;
N0=10e-2;
disp('Rayleigh Fading during Forwarding')
z=length(mod);
i=sqrt(-1);
% for k=1:z
% noise_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% h_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% end
IP_Packet=IP_Packet;

destinationnode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,IP_Packet,mod,dict,0,0,0,testno,data_o
ut,NoPacketsDropped,TimeTCP,FinalAudio,rr)

end
end
function
backoff10=node10(RTS,CTS,RTF,srcnode,destnode,XR1,YR1,XRsrc,YRsrc,IP_Packet,mo
d,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr)
nodenum10=10;
backoff10=0;
if nodenum10==srcnode || nodenum10==destnode %to avoid overlap of nodes
return;
end

if(RTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2); %Distance between the source
node and destination node
if (eucdist>100)
display('Node 10 is not in Range with Source Node');
return
else
%
node1macaddrhex=strcat('A4',':','34',':','55',':','10',':','22',':','10');
%Node MAC address assumed in hexnode1macaddrascii=double(node1macaddrhex);
%Source MAC address in ascii
% node1macaddrb=dec2bin(node1macaddrascii); %in bin
% node1macaddrbin=mat2str(node1macaddrb); %in bin
% node1loc=loc1; %location of the node
RTSframe=RTS;
propdelay=eucdist/(3*10^8); %Propagation delay

return;
end
elseif(CTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2);
if (eucdist>100)
display('Node 10 is not in Range with Destination Node');
return;
else
So=1*eucdist^-3; %Signal Power at the node in
No=10^-17; %Noise power in
SNR=So/No;
cc10=20*10^6*log2(1+So/No);
cc10bin=dec2bin(cc10);
j=1;
for i=147:1:178
ccdest(j)=CTS(i); %get the data rate at dest in bin
j=j+1;
end
ccdestdec=bin2dec(ccdest);

if(ccdestdec<cc10)
backoff10=cc10-ccdestdec;

else
return;
end
end
elseif (RTF~=0)
display('Relay Node selected is Node10');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RTF at MAC layer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rtf_fcln=2*8; %Frame control of RTF length

rtf_fcpvln=2; %Protocol version in FC length


rtf_fcpv=dec2bin(0,rtf_fcpvln); %Protocol version in FC

rtf_fctypeln=2; %Type of FC length


rtf_fctypeb=dec2bin(2,rtf_fctypeln); %The Type of FC is data here hence 12

rtf_fcsubtypeln=4; %Subtype of FC length


rtf_fcsubtype=dec2bin(10,rtf_fcsubtypeln); %Here Subtype of FC is RTF
hence 1010

rtf_fcretryln=1; %Retransmission at FC length


rtf_fcretryb=dec2bin(0,rtf_fcretryln); %Retransmission at FC

rtf_Dln=2*8; %Duration of NAV length


TxDelay=2048/(3.2557*10^5); %Transmission Delay
EucDist=sqrt((XR1-XRsrc)^2+(YR1-YRsrc)^2); %Distance between the source
node and destination node
PropDelay=EucDist/(3*10^8); %Propagation delay
ProcessDelay=0.1*10^-6*2048; %Processing delay
rtf_D=4*(TxDelay+PropDelay+ProcessDelay)/10^-3; %Total NAV time or the
total round trip time
rtf_D=dec2bin(rtf_D,rtf_Dln); %Duration of NAV in milliseconds
rtf_ownaddrln=6*8; %Own address length
rtf_ownaddrhex=strcat('A2',':','34',':','46',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_ownaddrascii=double(rtf_ownaddrhex); %Destination MAC address in ascii
rtf_ownaddrb=dec2bin(rtf_ownaddrascii); %in bin
rtf_ownaddr=mat2str(rtf_ownaddrb); %in bin
rtf_addrbin=(regexprep(rtf_ownaddr,'[,;]''',''));
rtf_ownaddrbin=strrep(rtf_addrbin,'''','');

rtf_helpaddrln=6*8; %Helping node address length


rtf_helpaddrhex=strcat('A2',':','34',':','45',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_helpaddrascii=double(rtf_helpaddrhex); %Source MAC address in ascii
rtf_helpaddr=dec2bin(rtf_helpaddrascii); %in bin
rtf_helpaddrb=mat2str(rtf_helpaddr); %in bin
rtf_helperaddrbin=(regexprep(rtf_helpaddrb,'[,;]''',''));
rtf_helpaddrbin=strrep(rtf_helperaddrbin,'''','');

rtf_ackln=4*8; %Acknowledgement length


rtf_ack=dec2bin(0,rtf_ackln);

RTF_Frame=strcat(rtf_fcpv,rtf_fctypeb,rtf_fcsubtype,rtf_fcretryb,rtf_D,rtf_own
addrbin,rtf_helpaddrbin,rtf_ack);
notmyRTF=RTF_Frame;

disp('Broadcasting RTF to all the nodes in the range with the Relay
Node');

node1(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node2(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node3(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node4(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node5(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node6(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node7(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);
node8(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node9(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node10(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node11(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node12(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node13(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node14(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node15(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node16(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node17(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node18(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node19(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node20(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);
disp('RTF Received at the Source Node from Relay Node');
IP_Packet=IP_Packet;
data_out=data_out;

sourcenode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,notmyRTF,0,0,0,nodenum10,0,IP_Packet,0,te
stno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr);
end

if(notmyRTF~=0)
return
end

if (mod~=0)
d=1;
M=16;
disp('Decoding at Relay Node')
demod=qamdemod(mod,M);
disp('Encoding at Relay Node')
mod=qammod(demod,M);
disp('Forwarding from Relay to Destination');
tx_p=150e-3;
P0=(100e-3/500);
P1=(100e-3/500);
d1=50;
d= 40;
N0=10e-2;
disp('Rayleigh Fading during Forwarding')
z=length(mod);
i=sqrt(-1);
% for k=1:z
% noise_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% h_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% end
IP_Packet=IP_Packet;

destinationnode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,IP_Packet,mod,dict,0,0,0,testno,data_o
ut,NoPacketsDropped,TimeTCP,FinalAudio,rr)

end
end
function
backoff11=node11(RTS,CTS,RTF,srcnode,destnode,XR1,YR1,XRsrc,YRsrc,IP_Packet,mo
d,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr)
nodenum11=11;
backoff11=0;
if nodenum11==srcnode || nodenum11==destnode %to avoid overlap of nodes
return;
end

if(RTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2); %Distance between the source
node and destination node
if (eucdist>100)
display('Node 11 is not in Range with Source Node');
return
else
%
node1macaddrhex=strcat('A4',':','34',':','55',':','10',':','22',':','10');
%Node MAC address assumed in hexnode1macaddrascii=double(node1macaddrhex);
%Source MAC address in ascii
% node1macaddrb=dec2bin(node1macaddrascii); %in bin
% node1macaddrbin=mat2str(node1macaddrb); %in bin
% node1loc=loc1; %location of the node
RTSframe=RTS;
propdelay=eucdist/(3*10^8); %Propagation delay

return;
end
elseif(CTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2);
if (eucdist>100)
display('Node 11 is not in Range with Destination Node');
return
else
So=1*eucdist^-3; %Signal Power at the node in
No=10^-17; %Noise power in
SNR=So/No;
cc11=20*10^6*log2(1+So/No);
cc11bin=dec2bin(cc11);
j=1;
for i=147:1:178
ccdest(j)=CTS(i); %get the data rate at dest in bin
j=j+1;
end
ccdestdec=bin2dec(ccdest);

if(ccdestdec<cc11)
backoff11=cc11-ccdestdec;

else
return;
end
end
elseif (RTF~=0)
display('Relay Node selected is Node11');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RTF at MAC layer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rtf_fcln=2*8; %Frame control of RTF length

rtf_fcpvln=2; %Protocol version in FC length


rtf_fcpv=dec2bin(0,rtf_fcpvln); %Protocol version in FC

rtf_fctypeln=2; %Type of FC length


rtf_fctypeb=dec2bin(2,rtf_fctypeln); %The Type of FC is data here hence 12

rtf_fcsubtypeln=4; %Subtype of FC length


rtf_fcsubtype=dec2bin(10,rtf_fcsubtypeln); %Here Subtype of FC is RTF
hence 1010

rtf_fcretryln=1; %Retransmission at FC length


rtf_fcretryb=dec2bin(0,rtf_fcretryln); %Retransmission at FC

rtf_Dln=2*8; %Duration of NAV length


TxDelay=2048/(3.2557*10^5); %Transmission Delay
EucDist=sqrt((XR1-XRsrc)^2+(YR1-YRsrc)^2); %Distance between the source
node and destination node
PropDelay=EucDist/(3*10^8); %Propagation delay
ProcessDelay=0.1*10^-6*2048; %Processing delay
rtf_D=4*(TxDelay+PropDelay+ProcessDelay)/10^-3; %Total NAV time or the
total round trip time
rtf_D=dec2bin(rtf_D,rtf_Dln); %Duration of NAV in milliseconds
rtf_ownaddrln=6*8; %Own address length
rtf_ownaddrhex=strcat('A2',':','34',':','46',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_ownaddrascii=double(rtf_ownaddrhex); %Destination MAC address in ascii
rtf_ownaddrb=dec2bin(rtf_ownaddrascii); %in bin
rtf_ownaddr=mat2str(rtf_ownaddrb); %in bin
rtf_addrbin=(regexprep(rtf_ownaddr,'[,;]''',''));
rtf_ownaddrbin=strrep(rtf_addrbin,'''','');

rtf_helpaddrln=6*8; %Helping node address length


rtf_helpaddrhex=strcat('A2',':','34',':','45',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_helpaddrascii=double(rtf_helpaddrhex); %Source MAC address in ascii
rtf_helpaddr=dec2bin(rtf_helpaddrascii); %in bin
rtf_helpaddrb=mat2str(rtf_helpaddr); %in bin
rtf_helperaddrbin=(regexprep(rtf_helpaddrb,'[,;]''',''));
rtf_helpaddrbin=strrep(rtf_helperaddrbin,'''','');

rtf_ackln=4*8; %Acknowledgement length


rtf_ack=dec2bin(0,rtf_ackln);

RTF_Frame=strcat(rtf_fcpv,rtf_fctypeb,rtf_fcsubtype,rtf_fcretryb,rtf_D,rtf_own
addrbin,rtf_helpaddrbin,rtf_ack);
notmyRTF=RTF_Frame;

disp('Broadcasting RTF to all the nodes in the range with the Relay
Node');

node1(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node2(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node3(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node4(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node5(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node6(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node7(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);
node8(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node9(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node10(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node11(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node12(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node13(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node14(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node15(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node16(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node17(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node18(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node19(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node20(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);
disp('RTF Received at the Source Node from Relay Node');
IP_Packet=IP_Packet;
data_out=data_out;

sourcenode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,notmyRTF,0,0,0,nodenum11,0,IP_Packet,0,te
stno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr);
end

if(notmyRTF~=0)
return
end

if (mod~=0)
d=1;
M=16;
disp('Decoding at Relay Node')
demod=qamdemod(mod,M);
disp('Encoding at Relay Node')
mod=qammod(demod,M);
disp('Forwarding from Relay to Destination');
tx_p=150e-3;
P0=(100e-3/500);
P1=(100e-3/500);
d1=50;
d= 40;
N0=10e-2;
disp('Rayleigh Fading during Forwarding')
z=length(mod);
i=sqrt(-1);
% for k=1:z
% noise_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% h_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% end
IP_Packet=IP_Packet;

destinationnode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,IP_Packet,mod,dict,0,0,0,testno,data_o
ut,NoPacketsDropped,TimeTCP,FinalAudio,rr)

end
end
function
backoff12=node12(RTS,CTS,RTF,srcnode,destnode,XR1,YR1,XRsrc,YRsrc,IP_Packet,mo
d,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr)
nodenum12=12;
backoff12=0;
if nodenum12==srcnode || nodenum12==destnode %to avoid overlap of nodes
return;
end

if(RTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2); %Distance between the source
node and destination node
if (eucdist>100)
display('Node 12 is not in Range with Source Node');
return
else
%
node1macaddrhex=strcat('A4',':','34',':','55',':','10',':','22',':','10');
%Node MAC address assumed in hexnode1macaddrascii=double(node1macaddrhex);
%Source MAC address in ascii
% node1macaddrb=dec2bin(node1macaddrascii); %in bin
% node1macaddrbin=mat2str(node1macaddrb); %in bin
% node1loc=loc1; %location of the node
RTSframe=RTS;
propdelay=eucdist/(3*10^8); %Propagation delay

return;
end
elseif(CTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2);
if (eucdist>100)
display('Node 12 is not in Range with Destination Node');
return
else
So=1*eucdist^-3; %Signal Power at the node in
No=10^-17; %Noise power in
SNR=So/No;
cc12=20*10^6*log2(1+So/No);
cc12bin=dec2bin(cc12);
j=1;
for i=147:1:178
ccdest(j)=CTS(i); %get the data rate at dest in bin
j=j+1;
end
ccdestdec=bin2dec(ccdest);

if(ccdestdec<cc12)
backoff12=cc12-ccdestdec;

else
return;
end
end
elseif (RTF~=0)
display('Relay Node selected is Node12');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RTF at MAC layer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rtf_fcln=2*8; %Frame control of RTF length

rtf_fcpvln=2; %Protocol version in FC length


rtf_fcpv=dec2bin(0,rtf_fcpvln); %Protocol version in FC

rtf_fctypeln=2; %Type of FC length


rtf_fctypeb=dec2bin(2,rtf_fctypeln); %The Type of FC is data here hence 12

rtf_fcsubtypeln=4; %Subtype of FC length


rtf_fcsubtype=dec2bin(10,rtf_fcsubtypeln); %Here Subtype of FC is RTF
hence 1010

rtf_fcretryln=1; %Retransmission at FC length


rtf_fcretryb=dec2bin(0,rtf_fcretryln); %Retransmission at FC

rtf_Dln=2*8; %Duration of NAV length


TxDelay=2048/(3.2557*10^5); %Transmission Delay
EucDist=sqrt((XR1-XRsrc)^2+(YR1-YRsrc)^2); %Distance between the source
node and destination node
PropDelay=EucDist/(3*10^8); %Propagation delay
ProcessDelay=0.1*10^-6*2048; %Processing delay
rtf_D=4*(TxDelay+PropDelay+ProcessDelay)/10^-3; %Total NAV time or the
total round trip time
rtf_D=dec2bin(rtf_D,rtf_Dln); %Duration of NAV in milliseconds
rtf_ownaddrln=6*8; %Own address length
rtf_ownaddrhex=strcat('A2',':','34',':','46',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_ownaddrascii=double(rtf_ownaddrhex); %Destination MAC address in ascii
rtf_ownaddrb=dec2bin(rtf_ownaddrascii); %in bin
rtf_ownaddr=mat2str(rtf_ownaddrb); %in bin
rtf_addrbin=(regexprep(rtf_ownaddr,'[,;]''',''));
rtf_ownaddrbin=strrep(rtf_addrbin,'''','');

rtf_helpaddrln=6*8; %Helping node address length


rtf_helpaddrhex=strcat('A2',':','34',':','45',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_helpaddrascii=double(rtf_helpaddrhex); %Source MAC address in ascii
rtf_helpaddr=dec2bin(rtf_helpaddrascii); %in bin
rtf_helpaddrb=mat2str(rtf_helpaddr); %in bin
rtf_helperaddrbin=(regexprep(rtf_helpaddrb,'[,;]''',''));
rtf_helpaddrbin=strrep(rtf_helperaddrbin,'''','');

rtf_ackln=4*8; %Acknowledgement length


rtf_ack=dec2bin(0,rtf_ackln);

RTF_Frame=strcat(rtf_fcpv,rtf_fctypeb,rtf_fcsubtype,rtf_fcretryb,rtf_D,rtf_own
addrbin,rtf_helpaddrbin,rtf_ack);
notmyRTF=RTF_Frame;

disp('Broadcasting RTF to all the nodes in the range with the Relay
Node');

node1(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node2(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node3(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node4(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node5(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node6(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node7(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);
node8(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node9(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node10(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node11(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node12(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node13(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node14(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node15(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node16(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node17(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node18(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node19(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node20(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);
disp('RTF Received at the Source Node from Relay Node');
IP_Packet=IP_Packet;
data_out=data_out;

sourcenode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,notmyRTF,0,0,0,nodenum12,0,IP_Packet,0,te
stno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr);
end

if(notmyRTF~=0)
return
end

if (mod~=0)
d=1;
M=16;
disp('Decoding at Relay Node')
demod=qamdemod(mod,M);
disp('Encoding at Relay Node')
mod=qammod(demod,M);
disp('Forwarding from Relay to Destination');
tx_p=150e-3;
P0=(100e-3/500);
P1=(100e-3/500);
d1=50;
d= 40;
N0=10e-2;
disp('Rayleigh Fading during Forwarding')
z=length(mod);
i=sqrt(-1);
% for k=1:z
% noise_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% h_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% end
IP_Packet=IP_Packet;

destinationnode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,IP_Packet,mod,dict,0,0,0,testno,data_o
ut,NoPacketsDropped,TimeTCP,FinalAudio,rr)

end
end
function
backoff13=node13(RTS,CTS,RTF,srcnode,destnode,XR1,YR1,XRsrc,YRsrc,IP_Packet,mo
d,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr)
nodenum13=13;
backoff13=0;
if nodenum13==srcnode || nodenum13==destnode %to avoid overlap of nodes
return;
end

if(RTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2); %Distance between the source
node and destination node
if (eucdist>100)
display('Node 13 is not in Range with Source Node');
return
else
%
node1macaddrhex=strcat('A4',':','34',':','55',':','10',':','22',':','10');
%Node MAC address assumed in hexnode1macaddrascii=double(node1macaddrhex);
%Source MAC address in ascii
% node1macaddrb=dec2bin(node1macaddrascii); %in bin
% node1macaddrbin=mat2str(node1macaddrb); %in bin
% node1loc=loc1; %location of the node
RTSframe=RTS;
propdelay=eucdist/(3*10^8); %Propagation delay

return;
end
elseif(CTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2);
if (eucdist>100)
display('Node 13 is not in Range with Destination Node');
return
else
So=1*eucdist^-3; %Signal Power at the node in
No=10^-17; %Noise power in
SNR=So/No;
cc13=20*10^6*log2(1+So/No);
cc13bin=dec2bin(cc13);
j=1;
for i=147:1:178
ccdest(j)=CTS(i); %get the data rate at dest in bin
j=j+1;
end
ccdestdec=bin2dec(ccdest);

if(ccdestdec<cc13)
backoff13=cc13-ccdestdec;

else
return;
end
end
elseif (RTF~=0)
display('Relay Node selected is Node13');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RTF at MAC layer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rtf_fcln=2*8; %Frame control of RTF length

rtf_fcpvln=2; %Protocol version in FC length


rtf_fcpv=dec2bin(0,rtf_fcpvln); %Protocol version in FC

rtf_fctypeln=2; %Type of FC length


rtf_fctypeb=dec2bin(2,rtf_fctypeln); %The Type of FC is data here hence 12

rtf_fcsubtypeln=4; %Subtype of FC length


rtf_fcsubtype=dec2bin(10,rtf_fcsubtypeln); %Here Subtype of FC is RTF
hence 1010

rtf_fcretryln=1; %Retransmission at FC length


rtf_fcretryb=dec2bin(0,rtf_fcretryln); %Retransmission at FC

rtf_Dln=2*8; %Duration of NAV length


TxDelay=2048/(3.2557*10^5); %Transmission Delay
EucDist=sqrt((XR1-XRsrc)^2+(YR1-YRsrc)^2); %Distance between the source
node and destination node
PropDelay=EucDist/(3*10^8); %Propagation delay
ProcessDelay=0.1*10^-6*2048; %Processing delay
rtf_D=4*(TxDelay+PropDelay+ProcessDelay)/10^-3; %Total NAV time or the
total round trip time
rtf_D=dec2bin(rtf_D,rtf_Dln); %Duration of NAV in milliseconds
rtf_ownaddrln=6*8; %Own address length
rtf_ownaddrhex=strcat('A2',':','34',':','46',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_ownaddrascii=double(rtf_ownaddrhex); %Destination MAC address in ascii
rtf_ownaddrb=dec2bin(rtf_ownaddrascii); %in bin
rtf_ownaddr=mat2str(rtf_ownaddrb); %in bin
rtf_addrbin=(regexprep(rtf_ownaddr,'[,;]''',''));
rtf_ownaddrbin=strrep(rtf_addrbin,'''','');

rtf_helpaddrln=6*8; %Helping node address length


rtf_helpaddrhex=strcat('A2',':','34',':','45',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_helpaddrascii=double(rtf_helpaddrhex); %Source MAC address in ascii
rtf_helpaddr=dec2bin(rtf_helpaddrascii); %in bin
rtf_helpaddrb=mat2str(rtf_helpaddr); %in bin
rtf_helperaddrbin=(regexprep(rtf_helpaddrb,'[,;]''',''));
rtf_helpaddrbin=strrep(rtf_helperaddrbin,'''','');

rtf_ackln=4*8; %Acknowledgement length


rtf_ack=dec2bin(0,rtf_ackln);

RTF_Frame=strcat(rtf_fcpv,rtf_fctypeb,rtf_fcsubtype,rtf_fcretryb,rtf_D,rtf_own
addrbin,rtf_helpaddrbin,rtf_ack);
notmyRTF=RTF_Frame;

disp('Broadcasting RTF to all the nodes in the range with the Relay
Node');

node1(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node2(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node3(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node4(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node5(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node6(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node7(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);
node8(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node9(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node10(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node11(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node12(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node13(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node14(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node15(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node16(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node17(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node18(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node19(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node20(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);
disp('RTF Received at the Source Node from Relay Node');
IP_Packet=IP_Packet;
data_out=data_out;

sourcenode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,notmyRTF,0,0,0,nodenum13,0,IP_Packet,0,te
stno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr);
end

if(notmyRTF~=0)
return
end

if (mod~=0)
d=1;
M=16;
disp('Decoding at Relay Node')
demod=qamdemod(mod,M);
disp('Encoding at Relay Node')
mod=qammod(demod,M);
disp('Forwarding from Relay to Destination');
tx_p=150e-3;
P0=(100e-3/500);
P1=(100e-3/500);
d1=50;
d= 40;
N0=10e-2;
disp('Rayleigh Fading during Forwarding')
z=length(mod);
i=sqrt(-1);
% for k=1:z
% noise_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% h_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% end
IP_Packet=IP_Packet;

destinationnode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,IP_Packet,mod,dict,0,0,0,testno,data_o
ut,NoPacketsDropped,TimeTCP,FinalAudio,rr)

end
end
function
backoff14=node14(RTS,CTS,RTF,srcnode,destnode,XR1,YR1,XRsrc,YRsrc,IP_Packet,mo
d,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr)
nodenum14=14;
backoff14=0;
if nodenum14==srcnode || nodenum14==destnode %to avoid overlap of nodes
return;
end

if(RTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2); %Distance between the source
node and destination node
if (eucdist>500)
display('Node 14 is not in Range with Source Node');
return
else
%
node1macaddrhex=strcat('A4',':','34',':','55',':','10',':','22',':','10');
%Node MAC address assumed in hexnode1macaddrascii=double(node1macaddrhex);
%Source MAC address in ascii
% node1macaddrb=dec2bin(node1macaddrascii); %in bin
% node1macaddrbin=mat2str(node1macaddrb); %in bin
% node1loc=loc1; %location of the node
RTSframe=RTS;
propdelay=eucdist/(3*10^8); %Propagation delay

return;
end
elseif(CTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2);
if (eucdist>500)
display('Node 14 not is in Range with Destination Node');
return
else
So=1*eucdist^-3; %Signal Power at the node in
No=10^-17; %Noise power in
SNR=So/No;
cc14=20*10^6*log2(1+So/No);
cc14bin=dec2bin(cc14);
j=1;
for i=147:1:178
ccdest(j)=CTS(i); %get the data rate at dest in bin
j=j+1;
end
ccdestdec=bin2dec(ccdest);

if(ccdestdec<cc14)
backoff14=cc14-ccdestdec;

else
return;
end
end
elseif (RTF~=0)
display('Relay Node selected is Node14');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RTF at MAC layer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rtf_fcln=2*8; %Frame control of RTF length

rtf_fcpvln=2; %Protocol version in FC length


rtf_fcpv=dec2bin(0,rtf_fcpvln); %Protocol version in FC

rtf_fctypeln=2; %Type of FC length


rtf_fctypeb=dec2bin(2,rtf_fctypeln); %The Type of FC is data here hence 12

rtf_fcsubtypeln=4; %Subtype of FC length


rtf_fcsubtype=dec2bin(10,rtf_fcsubtypeln); %Here Subtype of FC is RTF
hence 1010

rtf_fcretryln=1; %Retransmission at FC length


rtf_fcretryb=dec2bin(0,rtf_fcretryln); %Retransmission at FC

rtf_Dln=2*8; %Duration of NAV length


TxDelay=2048/(3.2557*10^5); %Transmission Delay
EucDist=sqrt((XR1-XRsrc)^2+(YR1-YRsrc)^2); %Distance between the source
node and destination node
PropDelay=EucDist/(3*10^8); %Propagation delay
ProcessDelay=0.1*10^-6*2048; %Processing delay
rtf_D=4*(TxDelay+PropDelay+ProcessDelay)/10^-3; %Total NAV time or the
total round trip time
rtf_D=dec2bin(rtf_D,rtf_Dln); %Duration of NAV in milliseconds
rtf_ownaddrln=6*8; %Own address length
rtf_ownaddrhex=strcat('A2',':','34',':','46',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_ownaddrascii=double(rtf_ownaddrhex); %Destination MAC address in ascii
rtf_ownaddrb=dec2bin(rtf_ownaddrascii); %in bin
rtf_ownaddr=mat2str(rtf_ownaddrb); %in bin
rtf_addrbin=(regexprep(rtf_ownaddr,'[,;]''',''));
rtf_ownaddrbin=strrep(rtf_addrbin,'''','');

rtf_helpaddrln=6*8; %Helping node address length


rtf_helpaddrhex=strcat('A2',':','34',':','45',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_helpaddrascii=double(rtf_helpaddrhex); %Source MAC address in ascii
rtf_helpaddr=dec2bin(rtf_helpaddrascii); %in bin
rtf_helpaddrb=mat2str(rtf_helpaddr); %in bin
rtf_helperaddrbin=(regexprep(rtf_helpaddrb,'[,;]''',''));
rtf_helpaddrbin=strrep(rtf_helperaddrbin,'''','');

rtf_ackln=4*8; %Acknowledgement length


rtf_ack=dec2bin(0,rtf_ackln);

RTF_Frame=strcat(rtf_fcpv,rtf_fctypeb,rtf_fcsubtype,rtf_fcretryb,rtf_D,rtf_own
addrbin,rtf_helpaddrbin,rtf_ack);
notmyRTF=RTF_Frame;

disp('Broadcasting RTF to all the nodes in the range with the Relay
Node');

node1(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node2(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node3(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node4(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node5(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node6(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node7(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node8(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);
node9(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node10(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node11(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node12(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node13(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node14(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node15(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node16(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node17(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node18(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node19(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node20(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);
disp('RTF Received at the Source Node from Relay Node');
IP_Packet=IP_Packet;
data_out=data_out;

sourcenode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,notmyRTF,0,0,0,nodenum14,0,IP_Packet,0,te
stno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr);
end

if(notmyRTF~=0)
return
end

if (mod~=0)
d=1;
M=16;
disp('Decoding at Relay Node')
demod=qamdemod(mod,M);
disp('Encoding at Relay Node')
mod=qammod(demod,M);
disp('Forwarding from Relay to Destination');
tx_p=150e-3;
P0=(100e-3/500);
P1=(100e-3/500);
d1=50;
d= 40;
N0=10e-2;
disp('Rayleigh Fading during Forwarding')
z=length(mod);
i=sqrt(-1);
% for k=1:z
% noise_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% h_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% end
IP_Packet=IP_Packet;

destinationnode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,IP_Packet,mod,dict,0,0,0,testno,data_o
ut,NoPacketsDropped,TimeTCP,FinalAudio,rr)

end
end
function
backoff15=node15(RTS,CTS,RTF,srcnode,destnode,XR1,YR1,XRsrc,YRsrc,IP_Packet,mo
d,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr)
nodenum15=15;
backoff15=0;
if nodenum15==srcnode || nodenum15==destnode %to avoid overlap of nodes
return;
end

if(RTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2); %Distance between the source
node and destination node
if (eucdist>500)
display('Node 15 is not in Range with Source Node');
return
else
%
node1macaddrhex=strcat('A4',':','34',':','55',':','10',':','22',':','10');
%Node MAC address assumed in hexnode1macaddrascii=double(node1macaddrhex);
%Source MAC address in ascii
% node1macaddrb=dec2bin(node1macaddrascii); %in bin
% node1macaddrbin=mat2str(node1macaddrb); %in bin
% node1loc=loc1; %location of the node
RTSframe=RTS;
propdelay=eucdist/(3*10^8); %Propagation delay

return;
end
elseif(CTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2);
if (eucdist>500)
display('Node 15 is not in Range with Destination Node');
return
else
So=1*eucdist^-3; %Signal Power at the node in
No=10^-17; %Noise power in
SNR=So/No;
cc15=20*10^6*log2(1+So/No);
cc15bin=dec2bin(cc15);
j=1;
for i=147:1:178
ccdest(j)=CTS(i); %get the data rate at dest in bin
j=j+1;
end
ccdestdec=bin2dec(ccdest);

if(ccdestdec<cc15)
backoff15=cc15-ccdestdec;

else
return;
end
end
elseif (RTF~=0)
display('Relay Node selected is Node15');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RTF at MAC layer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rtf_fcln=2*8; %Frame control of RTF length

rtf_fcpvln=2; %Protocol version in FC length


rtf_fcpv=dec2bin(0,rtf_fcpvln); %Protocol version in FC

rtf_fctypeln=2; %Type of FC length


rtf_fctypeb=dec2bin(2,rtf_fctypeln); %The Type of FC is data here hence 12

rtf_fcsubtypeln=4; %Subtype of FC length


rtf_fcsubtype=dec2bin(10,rtf_fcsubtypeln); %Here Subtype of FC is RTF
hence 1010

rtf_fcretryln=1; %Retransmission at FC length


rtf_fcretryb=dec2bin(0,rtf_fcretryln); %Retransmission at FC

rtf_Dln=2*8; %Duration of NAV length


TxDelay=2048/(3.2557*10^5); %Transmission Delay
EucDist=sqrt((XR1-XRsrc)^2+(YR1-YRsrc)^2); %Distance between the source
node and destination node
PropDelay=EucDist/(3*10^8); %Propagation delay
ProcessDelay=0.1*10^-6*2048; %Processing delay
rtf_D=4*(TxDelay+PropDelay+ProcessDelay)/10^-3; %Total NAV time or the
total round trip time
rtf_D=dec2bin(rtf_D,rtf_Dln); %Duration of NAV in milliseconds

rtf_ownaddrln=6*8; %Own address length


rtf_ownaddrhex=strcat('A2',':','34',':','46',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_ownaddrascii=double(rtf_ownaddrhex); %Destination MAC address in ascii
rtf_ownaddrb=dec2bin(rtf_ownaddrascii); %in bin
rtf_ownaddr=mat2str(rtf_ownaddrb); %in bin
rtf_addrbin=(regexprep(rtf_ownaddr,'[,;]''',''));
rtf_ownaddrbin=strrep(rtf_addrbin,'''','');

rtf_helpaddrln=6*8; %Helping node address length


rtf_helpaddrhex=strcat('A2',':','34',':','45',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_helpaddrascii=double(rtf_helpaddrhex); %Source MAC address in ascii
rtf_helpaddr=dec2bin(rtf_helpaddrascii); %in bin
rtf_helpaddrb=mat2str(rtf_helpaddr); %in bin
rtf_helperaddrbin=(regexprep(rtf_helpaddrb,'[,;]''',''));
rtf_helpaddrbin=strrep(rtf_helperaddrbin,'''','');

rtf_ackln=4*8; %Acknowledgement length


rtf_ack=dec2bin(0,rtf_ackln);

RTF_Frame=strcat(rtf_fcpv,rtf_fctypeb,rtf_fcsubtype,rtf_fcretryb,rtf_D,rtf_own
addrbin,rtf_helpaddrbin,rtf_ack);
notmyRTF=RTF_Frame;

disp('Broadcasting RTF to all the nodes in the range with the Relay
Node');

node1(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node2(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node3(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node4(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node5(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node6(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node7(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node8(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node9(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);
node10(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node11(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node12(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node13(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node14(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node15(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node16(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node17(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node18(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node19(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node20(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);
disp('RTF Received at the Source Node from Relay Node');
IP_Packet=IP_Packet;
data_out=data_out;

sourcenode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,notmyRTF,0,0,0,nodenum15,0,IP_Packet,0,te
stno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr);
end

if(notmyRTF~=0)
return
end

if (mod~=0)
d=1;
M=16;
disp('Decoding at Relay Node')
demod=qamdemod(mod,M);
disp('Encoding at Relay Node')
mod=qammod(demod,M);
disp('Forwarding from Relay to Destination');
tx_p=150e-3;
P0=(100e-3/500);
P1=(100e-3/500);
d1=50;
d= 40;
N0=10e-2;
disp('Rayleigh Fading during Forwarding')
z=length(mod);
i=sqrt(-1);
% for k=1:z
% noise_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% h_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% end
IP_Packet=IP_Packet;

destinationnode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,IP_Packet,mod,dict,0,0,0,testno,data_o
ut,NoPacketsDropped,TimeTCP,FinalAudio,rr)

end
end
function
backoff16=node16(RTS,CTS,RTF,srcnode,destnode,XR1,YR1,XRsrc,YRsrc,IP_Packet,mo
d,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr)
nodenum16=16;
backoff16=0;
if nodenum16==srcnode || nodenum16==destnode %to avoid overlap of nodes
return;
end

if(RTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2); %Distance between the source
node and destination node
if (eucdist>500)
display('Node 16 is not in Range with Source Node');
return
else
%
node1macaddrhex=strcat('A4',':','34',':','55',':','10',':','22',':','10');
%Node MAC address assumed in hexnode1macaddrascii=double(node1macaddrhex);
%Source MAC address in ascii
% node1macaddrb=dec2bin(node1macaddrascii); %in bin
% node1macaddrbin=mat2str(node1macaddrb); %in bin
% node1loc=loc1; %location of the node
RTSframe=RTS;
propdelay=eucdist/(3*10^8); %Propagation delay

return;
end
elseif(CTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2);
if (eucdist>500)
display('Node 16 is not in Range with Destination Node');
return
else
So=1*eucdist^-3; %Signal Power at the node in
No=10^-17; %Noise power in
SNR=So/No;
cc16=20*10^6*log2(1+So/No);
cc16bin=dec2bin(cc16);
j=1;
for i=147:1:178
ccdest(j)=CTS(i); %get the data rate at dest in bin
j=j+1;
end
ccdestdec=bin2dec(ccdest);

if(ccdestdec<cc16)
backoff16=cc16-ccdestdec;

else
return;
end
end
elseif (RTF~=0)
display('Relay Node selected is Node16');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RTF at MAC layer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rtf_fcln=2*8; %Frame control of RTF length

rtf_fcpvln=2; %Protocol version in FC length


rtf_fcpv=dec2bin(0,rtf_fcpvln); %Protocol version in FC

rtf_fctypeln=2; %Type of FC length


rtf_fctypeb=dec2bin(2,rtf_fctypeln); %The Type of FC is data here hence 12

rtf_fcsubtypeln=4; %Subtype of FC length


rtf_fcsubtype=dec2bin(10,rtf_fcsubtypeln); %Here Subtype of FC is RTF
hence 1010

rtf_fcretryln=1; %Retransmission at FC length


rtf_fcretryb=dec2bin(0,rtf_fcretryln); %Retransmission at FC

rtf_Dln=2*8; %Duration of NAV length


TxDelay=2048/(3.2557*10^5); %Transmission Delay
EucDist=sqrt((XR1-XRsrc)^2+(YR1-YRsrc)^2); %Distance between the source
node and destination node
PropDelay=EucDist/(3*10^8); %Propagation delay
ProcessDelay=0.1*10^-6*2048; %Processing delay
rtf_D=4*(TxDelay+PropDelay+ProcessDelay)/10^-3; %Total NAV time or the
total round trip time
rtf_D=dec2bin(rtf_D,rtf_Dln); %Duration of NAV in milliseconds

rtf_ownaddrln=6*8; %Own address length


rtf_ownaddrhex=strcat('A2',':','34',':','46',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_ownaddrascii=double(rtf_ownaddrhex); %Destination MAC address in ascii
rtf_ownaddrb=dec2bin(rtf_ownaddrascii); %in bin
rtf_ownaddr=mat2str(rtf_ownaddrb); %in bin
rtf_addrbin=(regexprep(rtf_ownaddr,'[,;]''',''));
rtf_ownaddrbin=strrep(rtf_addrbin,'''','');

rtf_helpaddrln=6*8; %Helping node address length


rtf_helpaddrhex=strcat('A2',':','34',':','45',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_helpaddrascii=double(rtf_helpaddrhex); %Source MAC address in ascii
rtf_helpaddr=dec2bin(rtf_helpaddrascii); %in bin
rtf_helpaddrb=mat2str(rtf_helpaddr); %in bin
rtf_helperaddrbin=(regexprep(rtf_helpaddrb,'[,;]''',''));
rtf_helpaddrbin=strrep(rtf_helperaddrbin,'''','');

rtf_ackln=4*8; %Acknowledgement length


rtf_ack=dec2bin(0,rtf_ackln);

RTF_Frame=strcat(rtf_fcpv,rtf_fctypeb,rtf_fcsubtype,rtf_fcretryb,rtf_D,rtf_own
addrbin,rtf_helpaddrbin,rtf_ack);
notmyRTF=RTF_Frame;

disp('Broadcasting RTF to all the nodes in the range with the Relay
Node');

node1(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node2(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node3(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node4(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node5(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node6(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node7(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node8(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node9(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);
node10(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node11(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node12(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node13(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node14(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node15(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node16(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node17(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node18(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node19(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node20(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);
disp('RTF Received at the Source Node from Relay Node');
IP_Packet=IP_Packet;
data_out=data_out;

sourcenode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,notmyRTF,0,0,0,nodenum16,0,IP_Packet,0,te
stno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr);
end

if(notmyRTF~=0)
return
end

if (mod~=0)
d=1;
M=16;
disp('Decoding at Relay Node')
demod=qamdemod(mod,M);
disp('Encoding at Relay Node')
mod=qammod(demod,M);
disp('Forwarding from Relay to Destination');
tx_p=150e-3;
P0=(100e-3/500);
P1=(100e-3/500);
d1=50;
d= 40;
N0=10e-2;
disp('Rayleigh Fading during Forwarding')
z=length(mod);
i=sqrt(-1);
% for k=1:z
% noise_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% h_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% end
IP_Packet=IP_Packet;

destinationnode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,IP_Packet,mod,dict,0,0,0,testno,data_o
ut,NoPacketsDropped,TimeTCP,FinalAudio,rr)

end
end
function
backoff17=node17(RTS,CTS,RTF,srcnode,destnode,XR1,YR1,XRsrc,YRsrc,IP_Packet,mo
d,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr)
nodenum17=17;
backoff17=0;
if nodenum17==srcnode || nodenum17==destnode %to avoid overlap of nodes
return;
end

if(RTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2); %Distance between the source
node and destination node
if (eucdist>100)
display('Node 17 is not in Range with Source Node');
return
else
%
node1macaddrhex=strcat('A4',':','34',':','55',':','10',':','22',':','10');
%Node MAC address assumed in hexnode1macaddrascii=double(node1macaddrhex);
%Source MAC address in ascii
% node1macaddrb=dec2bin(node1macaddrascii); %in bin
% node1macaddrbin=mat2str(node1macaddrb); %in bin
% node1loc=loc1; %location of the node
RTSframe=RTS;
propdelay=eucdist/(3*10^8); %Propagation delay

return;
end
elseif(CTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2);
if (eucdist>100)
display('Node 17 is not in Range with Destination Node');
return
else
So=1*eucdist^-3; %Signal Power at the node in
No=10^-17; %Noise power in
SNR=So/No;
cc17=20*10^6*log2(1+So/No);
cc17bin=dec2bin(cc17);
j=1;
for i=147:1:178
ccdest(j)=CTS(i); %get the data rate at dest in bin
j=j+1;
end
ccdestdec=bin2dec(ccdest);

if(ccdestdec<cc17)
backoff17=cc17-ccdestdec;

else
return;
end
end
elseif (RTF~=0)
display('Relay Node selected is Node17');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RTF at MAC layer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rtf_fcln=2*8; %Frame control of RTF length

rtf_fcpvln=2; %Protocol version in FC length


rtf_fcpv=dec2bin(0,rtf_fcpvln); %Protocol version in FC

rtf_fctypeln=2; %Type of FC length


rtf_fctypeb=dec2bin(2,rtf_fctypeln); %The Type of FC is data here hence 12

rtf_fcsubtypeln=4; %Subtype of FC length


rtf_fcsubtype=dec2bin(10,rtf_fcsubtypeln); %Here Subtype of FC is RTF
hence 1010

rtf_fcretryln=1; %Retransmission at FC length


rtf_fcretryb=dec2bin(0,rtf_fcretryln); %Retransmission at FC

rtf_Dln=2*8; %Duration of NAV length


TxDelay=2048/(3.2557*10^5); %Transmission Delay
EucDist=sqrt((XR1-XRsrc)^2+(YR1-YRsrc)^2); %Distance between the source
node and destination node
PropDelay=EucDist/(3*10^8); %Propagation delay
ProcessDelay=0.1*10^-6*2048; %Processing delay
rtf_D=4*(TxDelay+PropDelay+ProcessDelay)/10^-3; %Total NAV time or the
total round trip time
rtf_D=dec2bin(rtf_D,rtf_Dln); %Duration of NAV in milliseconds

rtf_ownaddrln=6*8; %Own address length


rtf_ownaddrhex=strcat('A2',':','34',':','46',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_ownaddrascii=double(rtf_ownaddrhex); %Destination MAC address in ascii
rtf_ownaddrb=dec2bin(rtf_ownaddrascii); %in bin
rtf_ownaddr=mat2str(rtf_ownaddrb); %in bin
rtf_addrbin=(regexprep(rtf_ownaddr,'[,;]''',''));
rtf_ownaddrbin=strrep(rtf_addrbin,'''','');

rtf_helpaddrln=6*8; %Helping node address length


rtf_helpaddrhex=strcat('A2',':','34',':','45',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_helpaddrascii=double(rtf_helpaddrhex); %Source MAC address in ascii
rtf_helpaddr=dec2bin(rtf_helpaddrascii); %in bin
rtf_helpaddrb=mat2str(rtf_helpaddr); %in bin
rtf_helperaddrbin=(regexprep(rtf_helpaddrb,'[,;]''',''));
rtf_helpaddrbin=strrep(rtf_helperaddrbin,'''','');

rtf_ackln=4*8; %Acknowledgement length


rtf_ack=dec2bin(0,rtf_ackln);

RTF_Frame=strcat(rtf_fcpv,rtf_fctypeb,rtf_fcsubtype,rtf_fcretryb,rtf_D,rtf_own
addrbin,rtf_helpaddrbin,rtf_ack);
notmyRTF=RTF_Frame;

disp('Broadcasting RTF to all the nodes in the range with the Relay
Node');

node1(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node2(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node3(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node4(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node5(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node6(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node7(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node8(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node9(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);
node10(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node11(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node12(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node13(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node14(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node15(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node16(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node17(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node18(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node19(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node20(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);
disp('RTF Received at the Source Node from Relay Node');
IP_Packet=IP_Packet;
data_out=data_out;

sourcenode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,notmyRTF,0,0,0,nodenum17,0,IP_Packet,0,te
stno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr);
end

if(notmyRTF~=0)
return
end

if (mod~=0)
d=1;
M=16;
disp('Decoding at Relay Node')
demod=qamdemod(mod,M);
disp('Encoding at Relay Node')
mod=qammod(demod,M);
disp('Forwarding from Relay to Destination');
tx_p=150e-3;
P0=(100e-3/500);
P1=(100e-3/500);
d1=50;
d= 40;
N0=10e-2;
disp('Rayleigh Fading during Forwarding')
z=length(mod);
i=sqrt(-1);
% for k=1:z
% noise_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% h_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% end
IP_Packet=IP_Packet;

destinationnode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,IP_Packet,mod,dict,0,0,0,testno,data_o
ut,NoPacketsDropped,TimeTCP,FinalAudio,rr)

end
end
function
backoff18=node18(RTS,CTS,RTF,srcnode,destnode,XR1,YR1,XRsrc,YRsrc,IP_Packet,mo
d,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr)
nodenum18=18;
backoff18=0;
if nodenum18==srcnode || nodenum18==destnode %to avoid overlap of nodes
return;
end

if(RTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2); %Distance between the source
node and destination node
if (eucdist>100)
display('Node 18 is not in Range with Source Node');
return;
else
%
node1macaddrhex=strcat('A4',':','34',':','55',':','10',':','22',':','10');
%Node MAC address assumed in hexnode1macaddrascii=double(node1macaddrhex);
%Source MAC address in ascii
% node1macaddrb=dec2bin(node1macaddrascii); %in bin
% node1macaddrbin=mat2str(node1macaddrb); %in bin
% node1loc=loc1; %location of the node
RTSframe=RTS;
propdelay=eucdist/(3*10^8); %Propagation delay
return;
end
elseif(CTS~=0)
eucdist=sqrt((XRsrc-XR1)^2+(YRsrc-YR1)^2);
if (eucdist>100)
display('Node 18 is not in Range with Destination Node');
return;
else
So=1*eucdist^-3; %Signal Power at the node in
No=10^-17; %Noise power in
SNR=So/No;
cc18=20*10^6*log2(1+So/No);
cc18bin=dec2bin(cc18);
j=1;
for i=147:1:178
ccdest(j)=CTS(i); %get the data rate at dest in bin
j=j+1;
end
ccdestdec=bin2dec(ccdest);
if(ccdestdec<cc18)
backoff18=cc18-ccdestdec;

else
return;
end
end
elseif (RTF~=0)
display('Relay Node selected is Node18');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RTF at MAC layer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rtf_fcln=2*8; %Frame control of RTF length

rtf_fcpvln=2; %Protocol version in FC length


rtf_fcpv=dec2bin(0,rtf_fcpvln); %Protocol version in FC

rtf_fctypeln=2; %Type of FC length


rtf_fctypeb=dec2bin(2,rtf_fctypeln); %The Type of FC is data here hence 12

rtf_fcsubtypeln=4; %Subtype of FC length


rtf_fcsubtype=dec2bin(10,rtf_fcsubtypeln); %Here Subtype of FC is RTF
hence 1010

rtf_fcretryln=1; %Retransmission at FC length


rtf_fcretryb=dec2bin(0,rtf_fcretryln); %Retransmission at FC

rtf_Dln=2*8; %Duration of NAV length


TxDelay=2048/(3.2557*10^5); %Transmission Delay
EucDist=sqrt((XR1-XRsrc)^2+(YR1-YRsrc)^2); %Distance between the source
node and destination node
PropDelay=EucDist/(3*10^8); %Propagation delay
ProcessDelay=0.1*10^-6*2048; %Processing delay
rtf_D=4*(TxDelay+PropDelay+ProcessDelay)/10^-3; %Total NAV time or the
total round trip time
rtf_D=dec2bin(rtf_D,rtf_Dln); %Duration of NAV in milliseconds

rtf_ownaddrln=6*8; %Own address length


rtf_ownaddrhex=strcat('A2',':','34',':','46',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_ownaddrascii=double(rtf_ownaddrhex); %Destination MAC address in ascii
rtf_ownaddrb=dec2bin(rtf_ownaddrascii); %in bin
rtf_ownaddr=mat2str(rtf_ownaddrb); %in bin
rtf_addrbin=(regexprep(rtf_ownaddr,'[,;]''',''));
rtf_ownaddrbin=strrep(rtf_addrbin,'''','');
rtf_helpaddrln=6*8; %Helping node address length
rtf_helpaddrhex=strcat('A2',':','34',':','45',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_helpaddrascii=double(rtf_helpaddrhex); %Source MAC address in ascii
rtf_helpaddr=dec2bin(rtf_helpaddrascii); %in bin
rtf_helpaddrb=mat2str(rtf_helpaddr); %in bin
rtf_helperaddrbin=(regexprep(rtf_helpaddrb,'[,;]''',''));
rtf_helpaddrbin=strrep(rtf_helperaddrbin,'''','');

rtf_ackln=4*8; %Acknowledgement length


rtf_ack=dec2bin(0,rtf_ackln);

RTF_Frame=strcat(rtf_fcpv,rtf_fctypeb,rtf_fcsubtype,rtf_fcretryb,rtf_D,rtf_own
addrbin,rtf_helpaddrbin,rtf_ack);
notmyRTF=RTF_Frame;

disp('Broadcasting RTF to all the nodes in the range with the Relay
Node');

node1(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node2(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node3(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node4(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node5(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node6(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node7(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node8(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node9(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node10(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);
node11(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node12(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node13(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node14(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node15(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node16(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node17(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node18(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node19(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node20(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);
disp('RTF Received at the Source Node from Relay Node');
IP_Packet=IP_Packet;
data_out=data_out;

sourcenode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,notmyRTF,0,0,0,nodenum18,0,IP_Packet,0,te
stno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr);
end

if(notmyRTF~=0)
return
end

if (mod~=0)
d=1;
M=16;
disp('Decoding at Relay Node')
demod=qamdemod(mod,M);
disp('Encoding at Relay Node')
mod=qammod(demod,M);
disp('Forwarding from Relay to Destination');
tx_p=150e-3;
P0=(100e-3/500);
P1=(100e-3/500);
d1=50;
d= 40;
N0=10e-2;
disp('Rayleigh Fading during Forwarding')
z=length(mod);
i=sqrt(-1);
% for k=1:z
% noise_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% h_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% end
IP_Packet=IP_Packet;

destinationnode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,IP_Packet,mod,dict,0,0,0,testno,data_o
ut,NoPacketsDropped,TimeTCP,FinalAudio,rr);

end
end
function
backoff19=node19(RTS,CTS,RTF,srcnode,destnode,XR19,YR19,XRsrc,YRsrc,IP_Packet,
mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr)
nodenum19=19;
backoff19=0;
if nodenum19==srcnode || nodenum19==destnode %to avoid overlap of nodes
return;
end

if(RTS~=0)
eucdist=sqrt((XRsrc-XR19)^2+(YRsrc-YR19)^2); %Distance between the source
node and destination node
if (eucdist>100)
display('Node 19 is not in Range with Source Node');
return;
else

%node1macaddrhex=strcat('A4',':','34',':','55',':','10',':','22',':','10');
%Node MAC address assumed in hexnode1macaddrascii=double(node1macaddrhex);
%Source MAC address in ascii
%node1macaddrb=dec2bin(node1macaddrascii); %in bin
%node1macaddrbin=mat2str(node1macaddrb); %in bin
%node1loc=loc1; %location of the node
RTSframe=RTS;
propdelay=eucdist/(3*10^8); %Propagation delay
return;
end
elseif(CTS~=0)
eucdist=sqrt((XRsrc-XR19)^2+(YRsrc-YR19)^2);
if (eucdist>100)
display('Node 19 is not in Range with Destination Node');
return;
else
So=1*eucdist^-3; %Signal Power at the node in
No=10^-17; %Noise power in
SNR=So/No;
cc19=20*10^6*log2(1+So/No);
cc19bin=dec2bin(cc19);
j=1;
for i=147:1:178
ccdest(j)=CTS(i); %get the data rate at dest in bin
j=j+1;
end
ccdestdec=bin2dec(ccdest);
if(ccdestdec<cc19)
backoff19=cc19-ccdestdec;

else
return;
end
end
elseif (RTF~=0)
display('Relay Node selected is Node19');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RTF at MAC layer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rtf_fcln=2*8; %Frame control of RTF length

rtf_fcpvln=2; %Protocol version in FC length


rtf_fcpv=dec2bin(0,rtf_fcpvln); %Protocol version in FC

rtf_fctypeln=2; %Type of FC length


rtf_fctypeb=dec2bin(2,rtf_fctypeln); %The Type of FC is data here hence 12

rtf_fcsubtypeln=4; %Subtype of FC length


rtf_fcsubtype=dec2bin(10,rtf_fcsubtypeln); %Here Subtype of FC is RTF
hence 1010

rtf_fcretryln=1; %Retransmission at FC length


rtf_fcretryb=dec2bin(0,rtf_fcretryln); %Retransmission at FC

rtf_Dln=2*8; %Duration of NAV length


TxDelay=2048/(3.2557*10^5); %Transmission Delay
EucDist=sqrt((XR19-XRsrc)^2+(YR19-YRsrc)^2); %Distance between the source
node and destination node
PropDelay=EucDist/(3*10^8); %Propagation delay
ProcessDelay=0.1*10^-6*2048; %Processing delay
rtf_D=4*(TxDelay+PropDelay+ProcessDelay)/10^-3; %Total NAV time or the
total round trip time
rtf_D=dec2bin(rtf_D,rtf_Dln); %Duration of NAV in milliseconds

rtf_ownaddrln=6*8; %Own address length


rtf_ownaddrhex=strcat('A2',':','34',':','46',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_ownaddrascii=double(rtf_ownaddrhex); %Destination MAC address in ascii
rtf_ownaddrb=dec2bin(rtf_ownaddrascii); %in bin
rtf_ownaddr=mat2str(rtf_ownaddrb); %in bin
rtf_addrbin=(regexprep(rtf_ownaddr,'[,;]''',''));
rtf_ownaddrbin=strrep(rtf_addrbin,'''','');

rtf_helpaddrln=6*8; %Helping node address length


rtf_helpaddrhex=strcat('A2',':','34',':','45',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_helpaddrascii=double(rtf_helpaddrhex); %Source MAC address in ascii
rtf_helpaddr=dec2bin(rtf_helpaddrascii); %in bin
rtf_helpaddrb=mat2str(rtf_helpaddr); %in bin
rtf_helperaddrbin=(regexprep(rtf_helpaddrb,'[,;]''',''));
rtf_helpaddrbin=strrep(rtf_helperaddrbin,'''','');

rtf_ackln=4*8; %Acknowledgement length


rtf_ack=dec2bin(0,rtf_ackln);

RTF_Frame=strcat(rtf_fcpv,rtf_fctypeb,rtf_fcsubtype,rtf_fcretryb,rtf_D,rtf_own
addrbin,rtf_helpaddrbin,rtf_ack);
notmyRTF=RTF_Frame;

disp('Broadcasting RTF to all the nodes in the range with the Relay
Node');

node1(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node2(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node3(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node4(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node5(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node6(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node7(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node8(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node9(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node10(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node11(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);
node12(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node13(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node14(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node15(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node16(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node17(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node18(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node19(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node20(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);
disp('RTF Received at the Source Node from Relay Node');
IP_Packet=IP_Packet;
data_out=data_out;

sourcenode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,notmyRTF,0,0,0,nodenum19,0,IP_Packet,0,te
stno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr);
end

if(notmyRTF~=0)
return
end

if (mod~=0)
d=1;
M=16;
disp('Decoding at Relay Node')
demod=qamdemod(mod,M);
disp('Encoding at Relay Node')
mod=qammod(demod,M);
disp('Forwarding from Relay to Destination');
tx_p=150e-3;
P0=(100e-3/500);
P1=(100e-3/500);
d1=50;
d= 40;
N0=10e-2;
disp('Rayleigh Fading during Forwarding')
z=length(mod);
i=sqrt(-1);
% for k=1:z
% noise_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% h_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% end
IP_Packet=IP_Packet;

destinationnode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,IP_Packet,mod,dict,0,0,0,testno,data_o
ut,NoPacketsDropped,TimeTCP,FinalAudio,rr)

end
end
function
backoff20=node20(RTS,CTS,RTF,srcnode,destnode,XR20,YR20,XRsrc,YRsrc,IP_Packet,
mod,dict,notmyRTF,testno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr)
nodenum20=20;
backoff20=0;
if nodenum20==srcnode || nodenum20==destnode %to avoid overlap of nodes
return;
end

if(RTS~=0)
eucdist=sqrt((XRsrc-XR20)^2+(YRsrc-YR20)^2); %Distance between the source
node and destination node
if (eucdist>100)
display('Node 20 is not in Range with Source Node');
return
else

%node1macaddrhex=strcat('A4',':','34',':','55',':','10',':','22',':','10');
%Node MAC address assumed in hexnode1macaddrascii=double(node1macaddrhex);
%Source MAC address in ascii
%node1macaddrb=dec2bin(node1macaddrascii); %in bin
%node1macaddrbin=mat2str(node1macaddrb); %in bin
%node1loc=loc1; %location of the node
RTSframe=RTS;
propdelay=eucdist/(3*10^8); %Propagation delay
return;
end
elseif(CTS~=0)
eucdist=sqrt((XRsrc-XR20)^2+(YRsrc-YR20)^2);
if (eucdist>100)
display('Node 20 is not in Range with Destination Node');
return
else
So=1*eucdist^-3; %Signal Power at the node in
No=10^-17; %Noise power in
SNR=So/No;
cc20=20*10^6*log2(1+So/No);
cc20bin=dec2bin(cc20);
j=1;
for i=147:1:178
ccdest(j)=CTS(i); %get the data rate at dest in bin
j=j+1;
end
ccdestdec=bin2dec(ccdest);
if(ccdestdec<cc20)
backoff20=cc20-ccdestdec;

else
return;
end
end
elseif (RTF~=0)
display('Relay Node selected is Node20');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RTF at MAC layer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rtf_fcln=2*8; %Frame control of RTF length

rtf_fcpvln=2; %Protocol version in FC length


rtf_fcpv=dec2bin(0,rtf_fcpvln); %Protocol version in FC

rtf_fctypeln=2; %Type of FC length


rtf_fctypeb=dec2bin(2,rtf_fctypeln); %The Type of FC is data here hence 12

rtf_fcsubtypeln=4; %Subtype of FC length


rtf_fcsubtype=dec2bin(10,rtf_fcsubtypeln); %Here Subtype of FC is RTF
hence 1010

rtf_fcretryln=1; %Retransmission at FC length


rtf_fcretryb=dec2bin(0,rtf_fcretryln); %Retransmission at FC

rtf_Dln=2*8; %Duration of NAV length


TxDelay=2048/(3.2557*10^5); %Transmission Delay
EucDist=sqrt((XR20-XRsrc)^2+(YR20-YRsrc)^2); %Distance between the source
node and destination node
PropDelay=EucDist/(3*10^8); %Propagation delay
ProcessDelay=0.1*10^-6*2048; %Processing delay
rtf_D=4*(TxDelay+PropDelay+ProcessDelay)/10^-3; %Total NAV time or the
total round trip time
rtf_D=dec2bin(rtf_D,rtf_Dln); %Duration of NAV in milliseconds

rtf_ownaddrln=6*8; %Own address length


rtf_ownaddrhex=strcat('A2',':','34',':','46',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_ownaddrascii=double(rtf_ownaddrhex); %Destination MAC address in ascii
rtf_ownaddrb=dec2bin(rtf_ownaddrascii); %in bin
rtf_ownaddr=mat2str(rtf_ownaddrb); %in bin
rtf_addrbin=(regexprep(rtf_ownaddr,'[,;]''',''));
rtf_ownaddrbin=strrep(rtf_addrbin,'''','');

rtf_helpaddrln=6*8; %Helping node address length


rtf_helpaddrhex=strcat('A2',':','34',':','45',':','11',':','92',':','F1');
%Source MAC address assumed in hex
rtf_helpaddrascii=double(rtf_helpaddrhex); %Source MAC address in ascii
rtf_helpaddr=dec2bin(rtf_helpaddrascii); %in bin
rtf_helpaddrb=mat2str(rtf_helpaddr); %in bin
rtf_helperaddrbin=(regexprep(rtf_helpaddrb,'[,;]''',''));
rtf_helpaddrbin=strrep(rtf_helperaddrbin,'''','');

rtf_ackln=4*8; %Acknowledgement length


rtf_ack=dec2bin(0,rtf_ackln);

RTF_Frame=strcat(rtf_fcpv,rtf_fctypeb,rtf_fcsubtype,rtf_fcretryb,rtf_D,rtf_own
addrbin,rtf_helpaddrbin,rtf_ack);
notmyRTF=RTF_Frame;

disp('Broadcasting RTF to all the nodes in the range with the Relay
Node');

node1(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node2(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node3(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node4(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node5(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node6(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node7(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node8(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node9(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDroppe
d,TimeTCP,FinalAudio,rr);

node10(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node11(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node12(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);
node13(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node14(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node15(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node16(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node17(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node18(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node19(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);

node20(0,0,0,0,0,0,0,0,0,IP_Packet,0,0,notmyRTF,testno,data_out,NoPacketsDropp
ed,TimeTCP,FinalAudio,rr);
disp('RTF Received at the Source Node from Relay Node');
IP_Packet=IP_Packet;
data_out=data_out;

sourcenode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,notmyRTF,0,0,0,nodenum20,0,IP_Packet,0,te
stno,data_out,NoPacketsDropped,TimeTCP,FinalAudio,rr);
end

if(notmyRTF~=0)
return
end

if (mod~=0)
d=1;
M=16;
disp('Decoding at Relay Node')
demod=qamdemod(mod,M);
disp('Encoding at Relay Node')
mod=qammod(demod,M);
disp('Forwarding from Relay to Destination');
tx_p=150e-3;
P0=(100e-3/500);
P1=(100e-3/500);
d1=50;
d= 40;
N0=10e-2;
disp('Rayleigh Fading during Forwarding')
z=length(mod);
i=sqrt(-1);
% for k=1:z
% noise_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% h_rd = 1/sqrt(2)*(randn(z,1)+i*randn(z,1));
% end
IP_Packet=IP_Packet;

destinationnode(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,IP_Packet,mod,dict,0,0,0,testno,data_o
ut,NoPacketsDropped,TimeTCP,FinalAudio,rr)

end
end
function [sig_encoded,dict]=encoding(string1)

string=string1(:)';
symbol=[]; %initialise variables
count=[];
j=1;

%------------------------------------------loop to separate symbols and how


many times they occur
for i=1:length(string)
flag=0;
flag=ismember(symbol,string(i)); %symbols
if sum(flag)==0
symbol(j) = string(i);
k=ismember(string,string(i));
c=sum(k); %no of times it occurs
count(j) = c;
j=j+1;
end
end
ent=0;
total=sum(count); %total no of symbols
prob=[];
%-----------------------------------------for loop to find probability and
%entropy
for i=1:1:size((count)');
prob(i)=count(i)/total;
ent=ent-prob(i)*log2(prob(i));
end
var=0;
%-----------------------------------------function to create dictionary
[dict, avglen]=huffmandict(symbol,prob);
% print the dictionary.
temp = dict;
for i = 1:length(temp)
temp{i,2} = num2str(temp{i,2});
var=var+(length(dict{i,2})-avglen)^2; %variance calculation
end
temp;
% encoder and decoder functions
sig_encoded=huffmanenco(string,dict);

end
function adpcm_y = audio_encoder(raw_y)

IndexTable = [-1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8];

StepSizeTable = [7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173,
190, 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658, 724,
796, 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499,
2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630,
9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623,
27086, 29794, 32767];
prevsample = 0;
previndex = 1;
Ns = length(raw_y);
n = 1;
raw_y = (2048*8+127+104) * raw_y; % 16-bit operation
while (n <= Ns)
predsample = prevsample;
index = previndex;
step = StepSizeTable(index);
diff = raw_y(n) - predsample;
if (diff >= 0)
code = 0;
else
code = 8;
diff = -diff;
end
tempstep = step;
if (diff >= tempstep)
code = bitor(code, 4);
diff = diff - tempstep;
end
tempstep = bitshift(tempstep, -1);
if (diff >= tempstep)
code = bitor(code, 2);
diff = diff - tempstep;
end
tempstep = bitshift(tempstep, -1);
if (diff >= tempstep)
code = bitor(code, 1);
end
diffq = bitshift(step, -3);
if (bitand(code, 4))
diffq = diffq + step;
end
if (bitand(code, 2))
diffq = diffq + bitshift(step, -1);
end
if (bitand(code, 1))
diffq = diffq + bitshift(step, -2);
end
if (bitand(code, 8))
predsample = predsample - diffq;
else
predsample = predsample + diffq;
end
if (predsample > (2048*8+127+104))
predsample = (2048*8+127+104);
elseif (predsample < -(2048*8+127+104))
predsample = -(2048*8+127+104);
end
index = index + IndexTable(code+1);
if (index < 1)
index = 1;
end
if (index > 89)
index = 89;
end
prevsample = predsample;
previndex = index;
adpcm_y(n) = bitand(code, 15);
%adpcm_y(n) = code;
n = n + 1;
end
end
function raw_y = audio_decoder(adpcm_y)

IndexTable = [-1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8];

StepSizeTable = [7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173,
190, 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658, 724,
796, 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499,
2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630,
9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623,
27086, 29794, 32767];
prevsample = 0;
previndex = 1;
Ns = length(adpcm_y);
n = 1;
while (n <= Ns)
predsample = prevsample;
index = previndex;
step = StepSizeTable(index);
code = adpcm_y(n);
diffq = bitshift(step, -3);
if (bitand(code, 4))
diffq = diffq + step;
end
if (bitand(code, 2))
diffq = diffq + bitshift(step, -1);
end
if (bitand(code, 1))
diffq = diffq + bitshift(step, -2);
end
if (bitand(code, 8))
predsample = predsample - diffq;
else
predsample = predsample + diffq;
end
if (predsample > 2048*8+127+104)
predsample = 2048*8+127+104;
elseif (predsample < -(2048*8+127+104))
predsample = -(2048*8+127+104);
end
index = index + IndexTable(code+1);
if (index < 1)
index = 1;
end
if (index > 89)
index = 89;
end
prevsample = predsample;
previndex = index;
raw_y(n) = predsample / (2048*8+127+104);
n = n + 1;
end
end

You might also like