功能描述:
%%---IEEE802.11 CSMA/CA AdHoc Network------%%
% close all;
% clear all;
for M = 2;
%M=5; %無線節點數量
Counter=0;%數據包發送成功計算器
Delay=0;
PacketResendingRate=0; %數據包重發率
TotalTimeInSeconds=200;%總仿真時間,單位:秒
Resend = zeros(1,M);
BER=10^(-6);%誤碼率
SendTime = zeros(1,M);%數據包發送時間向量
ChannelBusy = 0;
Start = 0;
Collition = 0;
DeferenceTime = 0; %發送完成時間
Throughput = 0;
Throughput_Result = zeros(1,TotalTimeInSeconds);
PacketResendingRate_Result = zeros(1,TotalTimeInSeconds);
Delay_Result = zeros(1,TotalTimeInSeconds);
ArrivalTime = 1:M; %新數據包到無線節點發送緩沖區的時間
PacketLength = 1:M; %到無線節點發送緩沖區的新數據包大小
HasPacket = zeros(1,M);
CW = 1:M;
BackoffTimer = 1:M;
PacketBuff = zeros(M,5001); %無線節點發送緩沖區各數據包大小矩陣
CollitionStations = zeros(1,M+1); %沖突站點號向量
PhyRate = 2*10^6; %物理層數據率=2Mbit/s
SlotTime = 20*10^(-6); %時隙間隔=20us
TotalTime = TotalTimeInSeconds/SlotTime; %總仿真時間,單位:slot
SIFS = 0.5; %SIFS=0.5*slot,10us
DIFS = 2.5; %DIFS=2.5*slot,50us
ACK = 14*8/(PhyRate*SlotTime); %ACK大小,單位:slot
AverageArrivalTime = 110; %slots
AveragePacketLength = 50; %slots
CurBufferSize = zeros(1,M); %當前發送幀長
Buffer_Threshold = 8*10^6/(PhyRate*SlotTime);%AP最大緩沖區大小
%初始化
for i = 1:M
ArrivalTime(i) = ExpDis(AverageArrivalTime); %初始化到達時間
PacketLength(i) = 50;%初始化分組長度
CW(i) = 32; %競爭窗口,CW = 32 slots
BackoffTimer(i) = 1200; %退避時間用 1200 slots來代表無窮大
end
for t = 1:TotalTime
for i = 1:M
%更新發送緩沖區
if t == ArrivalTime(i)
%目前不能發送,push分組進PackeBuff,修改退避計數器
if CurBufferSize(i) < Buffer_Threshold - PacketLength(i)
PacketBuff = Push(PacketBuff,i,PacketLength(i));
%Push新數據包
CurBufferSize(i) = CurBufferSize(i) + PacketLength(i);
HasPacket(i) = 1;
if BackoffTimer(i) == 1200
BackoffTimer(i) = RandSel(CW(i));%退避計數器達到最大1200時,重置退避計數器
end
end
ArrivalTime(i) = ExpDis(AverageArrivalTime) + PacketLength(i) + t;
%下一個數據包的到達時間
PacketLength(i) = 50;
%下一個數據包的大小
end
end
for i = 1:M
if HasPacket(i) == 1 & ChannelBusy == 0 %PackeBuff中有數據包發送并且信道空閑
if BackoffTimer(i) == 0 %退避時間=0則發送
CollitionStations = Add(CollitionStations,i);%加入沖突站點序列中
if Resend(i)==0 ; %第一次發送
SendTime(i)=t+DIFS;
end
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);
PER=1-(1-BER)^(PacketBuff(n,2)*PhyRate*SlotTime);
AckER=1-(1-BER)^(ACK*PhyRate*SlotTime);
if rand>=PER & rand>=AckER;
PacketLost=0;%無錯誤
else
PacketLost=1;
end
end
if Collition == 0 & PacketLost==0%發送成功
n = CollitionStations(2);
CurBufferSize(n) = CurBufferSize(n) - PacketBuff(n,2);
Throughput = Throughput + PacketBuff(n,2) * SlotTime * PhyRate;
PacketBuff = Pop(PacketBuff,n);
Resend(n)=0;
Delay = Delay+t-SendTime(n);
Counter=Counter+1;
CW(n) = 32;
k = PacketBuff(n,1);
if k ==0%如果沒有數據等待發送,將HasPacke置0,BackoffTimer置Max
HasPacket(n) = 0;
BackoffTimer(n) = 1200;
else%還有數據分發送,修改碰撞計數器
BackoffTimer(n) = RandSel(CW(n));
end
else%發送失敗
PacketResendingRate=PacketResendingRate+1;
n = CollitionStations(1);
for i = 1:n
j = CollitionStations(i+1);
CW(j) = Increase(CW,j);
BackoffTimer(j) = RandSel(CW(j));
Resend(j)=1;
end
end
CollitionStations = zeros(1,M+1);
DeferenceTime = 0;
ChannelBusy = 0;
Collition = 0;
end
if mod(t,1/SlotTime) == 0%時間為整數
s = t * SlotTime;
s = round(s);
Throughput_Result(s) = Throughput;
if Counter~=0
Delay_Result(s)=Delay*SlotTime/Counter;%平均時延
else
Delay_Result(s)=0;
end
PacketResendingRate_Result(s) = PacketResendingRate;
Throughput = 0;
PacketResendingRate=0;
Delay = 0;
Counter=0;
end
end
savefile = sprintf('Packet Resending Rate M=(%d).txt',M);
fid = fopen(savefile,'a+');
fprintf(fid,'%1.6f ',PacketResendingRate_Result);
fclose(fid);
savefile = sprintf('Throughput M=(%d).txt',M);
fid = fopen(savefile,'a+');
fprintf(fid,'%1.6f ',Throughput_Result);
fclose(fid);
savefile = sprintf('Average Delay M=(%d).txt',M);
fid = fopen(savefile,'a+');
fprintf(fid,'%1.6f ',Delay_Result);
fclose(fid);
end
聯系:highspeedlogic
QQ :1224848052
微信:HuangL1121
郵箱:1224848052@qq.com
網站:http://www.mat7lab.com/
網站:http://www.hslogic.com/
微信掃一掃:
|