stopandwait.m~

来自「本程序是用java编写的一些关于arq协议实现的小程序」· M~ 代码 · 共 98 行

M~
98
字号

function [Umas,Pak,Ack]=stopandwait(amas,Nsimul,Ploss);

% function [Umas,Pak,Ack]=stopandwait(amas,Nsimul,Ploss);
%
% StopAndWait algoritnm simulation with MaxSeqNum=2
% 
% 
%
% Input:
%
% amas - t_prop/t_transm    -  can be an array, in which case
% simulation is repeated for each value of an array and 
% corresponding utilization is recorded in corresponding element of Umas
%
% Nsimul - number of sent frames to simulate
% time required is proportional to Nsimul
% quality of utilization estimates improves with Nsimul
% recommended Nsimul=300;
%
% Ploss - probability of packet loss (or receiving with an error)
% this probability is assumed the same for data packets and acknowledgments
%
% Output:
%
% Umas - vector containing channel utilization estimates for values of a
% in amas parameter. Length(Umas)=Length(amas)
%
% In case of the length(amas)>1 the following 2 output parameters
% output the result for the simulation for the last entry of vector amas
%
% Pak - matrix, each raw contains a signature of the simulated data packet sent.
% structure of the raw: (Pak#   Time_sent  Time_received  sequence#)
% Time_received = Inf   if the packet is lost
%
% Ack - matrix, each raw contains a signature of the simulated acknowledgment 
% packet sent.
% structure of the raw: (Ack#   Time_sent  Time_received  sequence#)
% Time_received = Inf   if the ack is lost
%
% 15/11/01
% A. Litvin
% e-mail: litvin@bu.edu
%


if length(Ploss)>1
   error('Ploss size');
end

if Ploss<0 | Ploss>1 
   error('Ploss not in limits [0,1]');
end


Umas=[];

for j=1:length(amas)
   
   a=amas(j);
   
Tprop=1;
Ttran=Tprop/a;

Pak=[];
Ack=[];
time=0;
pakn=1;
U=0;
framen=0;

for i=1:Nsimul
   gotit=rand>Ploss;
   if gotit
      Pak=[Pak; pakn time time+Tprop+Ttran framen];
      gotack=rand>Ploss;
      if gotack
         Ack=[Ack; pakn time+Tprop+Ttran time+2*Tprop+Ttran framen];
         U=U+Ttran;
         pakn=pakn+1;
         framen=framen+1;
         if framen>1 framen=0; end;
      else
         Ack=[Ack; pakn time+Tprop+Ttran Inf framen];
      end
   else
      Pak=[Pak; pakn time Inf framen];
   end
   time=time+2*Tprop+Ttran;
end;

Umas(j)=U/time;

Umasb[];
Umas = [.96 .83 .71];
end;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?