我这边直接用的matlab是2024a版本
但是rate(j,frame) =snr2rate(snr(j,frame));这个语句一直报错
我自己修改之后 图片效果不如意 全部是一样的图
想知道应该如何修改
希望亲们可以自己跑一遍 还有报大
以下是我使用的
function []=nrtps_schedule(frame_num,user_num)
%RR, Max C/I, PF, WFQ
frame_num=100;
t_c = 0.0002058;
%time constant for PF
tc_pf = 5;
t_frame = 0.005;
user_num=300;
rr_throughput = 0;
rr_throughput_user = zeros(user_num,1);
rr_user_flag = 1;
maxci_throughput = 0;
maxci_throughput_user = zeros(user_num,1);
pf_throughput = 0;
pf_throughput_user = zeros(user_num,1);
wfq_throughput = 0;
wfq_throughput_user = zeros(user_num,1);
slot_remain_rr = 0;
slot_remain_maxci = 0;
slot_remain_pf = 0;
slot_remain_wfq = 0;
utilization_rr = zeros(1,frame_num);
total_throughput_rr = zeros(1,frame_num);
fi_rr = zeros(1,frame_num);
utilization_maxci = zeros(1,frame_num);
total_throughput_maxci = zeros(1,frame_num);
fi_maxci = zeros(1,frame_num);
utilization_pf = zeros(1,frame_num);
total_throughput_pf = zeros(1,frame_num);
fi_pf = zeros(1,frame_num);
utilization_wfq = zeros(1,frame_num);
total_throughput_wfq = zeros(1,frame_num);
fi_wfq = zeros(1,frame_num);
for i = 1:user_num
for j =1:5
maxci_pk(i,j).size = 0;
end
end
for i = 1:user_num
for j =1:5
pf_pk(i,j).size = 0;
end
end
%for PF
lamda_pf = ones(user_num,1); %ave_throughput,updating every allocation time
snr = zeros(user_num,frame_num);
for i = 1:user_num
snr(i,:) = raylrnd(15,1,frame_num);
end
% normal distribution
rsv_mu = 1200;
rsv_sigma = 100;
rsv_rate = normrnd(rsv_mu,rsv_sigma,user_num,1);
system_time = 0;
for frame = 1:frame_num
frame_rr = zeros(11,30);
frame_maxci = zeros(11,30);
frame_pf = zeros(11,30);
frame_wfq = zeros(11,30);
rate = zeros(user_num,frame_num);
for j = 1:user_num
if snr(j,frame)<5
snr(j,frame) = 5;
end
rate(j,frame) =snr2rate(snr(j,frame));
end
rtps_size_a = 0.12*8;
rtps_size_b = 0.24*8;
rtps_size = rtps_size_a + (rtps_size_b - rtps_size_a)* rand(user_num,5); % every user has 5 connections
%for rr
rr_rate = rate;
%Round Robin
rr_priority_lst = rtps_size;
%for rr allocate the frame resource
rr_slot_num = zeros(user_num,5);
for j = 1:user_num
for k = 1:5
rr_slot_num(j,k) = ceil(rr_priority_lst(j,k)*1000/rr_rate(j,frame)/t_c);
end
end
total_slot_rr = 330;
rr_break_flag = 0;
%round robin
for j = rr_user_flag:user_num
if rr_break_flag == 1
break
end
for k = 1:5
if rr_slot_num(j) > 0
total_slot_rr = total_slot_rr - rr_slot_num(j,k);
if (total_slot_rr < 0)
total_slot_rr = total_slot_rr + rr_slot_num(j,k);
rr_user_flag = j;
rr_break_flag = 1;
break
else
frame_rr = fill_ofdma_frame(frame_rr,rr_slot_num(j,k));
rr_throughput = rr_throughput + rr_priority_lst(j,k);
rr_throughput_user(j,1) = rr_throughput_user(j,1) + rr_priority_lst(j,k);
end
end
end
end
if rr_user_flag ~= 1
for j = 1:rr_user_flag-1
if rr_break_flag == 1
break
end
for k = 1:5
if rr_slot_num(j) > 0
total_slot_rr = total_slot_rr - rr_slot_num(j,k);
if (total_slot_rr < 0)
total_slot_rr = total_slot_rr + rr_slot_num(j,k);
rr_user_flag = j;
rr_break_flag = 1;
break
else
frame_rr = fill_ofdma_frame(frame_rr,rr_slot_num(j,k));
rr_throughput = rr_throughput + rr_priority_lst(j,k);
rr_throughput_user(j,1) = rr_throughput_user(j,1) + rr_priority_lst(j,k);
end
end
end
end
end
slot_remain_rr = slot_remain_rr + total_slot_rr;
%for max C/I
maxci_rate = rate;
snr_copy = snr;
%for max C/I allocate the frame resource
maxci_rate = rate;
for i = 1:user_num
for j =1:5
if maxci_pk(i,j).size == 0 % the connection request has been served
maxci_pk(i,j).size = rtps_size(i,j);
maxci_pk(i,j).slot_num = ceil(rtps_size(i,j)*1000/maxci_rate(i,frame)/t_c);
end
maxci_pk(i,j).serv_flag = 0; % at the beginning of each frame, everyone is unserved
end
end
total_slot_maxci = 330;
while total_slot_maxci > 0
%for maxci to find the one with biggest snr
all_serve_flag = 0; % flag to indicate if all connections of a user has been served
user_maxci_flag = 1;
snr_maxci_max = snr_copy(1,frame);
for j = 1:user_num
if (snr_copy(j,frame) > snr_maxci_max)
snr_maxci_max = snr_copy(j,frame);
user_maxci_flag = j;
end
end
%if the highest snr is 0, means all user has been served
if(snr_copy(user_maxci_flag,frame) == 0)
break
end
if maxci_pk(user_maxci_flag,5).size > 0 %the selected user is not empty
% the user_pf_flag_th user is selected
% serve all the connection request of the user
for n = 1:5
total_slot_maxci = total_slot_maxci - maxci_pk(user_maxci_flag,n).slot_num;%slot num decrease
if total_slot_maxci < 0
total_slot_maxci = total_slot_maxci + maxci_pk(user_maxci_flag,n).slot_num;
break
else
frame_maxci = fill_ofdma_frame(frame_maxci,maxci_pk(user_maxci_flag,n).slot_num);
maxci_pk(user_maxci_flag,n).serv_flag = 1; % indicate this connection has been served
maxci_throughput = maxci_throughput + maxci_pk(user_maxci_flag,n).size;
maxci_throughput_user(user_maxci_flag) = maxci_throughput_user(user_maxci_flag) + maxci_pk(user_maxci_flag,n).size;
maxci_pk(user_maxci_flag,n).size = 0; % clear the served pk
if n == 5
all_serve_flag = 1; %if all connections of the user have been served, flag it, clear its snrcopy later
end
end
end
% record the selected user has been served
% if all_serve_flag == 1
snr_copy(user_maxci_flag,frame) = 0;
% end
end
end % end of while
%for PF
pf_slot_num = zeros(user_num,1);
pf_slot_used = zeros(user_num,1);
pf_throughput_frame = zeros(user_num,1); %pf throughput per frame
pf_rate = rate; %equal to R
prio_pf = zeros(user_num,1);
for j = 1:user_num
prio_pf(j) = pf_rate(j,frame)/lamda_pf(j);
end
for i = 1:user_num
for j =1:5
if pf_pk(i,j).size == 0 % the connection request has been served
pf_pk(i,j).size = rtps_size(i,j);
pf_pk(i,j).slot_num = ceil(rtps_size(i,j)*1000/pf_rate(i,frame)/t_c);
end
pf_pk(i,j).serv_flag = 0; % at the beginning of each frame, everyone is unserved
end
end
total_slot_pf = 330;
while total_slot_pf >= 0
d_pf = zeros(user_num,1); %actual transmission rate,every allocation time needs updating
%for PF find the user with biggest priority
user_pf_flag = 1;
prio_pf_max = prio_pf(1);
for j = 1:user_num
if (prio_pf(j) > prio_pf_max)
prio_pf_max = prio_pf(j);
user_pf_flag = j;
end
end
%if the highest priority is zero: means all users have been served
if (prio_pf(user_pf_flag) == 0)
break
end
if (pf_pk(user_pf_flag,5).size > 0) %the selected user is not empty
%the user_pf_flag_th user is selected
%serve all the connection request of the user
for n = 1:5
total_slot_pf = total_slot_pf - pf_pk(user_pf_flag,n).slot_num; %slot num decrease
if total_slot_pf < 0
break
else
pf_slot_used(user_pf_flag,1) = pf_slot_used(user_pf_flag,1) + pf_pk(user_pf_flag,n).slot_num;
frame_pf = fill_ofdma_frame(frame_pf,pf_pk(user_pf_flag,n).slot_num);
pf_throughput = pf_throughput + pf_pk(user_pf_flag,n).size;
pf_throughput_frame(user_pf_flag) = pf_throughput_frame(user_pf_flag) + pf_pk(user_pf_flag,n).size;
pf_throughput_user(user_pf_flag) = pf_throughput_user(user_pf_flag) + pf_pk(user_pf_flag,n).size;
pf_pk(user_pf_flag,n).size = 0; % clear the served pk
pf_pk(user_pf_flag,n).serv_flag = 1; % indicate this connection has been served
end
end
end
%record the user selected has been served
prio_pf(user_pf_flag) = 0;
end %end of while
if total_slot_pf < 0
total_slot_pf = total_slot_pf + pf_pk(user_pf_flag,n).slot_num; %not enough slots
end
%updating related parameters after per frame
for j = 1:user_num
if pf_slot_used(j,1) > 0
d_pf(j) = (pf_throughput_frame(j)*1000/(pf_slot_used(j,1)*t_c));
else
d_pf(j) = 0;
end
end
for j = 1:user_num
lamda_pf(j) = (1 - 1/tc_pf)*lamda_pf(j) + d_pf(j)/tc_pf;
end
slot_remain_pf = slot_remain_pf + total_slot_pf;
% WFQ
arv_time_a = 0;
arv_time_b = t_frame;
arv_time = arv_time_a + (arv_time_b - arv_time_a)*rand(user_num,5);
wfq_slot_num = zeros(user_num,1);
wfq_throughput_frame = zeros(user_num,1); %WFQ throughput per frame
if frame == 1
% the first frame, initialization
for i = 1:user_num
for j = 1:5
wfq_pk(i,j).pre_fi_time = 0;
wfq_pk(i,j).size = rtps_size(i,j);
wfq_pk(i,j).rsv_rate = rsv_rate(i);
wfq_pk(i,j).rate = rate(i,frame);
wfq_pk(i,j).arv_time = arv_time(i,j);
% compute the vitual time
temp_virtual_time = system_time + wfq_pk(i,j).arv_time;
wfq_pk(i,j).finish_time = max(wfq_pk(i,j).pre_fi_time,temp_virtual_time) + wfq_pk(i,j).size*1000/wfq_pk(i,j).rsv_rate;
end
end
else
for i = 1:user_num
for j = 1:5
% for the served pk in previous frame
if wfq_pk(i,j).size == 0
wfq_pk(i,j).size = rtps_size(i,j);
wfq_pk(i,j).pre_fi_time = wfq_pk(i,j).finish_time;
wfq_pk(i,j).arv_time = arv_time(i,j);
% compute the vitual time
temp_virtual_time = system_time + wfq_pk(i,j).arv_time;
wfq_pk(i,j).finish_time = max(wfq_pk(i,j).pre_fi_time,temp_virtual_time) + wfq_pk(i,j).size*1000/wfq_pk(i,j).rsv_rate;
end
wfq_pk(i,j).rate = rate(i,frame); % the traffic rate needs updating in each frame
end
end
end
% bubble sort finish time inside each user
for i = 1:user_num
exchange_flag = 0;
for exchange_times = 4:-1:1
for j = 1:exchange_times
if wfq_pk(i,j).finish_time > wfq_pk(i,j+1).finish_time
tmp_wfq_pk = wfq_pk(i,j);
wfq_pk(i,j) = wfq_pk(i,j+1);
wfq_pk(i,j+1) = tmp_wfq_pk;
exchange_flag = 1;
end
end
if (exchange_flag == 0)
break
end
end
end
total_slot_wfq = 330;
wfq_serv_num = 0;
wfq_all_serv = zeros(user_num,1); %the flag array to indicate whether the request of a user has been all served
while total_slot_wfq >= 0
%for WFQ find the user with smallest finish time
user_wfq_flag = 0;
fi_time_wfq_min = 1000; %NaN
for i = 1:user_num
if wfq_all_serv(i,1) == 0
if wfq_pk(i,1).finish_time < fi_time_wfq_min
fi_time_wfq_min = wfq_pk(i,1).finish_time;
user_wfq_flag = i;
end
end
end
if wfq_serv_num == user_num*5
% all request have been served
break
end
if wfq_pk(user_wfq_flag,1).size == 0
wfq_all_serv(user_wfq_flag,1) = 1;
else
wfq_slot_num(user_wfq_flag) = ceil(wfq_pk(user_wfq_flag,1).size*1000/(t_c*wfq_pk(user_wfq_flag,1).rate));
total_slot_wfq = total_slot_wfq - wfq_slot_num(user_wfq_flag);%slot num decrease
if total_slot_wfq < 0
total_slot_wfq = total_slot_wfq + wfq_slot_num(user_wfq_flag);%not enough slots
break
else
frame_wfq = fill_ofdma_frame(frame_wfq,wfq_slot_num(user_wfq_flag));
wfq_throughput = wfq_throughput + wfq_pk(user_wfq_flag,1).size;
wfq_throughput_frame(user_wfq_flag) = wfq_throughput_frame(user_wfq_flag) + wfq_pk(user_wfq_flag,1).size;
wfq_throughput_user(user_wfq_flag) = wfq_throughput_user(user_wfq_flag) + wfq_pk(user_wfq_flag,1).size;
wfq_serv_num = wfq_serv_num + 1;
end
end
% have finished serving the (user_wfq_flag,1) pk, move on
tmp_wfq_pk = wfq_pk(user_wfq_flag,1);
for j = 1:4
wfq_pk(user_wfq_flag,j) = wfq_pk(user_wfq_flag,j+1);
end
wfq_pk(user_wfq_flag,5) = tmp_wfq_pk;
wfq_pk(user_wfq_flag,5).size = 0; % means this pk has been served
end % end of while
%update system time
system_time = system_time + t_frame;
%statistics
%for RR
utilization_rr(1,frame) = (330*frame - slot_remain_rr)/(330*frame);
total_throughput_rr(1,frame) = rr_throughput/(t_frame*frame);
a = 0;
b = 0;
for j = 1:user_num
a = a + rr_throughput_user(j,1);
b = b + rr_throughput_user(j,1).^2;
end
fi_rr(1,frame) = a.^2/(user_num*b);
%for max C/I
utilization_maxci(1,frame) = (330*frame - slot_remain_maxci)/(330*frame);
total_throughput_maxci(1,frame) = maxci_throughput/(t_frame*frame);
a = 0;
b = 0;
for j = 1:user_num
a = a + maxci_throughput_user(j,1);
b = b + maxci_throughput_user(j,1).^2;
end
fi_maxci(1,frame) = a.^2/(user_num*b);
%for PF
utilization_pf(1,frame) = (330*frame - slot_remain_pf)/(330*frame);
total_throughput_pf(1,frame) = pf_throughput/(t_frame*frame);
a = 0;
b = 0;
for j = 1:user_num
a = a + pf_throughput_user(j,1);
b = b + pf_throughput_user(j,1).^2;
end
fi_pf(1,frame) = a.^2/(user_num*b);
%for WFQ
total_throughput_wfq(1,frame) = wfq_throughput/(t_frame*frame);
a = 0;
b = 0;
for j = 1:user_num
a = a + wfq_throughput_user(j,1);
b = b + wfq_throughput_user(j,1).^2;
end
fi_wfq(1,frame) = a.^2/(user_num*b);
%another way for fairness measurement: min-max ratio:fi_minmax = throughput_min/throughput_max;
%fi_rr_minmax: fairness for RR
throughput_rr_min = rr_throughput_user(1,1);
throughput_rr_max = rr_throughput_user(1,1);
for j = 1:user_num
if rr_throughput_user(j,1) > throughput_rr_max
throughput_rr_max = rr_throughput_user(j,1);
end
if rr_throughput_user(j,1) < throughput_rr_min
throughput_rr_min = rr_throughput_user(j,1);
end
end
fi_rr_minmax(1,frame) = throughput_rr_min/throughput_rr_max;
%fi_maxci_minmax: fairness for Max C/I
throughput_maxci_min = maxci_throughput_user(1,1);
throughput_maxci_max = maxci_throughput_user(1,1);
for j = 1:user_num
if maxci_throughput_user(j,1) > throughput_maxci_max
throughput_maxci_max = maxci_throughput_user(j,1);
end
if maxci_throughput_user(j,1) < throughput_maxci_min
throughput_maxci_min = maxci_throughput_user(j,1);
end
end
fi_maxci_minmax(1,frame) = throughput_maxci_min/throughput_maxci_max;
%fi_pf_minmax: fairness for PF
throughput_pf_min = pf_throughput_user(1,1);
throughput_pf_max = pf_throughput_user(1,1);
for j = 1:user_num
if pf_throughput_user(j,1) > throughput_pf_max
throughput_pf_max = pf_throughput_user(j,1);
end
if pf_throughput_user(j,1) < throughput_pf_min
throughput_pf_min = pf_throughput_user(j,1);
end
end
fi_pf_minmax(1,frame) = throughput_pf_min/throughput_pf_max;
%fi_wfq_minmax: fairness for WFQ
throughput_wfq_min = wfq_throughput_user(1,1);
throughput_wfq_max = wfq_throughput_user(1,1);
for j = 1:user_num
if wfq_throughput_user(j,1) > throughput_wfq_max
throughput_wfq_max = wfq_throughput_user(j,1);
end
if wfq_throughput_user(j,1) < throughput_wfq_min
throughput_wfq_min = wfq_throughput_user(j,1);
end
end
fi_wfq_minmax(1,frame) = throughput_wfq_min/throughput_wfq_max;
end % end of a frame
fi_rr(1,frame_num);
fi_rr_minmax(1,frame_num);
total_throughput_rr(1,frame_num);
%utilization_rr(1,frame_num)
fi_maxci(1,frame_num);
fi_maxci_minmax(1,frame_num);
total_throughput_maxci(1,frame_num);
%utilization_maxci(1,frame)
fi_pf(1,frame_num);
fi_pf_minmax(1,frame_num);
total_throughput_pf(1,frame_num);
%utilization_pf(1,frame_num)
%draw figure
%1.Jain公平性指数
frame = 1:frame_num;
figure;
plot(frame,fi_rr,'r',frame,fi_maxci,'b',frame,fi_pf,'g',frame,fi_wfq,'y');
%plot(frame,fi_rr_minmax,'r',frame,fi_maxci_minmax,'b',frame,fi_pf_minmax,'g');
legend('RR','Max C/I','PF','WFQ');
%plot(frame,fi_rr,'r',frame,fi_maxci,'b');
axis([1 frame_num 0 1]);
xlabel('帧数');
ylabel('Jain公平性指数');
%2.Minmax公平性指数
frame = 1:frame_num;
figure;
%plot(frame,fi_rr,'r',frame,fi_maxci,'b',frame,fi_pf,'g');
plot(frame,fi_rr_minmax,'r',frame,fi_maxci_minmax,'b',frame,fi_pf_minmax,'g',frame,fi_wfq_minmax,'y');
legend('RR','Max C/I','PF','WFQ');
%plot(frame,fi_rr,'r',frame,fi_maxci,'b');
axis([1 frame_num 0 1]);
xlabel('帧数');
ylabel('MinMax公平性指数');
%3.吞吐量
%plot(frame,fi_index,'o');
%hold on;
frame = 1:frame_num;
figure;
plot(frame,total_throughput_rr,'r',frame,total_throughput_maxci,'b',frame,total_throughput_pf,'g',frame,total_throughput_wfq,'y');
legend('RR','Max C/I','PF','WFQ');
%plot(frame,total_throughput_rr,'r',frame,total_throughput_maxci,'b');
%axis([1 frame_num 100000 200000]);
xlabel('帧数');
ylabel('吞吐量(bps)');
%4.每个用户的吞吐量和minimum reserved rate的比较
for i = 1:user_num
rr_throughput_user(i,1) = rr_throughput_user(i,1)/(frame_num*t_frame);
maxci_throughput_user(i,1) = maxci_throughput_user(i,1)/(frame_num*t_frame);
pf_throughput_user(i,1) = pf_throughput_user(i,1)/(frame_num*t_frame);
wfq_throughput_user(i,1) = wfq_throughput_user(i,1)/(frame_num*t_frame);
end
user = 1:user_num;
figure;
plot(user,rsv_rate,'r-*',user,rr_throughput_user,'b-o');
legend('Minimum Reserved Rate','RR');
xlabel('用户数');
ylabel('RR Rate(bps)');
figure;
plot(user,rsv_rate,'r-*',user,maxci_throughput_user,'b-o');
legend('Minimum Reserved Rate','Max C/I');
xlabel('用户数');
ylabel('Max C/I Rate(bps)');
figure;
plot(user,rsv_rate,'r-*',user,pf_throughput_user,'b-o');
legend('Minimum Reserved Rate','PF');
xlabel('用户数');
ylabel('PF Rate(bps)');
figure;
plot(user,rsv_rate,'r-*',user,wfq_throughput_user,'b-o');
legend('Minimum Reserved Rate','WFQ');
xlabel('用户数');
ylabel('WFQ Rate(bps)');
return
return