📄 delay_fixed.m
字号:
% delay for fixed subchannel selection scheme (figure 7(a))
% adpative subchannel allocation according to traffic load
clear all
close all
G=4; % number of subchannel
M=10000; % number of simulation slots
J=32; % number of users
simurange=20; % number of points to plot
WorkLoad=[0:simurange-1]/simurange+0.01;
BufSize = M; % buffer size
num_trial=20; % number of Monto-Carlo experiments
avg_delay=zeros(simurange,1); % fix subchannel allocation (each traffic type only uses its own subchannels)
avg_delay1=zeros(simurange,1); % type I with adaptive subchannel allocation
avg_delay2=zeros(simurange,1); % type II with adaptive subchannel allocation
avg_delay3=zeros(simurange,1); % type III with adaptive subchannel allocation
for ss=1:num_trial
ss
% generate the Poisson traffic
[Traffic,WorkLoad] = gen_traffic(M, J, simurange);
for ii = 1:simurange % for each traffic
sm_traffic = Traffic(:,:,ii); % Generate new traffic
%%%%%%%% initialization %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% new traffic load
Loads=zeros(1,J);
Loads1 = zeros(1,J);
Loads2 = zeros(1,J);
Loads3 = zeros(1,J);
% to indicate which packet each user is transmiting
flag=zeros(1,J);
flag1=zeros(1,J);
flag2=zeros(1,J);
flag3=zeros(1,J);
% used to store the time slots at which packets are resolved
time_table=zeros(J,M+J);
time_table1=zeros(J,M+J);
time_table2=zeros(J,M+J);
time_table3=zeros(J,M+J);
% record time slots at which packets are generated (put in the first
% coule of columns of the matrix pos_pack)
pos_pack=zeros(J,M+J);
for nn=1:J
temp=find(Traffic(nn,:,ii)==1); % find packets
pos_pack(nn,1:length(temp))=temp;
end
% count number of packets
count_pack=0;
count_pack1=0;
count_pack2=0;
count_pack3=0;
% indicate whether a CTE is over
flag_CTE=0;
flag_CTE1=0;
flag_CTE11=0;
flat_CET13=0;
flag_CTE2=0;
flag_CTE3=0;
%%%%%%%%%%% start the simulation %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for t1 = 1:M % for each slot
newLoads = sm_traffic(:,t1)'; % generate new traffic
Loads1 = Loads1 + newLoads; % Add new traffic of type I to buffer
Loads2 = Loads2 + newLoads; % Add type II to buffer
Loads3 = Loads3 + newLoads; % Add type III to buffer
Loads = Loads + newLoads; % Add to buffer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% fixed subchannel allocation (each traffic type uses its own
% subchannels for collsion resolution)
flag_CTE=max(0,flag_CTE-1);
Trans = Loads >0; % check whether buffer is not empty
num_active=sum(Trans); % number of active users
if num_active>0 & flag_CTE==0
active = find(Trans)'; % active users
flag_CTE=num_active; % CTE length + collision slot
ret_array = zeros(1,J);
ret_array(active)=1;
Loads = Loads - ret_array; % update the buffer
count_pack=count_pack+num_active; % count received packets
for kk=1:num_active
flag(active(kk))=flag(active(kk))+1; % update flag
t_gen=pos_pack(active(kk),flag(active(kk))); % which time slot the packet is generated
time_table(active(kk),flag(active(kk)))=num_active+t1-t_gen; % record the duration for this packet's transmission
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% traffic I packet
flag_CTE1=max(0,flag_CTE1-1);
flag_CTE11=max(0,flag_CTE11-1);
if flag_CTE11==0
flag_CTE13=0;
end
if flag_CTE1==0
Trans1 = Loads1 >0;
num_active1=sum(Trans1);
active1 = find(Trans1)';
if num_active1>0
time1=num_active1-1; % uses its own 2 subchannels
if WorkLoad(ii) > 0.7
for kk=1:num_active1
t_gen=pos_pack(active1(kk),flag1(active1(kk))+1);
temptime(kk)=num_active1+t1-t_gen;
end
if min(temptime) > 5
time1=(2*num_active1-2)/3; % uses its own subchannel as well as Type III (totally 3)
flag_CTE13=1;
end
end
flag_CTE1=1+ceil(time1);
flag_CTE11=1+fix(time1);
ret_array = zeros(1,J);
ret_array(active1)=1;
Loads1 = Loads1 - ret_array;
count_pack1=count_pack1+num_active1;
for kk=1:num_active1
flag1(active1(kk))=flag1(active1(kk))+1;
t_gen=pos_pack(active1(kk),flag1(active1(kk)));
time_table1(active1(kk),flag1(active1(kk)))=1+ceil(time1)+t1-t_gen;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% traffic II packet
flag_CTE2=max(0,flag_CTE2-1);
if flag_CTE2==0
Trans2 = Loads2 >0;
num_active2=sum(Trans2);
active2 = find(Trans2)';
if num_active2>0
time2=num_active2-1;
flag_CTE2=num_active2;
for kk=1:num_active2
flag2(active2(kk))=flag2(active2(kk))+1;
t_gen=pos_pack(active2(kk),flag2(active2(kk)));
time_table2(active2(kk),flag2(active2(kk)))=num_active2+t1-t_gen;
end
ret_array = zeros(1,J);
ret_array(active2)=1;
Loads2 = Loads2 - ret_array;
count_pack2=count_pack2+num_active2;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% traffic III packet
flag_CTE3=max(0,flag_CTE3-1);
if flag_CTE3==0
Trans3 = Loads3 >0;
num_active3=sum(Trans3);
active3 = find(Trans3)';
if num_active3>0
if WorkLoad(ii)<=0.7 | (WorkLoad(ii)>=0.7 & flag_CTE13==0)
flag_CTE3=num_active3;
ret_array = zeros(1,J);
ret_array(active3)=1;
Loads3 = Loads3 - ret_array;
count_pack3=count_pack3+num_active3;
num_active3_save=num_active3;
active3_save=active3;
for kk=1:num_active3
flag3(active3(kk))=flag3(active3(kk))+1;
t_gen=pos_pack(active3(kk),flag3(active3(kk)));
time_table3(active3(kk),flag3(active3(kk)))=num_active3+t1-t_gen;
end
else
if num_active1>0 | flag_CTE11==0
flag_CTE3=fix(time1)+num_active3;
ret_array = zeros(1,J);
ret_array(active3)=1;
Loads3 = Loads3 - ret_array;
count_pack3=count_pack3+num_active3;
num_active3_save=num_active3;
active3_save=active3;
for kk=1:num_active3
flag3(active3(kk))=flag3(active3(kk))+1;
t_gen=pos_pack(active3(kk),flag3(active3(kk)));
time_table3(active3(kk),flag3(active3(kk)))=fix(time1)+num_active3+t1-t_gen;
end
end
end
end
end
if WorkLoad(ii)>0.7 & flag_CTE13==1 & flag_CTE3>0 & num_active3_save>0 & num_active1>0 & flag_CTE1 ==1+ceil(time1);
additional=fix(time1);
for kk=1:num_active3_save
time_table3(active3_save(kk),flag3(active3_save(kk)))=time_table3(active3_save(kk),flag3(active3_save(kk)))+additional;
end
end
num_active1=0;
num_active2=0;
num_active3=0;
time1=0;
time2=0;
time3=0;
end % for t1 = 1:M
if count_pack>0
avg_delay(ii)=avg_delay(ii)+sum(sum(time_table))/count_pack;
avg_delay1(ii)=avg_delay1(ii)+sum(sum(time_table1))/count_pack1;
avg_delay2(ii)=avg_delay2(ii)+sum(sum(time_table2))/count_pack2;
avg_delay3(ii)=avg_delay3(ii)+sum(sum(time_table3))/count_pack3;
end
end
end
figure
plot(WorkLoad,avg_delay/num_trial,'b','linewidth',2)
hold on
plot(WorkLoad,avg_delay1/num_trial,'k-','linewidth',2) % traffic I
plot(WorkLoad,avg_delay2/num_trial,'k-.','linewidth',2) % traffic II
plot(WorkLoad,avg_delay3/num_trial,'k--','linewidth',2) % traffic III
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -