📄 cdma_cd.m
字号:
%%---------------------------------------------%%
%%---802.11 MAC CSMA/CD------%%
close all;
clear all;
% M=3 %numbers of stations
for M = 2:4
ChannelBusy = 0
Start = 0
Collition = 0
DeferenceTime = 0 %Station's deference time,发送期
Throughput = 0
Result = 1:20 %simulation result, in seconds
ArrivalTime = 1:M %Packets arrival time
PacketLength = 1:M %Packets length
HasPacket = zeros(1,M)
CW = 1:M
BackoffTimer = 1:M
PacketBuff = zeros(M,1501); %stations Packets buffer,1501的意思?
CollitionStations = zeros(1,M+1); %Collition station sequence
PhyRate = 2*10^6 %物理层数据率, physical rate,2Mbit/s
SlotTime = 20*10^(-6) %时隙间隔, slot time,20us
TotalTime = 200/SlotTime %total simulation time,in slots, 200s,10M slot
SIFS = 0.5 %SIFS=0.5*slot,10us
DIFS = 2.5 %DIFS=2.5*slot,50us
ACK = 14*8/(PhyRate*SlotTime) %ACK 14bytes换算成slot
AverageArrivalTime = 110 %slots
AveragePacketLength = 50 %slots
CurBufferSize = zeros(1,M) %slots,indicate the current buffer length,当前帧长为
Buffer_Threshold = 8*10^6/(PhyRate*SlotTime)%the max buffer length is 1M byte
%初始化
for i = 1:M
ArrivalTime(i) = ExpDis(AverageArrivalTime); %initial packet initial time,初始化到达时间
PacketLength(i) = ExpDis(AveragePacketLength);%initial packet length,初始化分组长度
CW(i) = 32; %竞争窗口,CW = 32 slots
BackoffTimer(i) = 1000; %BackoffTimer initial value,退避时间 1000 slots
end
for t = 1:TotalTime
for i = 1:M
%renew process,every station has almost infinite buffer(l-100)
if t == ArrivalTime(i)
%目前不能发送,push分组进PackeBuff,修改退避计数器
if CurBufferSize(i) < Buffer_Threshold - PacketLength(i)
PacketBuff = Push(PacketBuff,i,PacketLength(i));
%Push the new packet
CurBufferSize(i) = CurBufferSize(i) + PacketLength(i);
HasPacket(i) = 1;
if BackoffTimer(i) == 1000
BackoffTimer(i) = RandSel(CW(i));%退避计数器达到最大1000时,重置退避计数器
end
end
ArrivalTime(i) = ExpDis(AverageArrivalTime) + PacketLength(i) + t;
%update packet arrival time and length
PacketLength(i) = ExpDis(AveragePacketLength);
end
end
for i = 1:M
if HasPacket(i) == 1 & ChannelBusy == 0 %PackeBuff中有数据包发送并且信道空闲
if BackoffTimer(i) == 0 %退避时间=0 -> 发送
CollitionStations = Add(CollitionStations,i);%加入冲突站点序列中
Start = 1;
else
BackoffTimer(i) = BackoffTimer(i) - 1;%退避时间=!0 -> 退避时间-1
end
end
end
if Start == 1%信道改为忙碌
ChannelBusy = 1;
n = CollitionStations(1);
if n == 1%信道中只有一个站点发送数据则为正常发送情况
DeferenceTime = floor(t + SIFS + DIFS + ACK + PacketBuff(CollitionStations(2),2));
%PacketBuff(CollitionStations(2),2)数据长度
%成功发送时间
Collition=0;%没有碰撞
else
DeferenceTime = floor(t + DIFS + MaxLength(PacketBuff,CollitionStations));
%不能发送是的虚拟计数器时间?
Collition=1;%发生碰撞
end
Start=0;
end
if t == DeferenceTime & ChannelBusy == 1%信道忙的时候达到站点的等待时间
if Collition == 0
n = CollitionStations(2);
CurBufferSize(n) = CurBufferSize(n) - PacketBuff(n,2);
Throughput = Throughput + PacketBuff(n,2) * SlotTime * PhyRate;
PacketBuff = Pop(PacketBuff,n);
CW(n) = 32;
k = PacketBuff(n,1);
if k ==0%如果没有数据等待发送,将HasPacke置0,BackoffTimer置Max
HasPacket(n) = 0;
BackoffTimer(n) = 1000;
else%还有数据分发送,修改碰撞计数器
BackoffTimer(n) = RandSel(CW(n));
end
else
n = CollitionStations(1);
for i = 1:n
j = CollitionStations(i+1);
CW(j) = Increase(CW,j);
BackoffTimer(j) = RandSel(CW(j));
end
end
CollitionStations = zeros(1,M+1);
DeferenceTime = 0;
ChannelBusy = 0;
Collition = 0;
end
if mod(t,10/SlotTime) == 0
s = t * SlotTime/10;
s = round(s);
Result(s) = Throughput;
Throughput = 0;
end
end
% x = 1:200; %%%750
% plot(x,Result);
% title('csma')
% axis([0,20,0,1.8*10^6]);
savefile = sprintf('cdma_ca_M=(%d).txt',M);
fid = fopen(savefile,'a+');
fprintf(fid,'%1.6f ',Result);
fclose(fid);
end
% function y = ExpDis(a)
% y=0;
% while y == O
% A = rand(1);
% y = floor((-a) * log(1-A));
% end
%
% function y = Increase(CW,n)
% i = log2(CW(n));
% if i < 8
% i = i + 1;
% end
% y = 2^i;
%
% function y = MaxLength(PacketBuff,CollitionStations)
% max=0;
% for i = l:CollitionStations(1)
% if PacketBuff(Collitionstations(i+l),2) > max
% max = PacketBuff(Collitionstations(i+l),2);
% end
% end
% y=max;
%
% function y = Pop(PacketBuff,n)
% PacketBuff(n,:) = [PacketBuff(n,1),PacketBuff(n,3:1501),0];
% %here,accomadate with packet buffer length
% PacketBuff(n,l) = PacketBuff(n,1) - 1;
% y = PacketBuff;
%
% function y = Push(PacketBuff,index,PacketLength)
% i = PacketBuff(index,l);
% if i < 1500 %length(PacketBuff(index,:))-l
% PacketBuff(index,i+2) = PacketLength;
% PacketBuff(index,1) = PacketBuff(index,1) + l;
% end
% y = PacketBuff;
%
% function y = RandSel(CW)
% y = 0
% while y == 0
% y = floor(rand * CW);
% end
%
% function y = Add(CollitionStations,index)
% CollitionStations(1) = CollitionStations(1) + 1;
% i = CollitionStations(1);
% CollitionStations(i+1) = index;
% y = CollitionStations;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -