📄 tdma_test.m
字号:
clear;alpha=1;data_size=8*5*10^3;unit_pkt_size=8*(128);pkt_hdr_size=8*(12);tx_rate=250*(10^3);BER=(10^-5);PER=1-(1-BER)^(pkt_hdr_size+unit_pkt_size);% PER=0.2;unit_tx_time=(pkt_hdr_size+unit_pkt_size)/tx_rate;% Number of nodesn=10;num_sample=1000;for h=1:50 TDMA_cycle=0.1*h; TDMA_cycle_log(h)=TDMA_cycle; max_num_re_tx=3; % Accumulated tx time for a slot tx_time_slot=0; %length of unit time slot slot_unit_time=TDMA_cycle/n; if (pkt_hdr_size+unit_pkt_size)/tx_rate > slot_unit_time error (' too small time slot'); end disp('--------------------------------------------'); disp(['number of slot: ', num2str(n)]); disp(['TDMA cycle: ', num2str(TDMA_cycle)]); disp(['slot time: ', num2str(slot_unit_time),' sec']); disp(['Unit Tx time: ', num2str(unit_tx_time),' sec']); disp(['Max num of packet per slot: ', num2str(floor(slot_unit_time/unit_tx_time))]); disp('--------------------------------------------'); for k=1:num_sample num_re_tx=0; superframe=0; wait_time=0; tx_time=0; S=round(rand*n); if (S==0) S=n; end i=round(rand*n); if (i==0) i=n; end % disp(['Assigned slot number: ', num2str(S)]); pkt_size=unit_pkt_size; Tx_FIFO=data_size; u=1; while (Tx_FIFO>0) % i is time ( indexed qunatized by unit slot) slot_index=rem(i,n); if (slot_index==0) slot_index=n; end % disp(['slot index',num2str(slot_index)]); % disp(['Current slot number: ', num2str(slot_index)]); % If slot is assigned to the node, start tx if (slot_index==S)&&(superframe>0) % disp('send'); u=u+1; % disp([num2str(u),'th slot is assigned: ', num2str(S)]); % disp('Slot starts '); % disp('-------------------------------------'); remaining_slot_unit_time=slot_unit_time; while (remaining_slot_unit_time>0) &&(Tx_FIFO>0) % disp(['Tx_FIFO: ', num2str(Tx_FIFO)]); tx_time=tx_time+(pkt_hdr_size+pkt_size)/tx_rate;; % disp(['Tx starts: ', num2str(tx_time)]); % The remaining time slot decreases as sending packet remaining_slot_unit_time=remaining_slot_unit_time-unit_tx_time; % disp(['Timer slot remaining: ', num2str(remaining_slot_unit_time)]); Is_Succ=bern_gen(1- PER); %------------------------------------------------------------------ if (Is_Succ==1) if Tx_FIFO-pkt_size>=0 Tx_FIFO=Tx_FIFO-pkt_size; num_re_tx=0; end else num_re_tx=num_re_tx+1; % Packet error report and drop packet if(num_re_tx>=max_num_re_tx) Tx_FIFO=Tx_FIFO-pkt_size; num_re_tx=0; end end if Tx_FIFO-pkt_size<0 pkt_size=Tx_FIFO; end %------------------------------------------------------------------ end % disp('--------------------------------------------'); if (remaining_slot_unit_time<0) % Residual time for Tx slot goes to wait time wait_time=wait_time-remaining_slot_unit_time; % disp(['Tx residual time wait: ', num2str(-remaining_slot_unit_time)]); end % If slot is not assigned to the node, wait until next slot else % disp('waiting in slot'); % disp('wait'); wait_time=wait_time+slot_unit_time; end % if(Tx_FIFO==0) break; end if(slot_index==n) superframe=superframe+1; % disp('-------------------------------------'); end i=i+1; end wait_time_log(k)=wait_time; tx_time_log(k)=tx_time; end disp(['wait time(simul): ', num2str(mean(wait_time_log))]); disp(['tx time(simul): ', num2str(mean(tx_time))]); p=PER; Q=max_num_re_tx; G_Q=(1-p^Q)/(1-p);% G_Q=(1-Q*p^Q+(p/(1-p))*(1-p^(Q-1)))+Q*p^Q; N=floor(G_Q*(n/TDMA_cycle)*data_size*(pkt_hdr_size+unit_pkt_size)/(unit_pkt_size*tx_rate)); if N>0 Z=G_Q*data_size*(pkt_hdr_size+unit_pkt_size)/(unit_pkt_size*tx_rate*N); L=TDMA_cycle/(N)+(n-1)*TDMA_cycle/n; wait_time_thy(h)=N*L; tx_time_thy(h)=N*Z; else N=1;L=TDMA_cycle; Z=G_Q*data_size*(pkt_hdr_size+unit_pkt_size)/(unit_pkt_size*tx_rate); wait_time_thy(h)=N*L; tx_time_thy(h)=N*Z; end disp(['wait time(thy): ', num2str(N*L)]); disp(['tx time(thy): ', num2str(N*Z)]); wait_time_sim(h)=mean(wait_time_log); wait_time_std(h)=std(wait_time_log); tx_time_sim(h)=mean(tx_time_log(k)); tx_time_std(h)=std(tx_time_log(k)); N_log(h)=N;endplot( TDMA_cycle_log,wait_time_sim,'b',TDMA_cycle_log,wait_time_thy,'.-r');% hold on;% plot( TDMA_cycle_log,tx_time_sim,'b',TDMA_cycle_log,tx_time_thy,'.-r');set(gca,'FontSize',24);xlabel('TDMA cycle length');ylabel('Time duraiton, (sec)');% h1=errorbar((P),mean_num_sim, L,U,'+');% hold on;% h2=plot( (P),mean_num_sim);% set(gca,'FontSize',18);% xlabel('The maximum number of backoff, P');% ylabel('Average listening delay, L(sec)');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -