CR网络和主网络的认知无线电切换算法
1. CR網(wǎng)絡和主網(wǎng)絡(授權網(wǎng)絡)共同存在于同一區(qū)域,并且使用同一頻段。假設該頻段共有N個信道,每個主用戶或CR用戶每次接入只占用一個信道。若所有信道均被主用戶占用,此時CR用戶到達就被阻塞。若CR用戶正在使用的信道有主用戶出現(xiàn),此時CR用戶被迫中斷,并進入緩存區(qū)排隊等待空閑可用信道以繼續(xù)剛被中斷的通信,若等待超過一定時限,則判定CR用戶強制中斷退離緩存區(qū)。
2. 主用戶和次用戶的到達服從泊松分布,參數(shù)分別為lambdap和lambdas,平均服務時間服從參數(shù)為mup和mus的負指數(shù)分布
3. 對次用戶而言,主用戶搶占優(yōu)先。總共有N個信道,也就是最多可以有N個主用戶搶占所有信道,故Z隊列的長度不會超過N,這里給定Z隊列長度為N。
4. 假設初始狀態(tài)所有N個信道均空閑,次用戶理想感知,感知延時為0.005
clear?
clc?
rand('twister',0);
blockpu=[];
blocksu=[];
for N=3:2:7
? ? block=[];
? ? for lambdap =0.01:0.01:0.5
%*****************************************?
?
%初始化?
%*****************************************?
a = 10; %主用戶數(shù)量
b = 100; %次用戶數(shù)量
%N =2 ?%Z隊列最大長度/總的信道數(shù)
%Tao=5;%切換時延門限Tao
A = [ ]; %某主用戶到達時刻占用信道序號的集合
B = [ ]; %某次用戶到達時刻占用信道序號的集合
C = [ ]; %切換用戶占用的當前所有信道序號集合
D = [ ]; %某次用戶到達時刻主用戶占用信道集合
member = [ ];
member_CR = [ ];
j1=1;
%主用戶參數(shù)*****************************************?
%lambdap = 0.1;?
mup =0.6; %主用戶到達率與服務率
arr_meanp = 1/lambdap;?
ser_meanp = 1/mup;%主用戶平均到達時間與平均服務時間?
arr_nump = a; %round(Total_time*lambdap*2);
tp = zeros(6,arr_nump);?
tp(1,:) = exprnd(arr_meanp,1,arr_nump); %按負指數(shù)分布產生各主用戶到達時間間隔?
tp(1,:) = cumsum(tp(1,:)); %各主用戶的到達時刻等于時間間隔的累積和?
tp(2,:) = exprnd(ser_meanp,1,arr_nump); %按負指數(shù)分布產生各主用戶服務時間
%次用戶參數(shù)*****************************************?
lambdas =0.4;?
mus =0.7; %次用戶到達率與服務率
arr_means = 1/lambdas;?
ser_means = 1/mus; %次用戶平均到達時間與平均服務時間
arr_nums = b; ?
ts = zeros(6,arr_nums);?
ts(1,:) = exprnd(arr_means,1,arr_nums); %按負指數(shù)分布產生各次用戶達到時間間隔
ts(1,:) = cumsum(ts(1,:)); %各次用戶的到達時刻等于時間間隔的累積和?
ts(2,:) = exprnd(ser_means,1,arr_nums); %按負指數(shù)分布產生各次用戶服務時間?
%切換用戶參數(shù)*****************************************待計算lambdah和muh
arr_numh = 10; %切換用戶排隊長度設置
th = zeros(6,arr_numh);?
tsh=[];
%*****************************************?
%計算第1個主用戶的信息?
%*****************************************?
if arr_nump>=1
tp(3,1) = 0; %第1個主用戶進入系統(tǒng)后直接接受服務,無需等待?
n = ceil(rand*N); %產生一個在N范圍內的隨機數(shù)作為接入的信道序號
tp(4,1) = tp(1,1)+tp(2,1); %其離開時刻等于其到達時刻與服務時間之和?
tp(5,1) = 1; %其肯定被系統(tǒng)接納,此時系統(tǒng)內共有1個主用戶,故標志位置1?
tp(6,1) = n; %依次記錄主用戶占用信道的序號
A=[A n];
member = [1]; %其進入系統(tǒng)后,系統(tǒng)內已有成員序號為1?
else
? ? !echo No Primary Users!!!!
end
%*****************************************?
if arr_nums>=1
if arr_nump>=1
k1 = sum(ts(1,:) < tp(1,1));
if k1~=0
? for i =1:k1
? ? if i==1
? ? ? ? ts(3,i) = 0.005; %其等待時間為 0
? ? ? ? ts(4,i) = ts(1,i)+ts(2,i)+ts(3,i); %其離開時刻等于到達時刻與服務時間之和?
? ? ? ? ts(5,i) = 1; %其標志位置 1 ,即次用戶在用當前信道
? ? ? ? m = ceil(rand*N); %產生一個在N范圍內的隨機數(shù)作為接入的信道序號
? ? ? ? ts(6,i) = m; %依次記錄次用戶占用信道的序號
? ? ? ? B=[B m];
? ? ? ? member_CR =[1];
? ? ? ? handoff1=find(tp(1,:)>ts(1,i));
? ? ? ? handoff2=find(tp(1,:)<ts(4,i));
? ? ? ? if isempty(handoff1)==0&&isempty(handoff2)==0
? ? ? ? handoff3=setdiff(handoff1,handoff2);%當前次用戶通信期間(時間段)到達的主用戶
? ? ? ? handoff=setdiff(handoff1,handoff3);
? ? ? ? handoff4=[];
? ? ? ? for puid=1:length(handoff)
? ? ? ? ? ?if tp(6,handoff(puid))==ts(6,i)
? ? ? ? ? ? ? ?handoff4=[handoff4,handoff(puid)];
? ? ? ? ? ? ? ?break;
? ? ? ? ? ?end
? ? ? ? end ? ?
? ? ? ? if isempty(handoff4)==0
? ? ? ? ? ? if j1<=arr_numh ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? th(1,j1)=tp(1,handoff4(1));
? ? ? ? ? ? ? ? th(2,j1)=ts(4,i)-tp(1,handoff4(1));
? ? ? ? ? ? ? ? ts(4,i)=tp(1,handoff4(1)); ?
? ? ? ? ? ? ? ? tsh=ts(:,i); ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? %切換到其他可用信道
? ? ? ? ? ? ? ? num_arrivep=find(tp(1,:)<=th(1,j1));
? ? ? ? ? ? ? ? num_leavep=find(tp(4,:)>=th(1,j1));
? ? ? ? ? ? ? ? num_present1=setdiff(num_arrivep,num_leavep);
? ? ? ? ? ? ? ? num_present2=setdiff(num_arrivep,num_present1);%當前在的主用戶
? ? ? ? ? ? ? ? num_arrives=find(ts(1,:)<th(1,j1));
? ? ? ? ? ? ? ? num_leaves=find(ts(4,:)>th(1,j1));
? ? ? ? ? ? ? ? num_present3=setdiff(num_arrives,num_leaves);
? ? ? ? ? ? ? ? num_present4=setdiff(num_arrives,num_present3);%當前在的次用戶
? ? ? ? ? ? ? ? num_present6=[];
wait=0;
? ?if j1>1
? ? ? j2=1:j1-1;
? ? ? num_arriveh=find(th(1,j2)<th(1,j1));
? ? ? num_leaveh=find(th(4,j2)>th(1,j1));
? ? ? num_present5=setdiff(num_arriveh,num_leaveh);
? ? ? num_present6=setdiff(num_arriveh,num_present5);%當前在的切換用戶
? ? ? for j3=1:length(num_present6)
? ? ? wait=wait+th(3,num_present6(j3));
? ? ? end
? ? ? th(3,j1)=wait;%切換用戶排隊等待時間
? ?end
? ? ? ? ? ? ? ? if length(num_present2)==N
? ? ? ? ? ? ? ? ? ? wait1=tp(4,num_present2(1));
? ? ? ? ? ? ? ? ? ? for k=1:length(num_present2)
? ? ? ? ? ? ? ? ? ? wait1=min(tp(4,num_present2(k)),wait1);
? ? ? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? ? ? for k=1:length(num_present2)
? ? ? ? ? ? ? ? ? ? ? ? if wait1==tp(4,num_present2(k))
? ? ? ? ? ? ? ? ? ? ? ? ? ? wait2=num_present2(k);
? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? ? ?th(3,j1)=wait1-th(1,j1)+th(3,j1);
? ? ? ? ? ? ? ? ? ?th(4,j1)=th(1,j1)+th(2,j1)+th(3,j1);
? ? ? ? ? ? ? ? ? ? th(5,j1)=1;
? ? ? ? ? ? ? ? ? ? th(6,j1)=tp(6,wait2);
? ? ? ? ? ? ? ? ? ? tsh=[tsh,th(:,j1)];
? ? ? ? ? ? ? ? elseif isempty(num_present2)&&length(num_present4)==N
? ? ? ? ? ? ? ? ? ? wait1=ts(4,num_present4(1));
? ? ? ? ? ? ? ? ? ? for k=1:length(num_present4)
? ? ? ? ? ? ? ? ? ? wait1=min(ts(4,num_present4(k)),wait1);
? ? ? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? ? ? for k=1:length(num_present4)
? ? ? ? ? ? ? ? ? ? ? ? if wait1==ts(4,num_present4(k))
? ? ? ? ? ? ? ? ? ? ? ? ? ? wait3=num_present4(k);
? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? ? ? th(3,j1)=wait1-th(1,j1)+th(3,j1);
? ? ? ? ? ? ? ? ? ? th(4,j1)=th(1,j1)+th(2,j1)+th(3,j1);
? ? ? ? ? ? ? ? ? ? th(5,j1)=1;
? ? ? ? ? ? ? ? ? ? th(6,j1)=ts(6,wait3);
? ? ? ? ? ? ? ? ? ? tsh=[tsh,th(:,j1)];
? ? ? ? ? ? ? ? elseif length(num_present2)+length(num_present4)==N&&isempty(num_present2)==0&&isempty(num_present4)==0
? ? ? ? ? ? ? ? ? ? wait1=tp(4,num_present2(1));
? ? ? ? ? ? ? ? ? ? for k=1:length(num_present2)
? ? ? ? ? ? ? ? ? ? wait1=min(tp(4,num_present2(k)),wait1);
? ? ? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? ? ? for k=1:length(num_present4)
? ? ? ? ? ? ? ? ? ? wait1=min(ts(4,num_present4(k)),wait1);
? ? ? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? ? ? tps=[tp(:,num_present2),ts(:,num_present4)];
? ? ? ? ? ? ? ? ? ? for k=1:length(tps)
? ? ? ? ? ? ? ? ? ? ? ?if wait1==tps(4,k)
? ? ? ? ? ? ? ? ? ? ? ?wait4=k;
? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ? ? ?end
? ? ? ? ? ? ? ? ? ? end?
? ? ? ? ? ? ? ? ? ? th(3,j1)=wait1-th(1,j1)+th(3,j1);
? ? ? ? ? ? ? ? ? ? th(4,j1)=th(1,j1)+th(2,j1)+th(3,j1);
? ? ? ? ? ? ? ? ? ? th(5,j1)=1;
? ? ? ? ? ? ? ? ? ? th(6,j1)=tps(6,wait4);
? ? ? ? ? ? ? ? ? ? tsh=[tsh,th(:,j1)];
? ? ? ? ? ? ? ? elseif length(num_present2)+length(num_present4)<N
? ? ? ? ? ? ? ? ? ? channel1=[tp(6,num_present2),ts(6,num_present4)];
? ? ? ? ? ? ? ? ? ? channel=setdiff([1:N],channel1);
? ? ? ? ? ? ? ? ? ? th(3,j1)=0.006+th(3,j1);
? ? ? ? ? ? ? ? ? ? th(4,j1)=th(1,j1)+th(2,j1)+th(3,j1);
? ? ? ? ? ? ? ? ? ? th(5,j1)=1;
? ? ? ? ? ? ? ? ? ? th(6,j1)=channel(1);
? ? ? ? ? ? ? ? ? ? tsh=[tsh,th(:,j1)];
? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? j1=j1+1;
? ? ? ? ? ? end
? ? ? ? end ? ? ? ??
? ? ? ? end
? ? elseif i >= 2
? ? ? ? j=1:i-1;
? ? ? ? number_CR = find(ts(4,j) > ts(1,i)); ?%當前占用信道的次用戶個數(shù)
? ? ? ? [th1,th2]=find(th==0);
? ? ? ? th(:,th2)=[];
? ? ? ? if isempty(number_CR)&&isempty(th)
? ? ? ? ?m = ceil(rand*N); %產生一個在N范圍內的隨機數(shù)作為接入的信道序號
? ? ? ? ?ts(3,i) = 0.005;
? ? ? ? ?ts(4,i) = ts(1,i)+ts(2,i)+ts(3,i); %其離開時刻等于到達時刻與服務時間之和
? ? ? ? ?ts(5,i) = 1; %其標志位置 1 ,即次用戶在用當前信道
? ? ? ? ?ts(6,i) = m; %依次記錄次用戶占用信道的序號
? ? ? ? ?B=[B m];?
? ? ? ? ?member_CR = [member_CR,i];
? ? ? ? ?handoff1=find(tp(1,:)>ts(1,i));
? ? ? ? handoff2=find(tp(1,:)<ts(4,i));
? ? ? ? if isempty(handoff1)==0&&isempty(handoff2)==0
? ? ? ? handoff3=setdiff(handoff1,handoff2);%當前次用戶通信期間(時間段)到達的主用戶
? ? ? ? handoff=setdiff(handoff1,handoff3);
? ? ? ? handoff4=[];
? ? ? ? for puid=1:length(handoff)
? ? ? ? ? ?if tp(6,handoff(puid))==ts(6,i)
? ? ? ? ? ? ? ?handoff4=[handoff4,handoff(puid)];
? ? ? ? ? ? ? ?break;
? ? ? ? ? ?end
? ? ? ? end ? ?
? ? ? ? if isempty(handoff4)==0
? ? ? ? ? ? if j1<=arr_numh
? ? ? ? ? ? th(1,j1)=tp(1,handoff4(1));
? ? ? ? ? ? th(2,j1)=ts(4,i)-tp(1,handoff4(1));
? ? ? ? ? ? ts(4,i)=tp(1,handoff4(1));
? ? ? ? ? ? tsh=[tsh,ts(:,i)];?
? ? ? ? ? ? ?%切換到其他可用信道
? ? ? ? ? ? ? ? num_arrivep=find(tp(1,:)<=th(1,j1));
? ? ? ? ? ? ? ? num_leavep=find(tp(4,:)>=th(1,j1));
? ? ? ? ? ? ? ? num_present1=setdiff(num_arrivep,num_leavep);
? ? ? ? ? ? ? ? num_present2=setdiff(num_arrivep,num_present1);%當前在的主用戶
? ? ? ? ? ? ? ? num_arrives=find(ts(1,:)<th(1,j1));
? ? ? ? ? ? ? ? num_leaves=find(ts(4,:)>th(1,j1));
? ? ? ? ? ? ? ? num_present3=setdiff(num_arrives,num_leaves);
? ? ? ? ? ? ? ? num_present4=setdiff(num_arrives,num_present3);%當前在的次用戶
? ? ? ? ? ? ? ? num_present6=[];
wait=0;
? ?if j1>1
? ? ? j2=1:j1-1;
? ? ? num_arriveh=find(th(1,j2)<th(1,j1));
? ? ? num_leaveh=find(th(4,j2)>th(1,j1));
? ? ? num_present5=setdiff(num_arriveh,num_leaveh);
? ? ? num_present6=setdiff(num_arriveh,num_present5);%當前在的切換用戶
? ? ? for j3=1:length(num_present6)
? ? ? wait=wait+th(3,num_present6(j3));
? ? ? end
? ? ? th(3,j1)=wait;%切換用戶排隊等待時間
? ?end
? ? ? ? ? ? ? ? if length(num_present2)==N
? ? ? ? ? ? ? ? ? ? wait1=tp(4,num_present2(1));
? ? ? ? ? ? ? ? ? ? for k=1:length(num_present2)
? ? ? ? ? ? ? ? ? ? wait1=min(tp(4,num_present2(k)),wait1);
? ? ? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? ? ? for k=1:length(num_present2)
? ? ? ? ? ? ? ? ? ? ? ? if wait1==tp(4,num_present2(k))
? ? ? ? ? ? ? ? ? ? ? ? ? ? wait2=num_present2(k);
? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? ? ?th(3,j1)=wait1-th(1,j1)+th(3,j1);
? ? ? ? ? ? ? ? ? ?th(4,j1)=th(1,j1)+th(2,j1)+th(3,j1);
? ? ? ? ? ? ? ? ? ? th(5,j1)=1;
? ? ? ? ? ? ? ? ? ? th(6,j1)=tp(6,wait2);
? ? ? ? ? ? ? ? ? ? tsh=[tsh,th(:,j1)];
? ? ? ? ? ? ? ? elseif isempty(num_present2)&&length(num_present4)==N
? ? ? ? ? ? ? ? ? ? wait1=ts(4,num_present4(1));
? ? ? ? ? ? ? ? ? ? for k=1:length(num_present4)
? ? ? ? ? ? ? ? ? ? wait1=min(ts(4,num_present4(k)),wait1);
? ? ? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? ? ? for k=1:length(num_present4)
? ? ? ? ? ? ? ? ? ? ? ? if wait1==ts(4,num_present4(k))
? ? ? ? ? ? ? ? ? ? ? ? ? ? wait3=num_present4(k);
? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? ? ? th(3,j1)=wait1-th(1,j1)+th(3,j1);
? ? ? ? ? ? ? ? ? ? th(4,j1)=th(1,j1)+th(2,j1)+th(3,j1);
? ? ? ? ? ? ? ? ? ? th(5,j1)=1;
? ? ? ? ? ? ? ? ? ? th(6,j1)=ts(6,wait3);
? ? ? ? ? ? ? ? ? ? tsh=[tsh,th(:,j1)];
? ? ? ? ? ? ? ? elseif length(num_present2)+length(num_present4)==N&&isempty(num_present2)==0&&isempty(num........................
D101
?
總結
以上是生活随笔為你收集整理的CR网络和主网络的认知无线电切换算法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [jQuery] 说说看jQuery的选
- 下一篇: [vue] EventBus注册在全局上