main.m
上传用户:m_sun_001
上传日期:2014-07-30
资源大小:1115k
文件大小:6k
源码类别:

matlab例程

开发平台:

Matlab

  1. % Program 6-1
  2. % main.m
  3. %
  4. % Packet communication system
  5. %
  6. % MATLAB version
  7. % Programmed by M.Okita
  8. % Checked by H.Harada
  9. %
  10. clear;
  11.                                                 % definition of the global variable
  12. global STANDBY TRANSMIT COLLISION PERMIT
  13. global Srate Plen Ttime Dtime
  14. global Mnum Mplen Mstime Mstate
  15. global Tint Rint
  16. global Spnum Splen Tplen Wtime
  17. STANDBY    = 0;                                 % definition of the fixed number
  18. TRANSMIT   = 1;
  19. COLLISION  = 2;
  20. PERMIT     = 3;
  21.                                                 % definition of the protocol
  22. protocol = { 'paloha' ...                     % Pure ALOHA        : pno = 1
  23.              'saloha' ...                     % Slotted ALOHA     : pno = 2
  24.              'npcsma' ...                     % np-CSMA           : pno = 3
  25.              'snpisma' ...                     % Slotted np-ISMA   : pno = 4
  26.            };
  27.                                                 % definition of communication channel
  28. brate = 512e3;                                  % bit rate
  29. Srate = 256e3;                                  % symbol rate
  30. Plen  = 128;                                    % length of a packet
  31. Dtime = 0.01;                                   % normalized propagation delay
  32. alfa  = 3;                                      % decline fixed number of propagation loss
  33. sigma = 6;                                      % standard deviation of shadowing [dB]
  34.                                                 % definition of the access point
  35. r   = 100;                                      % service area radius [m]
  36. bxy = [0, 0, 5];                                % position of the access point (x,y,z)[m]
  37. tcn = 10;                                       % capture ratio [dB]
  38.                                                 % definition of the access terminals
  39. Mnum  = 100;                                    % number of the access terminal
  40. mcn   = 30;                                     % C/N at the access point when transmitted from area edge
  41.                                                 % simulation condition
  42. pno     = 1;                                    % protocol number
  43. capture = 0;                                    % capture effect  0:nothing  1:consider
  44. spend   = 10000;                                % number of packets that simulation is finished
  45. outfile = 'test.dat';                           % result output file name
  46. Ttime = Plen / Srate;                           % transmission time of one packet
  47. mpow  = 10^(mcn/10) * sqrt(r^2+bxy(3)^2)^alfa;  % true value of C/N
  48. fid = fopen(outfile,'w');
  49. fprintf(fid,'Protocol                = %dn',pno);
  50. fprintf(fid,'Capture                 = %dn',capture);
  51. fprintf(fid,'Normalize_delay_time    = %fn',Dtime);
  52. fprintf(fid,'Bit_rate           (bps)= %dn',brate);
  53. fprintf(fid,'Symbol_rate        (sps)= %dn',Srate);
  54. fprintf(fid,'Length_of_Packet   (sbl)= %dn',Plen);
  55. fprintf(fid,'Number_of_mobile_station= %dn',Mnum);
  56. fprintf(fid,'Transmission_power (C/N)= %fn',mcn);
  57. fprintf(fid,'Capture_ratio       (dB)= %fn',tcn);
  58. fprintf(fid,'Number_of_Packet        = %dn',spend);
  59. fprintf('n********* Simulation Start *********nn');
  60. if capture == 0
  61.     fprintf(' %s without capture effectnn',char(protocol(pno)));
  62. else
  63.     fprintf(' %s with capture effectnn',char(protocol(pno)));
  64. end
  65. mxy  = position(r,Mnum,0);                      % positioning of the access terminals
  66. randn('state',sum(100*clock));                  % resetting of the random table
  67. mrnd = randn(1,Mnum);                           % decision of the shadowing
  68. for G=[0.1:0.1:1,1.2:0.2:2]                     % offered traffic
  69.     if G >= Mnum
  70.         break
  71.     end
  72.     
  73.     Tint  = -Ttime / log(1-G/Mnum);             % expectation value of the packet generation interval
  74.     Rint  = Tint;                               % expectation value of the packet resending interval
  75.     Spnum = 0;
  76.     Splen = 0;
  77.     Tplen = 0;
  78.     Wtime = 0;
  79.     
  80.     now_time = feval(char(protocol(pno)),-1);   % initialize of the access terminals
  81.     
  82.     while 1
  83.         next_time = feval(char(protocol(pno)),now_time);
  84.         if Spnum >= spend
  85.             break
  86.         end
  87.         idx = find(Mstate==TRANSMIT | Mstate==COLLISION);
  88.         if capture == 0                         % without capture effect
  89.             if length(idx) > 1
  90.                 Mstate(idx) = COLLISION;        % collision occurs
  91.             end
  92.         else                                    % with capture effect
  93.             if length(idx) > 1
  94.                 dxy  = distance(bxy,mxy(idx,:),1);                      % calculation of the distance
  95.                 pow  = mpow * dxy.^-alfa .* 10.^(sigma/10*mrnd(idx));   % calculation of received power
  96.                 [maxp no] = max(pow);
  97.                 if Mstate(idx(no)) == TRANSMIT
  98.                     if length(idx) == 1
  99.                         cn = 10 * log10(maxp);
  100.                     else
  101.                         cn = 10 * log10(maxp/(sum(pow)-maxp+1));
  102.                     end
  103.                     Mstate(idx) = COLLISION;
  104.                     if cn >= tcn                    % received power larger than capture ratio
  105.                         Mstate(idx(no)) = TRANSMIT; % transmitting success
  106.                     end
  107.                 else
  108.                     Mstate(idx) = COLLISION;
  109.                 end
  110.             end
  111.         end
  112.         now_time = next_time;                       % time is advanced until the next state change time
  113.     end
  114.     
  115.     traffic = Tplen / Srate / now_time;             % calculation of the traffic
  116.     ts      = theorys(pno,traffic,Dtime);           % calculation of the theory value of the throughput
  117.     fprintf(fid,'%ft%ft%ft%ft%ft%ft%fn',G,now_time   ...
  118.                ,Splen/Srate,Tplen/Srate,Wtime,Tint,Rint);
  119.     fprintf('G=%ftS=%ftTS=%fn',traffic,Splen/Srate/now_time,ts);
  120. end
  121. fprintf('n********** Simulation End **********nn');
  122. fclose(fid);
  123. graph(outfile);
  124. %%%%%%%%%%%%%%%%%%%%%% end of file %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%