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

matlab例程

开发平台:

Matlab

  1. % Program 7-1
  2. %
  3. % dcamain.m
  4. %
  5. % Simulation program to realize DCA algorithm
  6. %
  7. % Programmed by F. Kojima
  8. %
  9. %%%%%%%%%%%%%%%% preparation part %%%%%%%%%%%%%%%%%%%%%%%%%%%%
  10. cnedge = 20.0; % CNR on cell edge (dB)
  11. cnirth = 15.0; % CNIR threshold (dB)
  12. lambda = 6.0; % average call arrival rate (times/hour)
  13. ht = 120.0; % average call holding time (second)
  14. timestep = 10; % time step of condition check (second)
  15. timeend = 5000; % time length of simulation (second)
  16. chnum = 5; % number of channels per each base station
  17. alpha = 3.5;          % pass loss factor
  18. sigma = 6.5;          % standard deviation of shadowing
  19. usernum = [5,10,15,20,25]; % number of users per cell
  20. output = zeros(4,5); % output matrix
  21. check = zeros(5,floor(timeend/timestep)); % matrix for transient statu
  22. for parameter = 1:5
  23.    
  24.    rand('state',5);
  25.    randn('state',1);
  26.    
  27.    user = usernum(parameter); %number of users per cell
  28.    
  29.    baseinfo = zeros(19, 2);
  30.    %baseinfo(cell #, informations)
  31.    %%%%%baseinfo(:, 1): x coordinates
  32.    %%%%%baseinfo(:, 2): y coordinates
  33.    
  34.    userinfo = zeros(19, user, 15);
  35.    %userinfo(cell #, user #, informations)
  36.    %%%%%userinfo(:, :, 1): x axis
  37.    %%%%%userinfo(:, :, 2): y axis
  38.    %%%%%userinfo(:, :, 3): attenuation
  39.    %%%%%userinfo(:, :, 4): usage 0->non-connected 1->connected
  40.    %%%%%userinfo(:, :, 5): call termination time
  41.    %%%%%userinfo(:, :, 6): allocated channel # 
  42.    
  43.    [baseinfo] = basest;
  44.    [wrapinfo] = wrap;
  45.    
  46.    [meshnum, meshposition] = cellmesh;
  47.    
  48.    timenow = 0;
  49.    blocknum = 0;
  50.    forcenum = 0;
  51.    callnum = 0;
  52.    users = 0; % number of connected users
  53.    
  54.    %%%%%%%%%%%%%%%% main loop part %%%%%%%%%%%%%%%%%%%%%%%%%%%%
  55.    
  56.    while timenow < timeend
  57.       
  58.       callnumold = callnum;
  59.       blocknumold = blocknum;
  60.       forcenumold = forcenum;
  61.       
  62.       %finished calls
  63.       for numcell = 1:19
  64.          for numuser = 1:user
  65.             if userinfo(numcell, numuser, 4) == 1 & userinfo(numcell, numuser, 5) < timenow
  66.                userinfo(numcell, numuser, 4) = 0;
  67.                users = users -1;
  68.             end
  69.          end
  70.       end
  71.       
  72.       %reallocation check
  73.       for numcell = 1:19
  74.          for numuser = 1:user
  75.             if userinfo(numcell, numuser,4) == 1
  76.                reallo = 0; % flag
  77.                cnirdb1 = 0.0;
  78.                dwave = userinfo(numcell, numuser, 3);
  79.                cn = power(10.0, cnedge/10.0) * dwave;
  80.                uwave = 0.0;
  81.                ch = userinfo(numcell, numuser, 6);
  82.                for around = 2:7
  83.                   othercell = wrapinfo(numcell, around);
  84.                   for other = 1:user
  85.                      if userinfo(othercell, other, 4) == 1 & userinfo(othercell, other, 6) == ch
  86.                         userposi(1,1:2) = userinfo(othercell, other, 1:2);
  87.                         here = baseinfo(numcell, :);
  88.                         there = userposi - baseinfo(othercell, :) + baseinfo(around, :) + baseinfo(numcell, :);
  89.                         uwave = uwave + dist(here, there, alpha)*shadow(sigma);
  90.                      end
  91.                   end
  92.                end % around loop
  93.                if uwave == 0
  94.                   cnirdb = 10.0*log10(cn);
  95.                else
  96.                   cnirdb = 10.0*log10(1/(uwave/dwave+1/cn));
  97.                end
  98.                if cnirdb < cnirth
  99.                   reallo = 1;
  100.                end
  101.                
  102.                if reallo == 1 
  103.                   userinfo(numcell, numuser, 4) = 0;
  104.                   users = users -1;
  105.                   succeed = 0;
  106.                   cnirdb = 0.0;
  107.                   for ch = 1:chnum
  108.                      available = 1;
  109.                      for other = 1:user
  110.                         if userinfo(numcell, other, 4) == 1 & userinfo(numcell, other, 6) == ch
  111.                            available = 0;
  112.                         end
  113.                      end
  114.                      if available == 1
  115.                         uwave = 0.0;
  116.                         for around = 2:7
  117.                            othercell = wrapinfo(numcell, around);
  118.                            for other = 1:user
  119.                               if userinfo(othercell, other, 4) == 1 & userinfo(othercell, other, 6) == ch
  120.                                  userposi(1,1:2) = userinfo(othercell, other, 1:2);
  121.                                  here = baseinfo(numcell, :);
  122.                                  there = userposi - baseinfo(othercell, :) + baseinfo(around, :) + baseinfo(numcell, :);
  123.                                  uwave = uwave + dist(here, there, alpha)*shadow(sigma);
  124.                               end
  125.                            end
  126.                         end % around loop
  127.                         if uwave == 0
  128.                            cnirdb = 10.0*log10(cn);
  129.                         else
  130.                            cnirdb = 10.0*log10(1/(uwave/dwave+1/cn));
  131.                         end
  132.                      else
  133.                         cnirdb = 0.0;
  134.                      end
  135.                      if cnirdb >= cnirth
  136.                         succeed = 1;
  137.                         users = users + 1;
  138.                         userinfo(numcell, numuser, 4) = 1;
  139.                         userinfo(numcell, numuser, 6) = ch;
  140.                         break
  141.                      end
  142.                   end % ch loop
  143.                   if succeed == 0
  144.                      forcenum = forcenum + 1;
  145.                   end
  146.                end % reallo == 1
  147.             end % connected (need to be checked)
  148.          end % user loop
  149.       end % cell loop
  150.       
  151.       %new call arrival
  152.       for numcell = 1:19
  153.          for numuser = 1:user
  154.             if userinfo(numcell, numuser, 4) == 0 & rand <= lambda*timestep/3600
  155.                callnum = callnum + 1;
  156.                mesh = floor(meshnum.*rand) +1;
  157.                while mesh > meshnum
  158.                   mesh = floor(meshnum.*rand) +1;
  159.                end
  160.                userinfo(numcell, numuser, 1:2) = baseinfo(numcell, :) + meshposition(mesh, :);
  161.                succeed = 0; % flag
  162.                cnirdb = 0.0;
  163.                userposi(1,1:2) = userinfo(numcell, numuser, 1:2);
  164.                here = baseinfo(numcell,:);
  165.                there = userposi;
  166.                dwave = dist(here, there, alpha) * shadow(sigma);
  167.                cn = power(10.0, cnedge/10.0) * dwave;
  168.                for ch = 1:chnum
  169.                   available = 1;
  170.                   for other = 1:user
  171.                      if userinfo(numcell, other, 4) == 1 & userinfo(numcell, other, 6) == ch
  172.                         available = 0;
  173.                      end
  174.                   end
  175.                   if available == 1
  176.                      
  177.                      uwave = 0.0;
  178.                      for around = 2:7
  179.                         othercell = wrapinfo(numcell, around);
  180.                         for other = 1:user
  181.                            if userinfo(othercell, other, 4) == 1 & userinfo(othercell, other, 6) == ch
  182.                               userposi(1,1:2) = userinfo(othercell, other, 1:2);
  183.                               here = baseinfo(numcell, :);
  184.                               there = userposi - baseinfo(othercell, :) + baseinfo(around, :) + baseinfo(numcell, :);
  185.                               uwave = uwave + dist(here, there, alpha)*shadow(sigma);
  186.                            end
  187.                         end
  188.                      end % around loop
  189.                      if uwave == 0
  190.                         cnirdb = 10.0*log10(cn);
  191.                      else
  192.                         cnirdb = 10.0*log10(1/(uwave/dwave+1/cn));
  193.                      end
  194.                   else
  195.                      cnirdb = 0.0;
  196.                   end
  197.                   if cnirdb >= cnirth
  198.                      succeed = 1;
  199.                      users = users + 1;
  200.                      userinfo(numcell, numuser, 3) = dwave;
  201.                      userinfo(numcell, numuser, 4) = 1;
  202.                      userinfo(numcell, numuser, 5) = timenow + holdtime(ht);
  203.                      userinfo(numcell, numuser, 6) = ch;
  204.                      break
  205.                   end
  206.                end % ch loop
  207.                if succeed == 0
  208.                   blocknum = blocknum + 1;
  209.                end
  210.             end % new call
  211.          end % user loop
  212.       end % cell loop
  213.       
  214.       fprintf('%dt%dt%dt%dt%en',parameter,timenow,callnum-callnumold,blocknum-blocknumold,blocknum/callnum);
  215.       check(parameter,timenow/timestep+1) = blocknum/callnum;
  216.       check2(parameter,timenow/timestep+1) = forcenum/(callnum-blocknum);
  217.       
  218.       timenow = timenow + timestep;
  219.    end %while loop
  220.    
  221.    %%%%%%%%%%%%%%%% output part %%%%%%%%%%%%%%%%%%%%%%%%%%%%
  222.    
  223.    output(1,parameter) = callnum;
  224.    output(2,parameter) = blocknum;
  225.    output(3,parameter) = blocknum/callnum;
  226.    output(4,parameter) = forcenum/(callnum-blocknum);
  227. end %parameter loop
  228. fid = fopen('data.txt','w');
  229. fprintf(fid,'UserNumbert');
  230. fprintf(fid,'%gt%gt%gn', usernum(1,:));
  231. fprintf(fid,'CallNumbert');
  232. fprintf(fid,'%gt%gt%gn', output(1,:));
  233. fprintf(fid,'BlockNumbert');
  234. fprintf(fid,'%gt%gt%gn', output(2,:));
  235. fprintf(fid,'BlockingProb. t');
  236. fprintf(fid,'%gt%gt%gn', output(3,:));
  237. fprintf(fid,'ForcedTerminationProb. t');
  238. fprintf(fid,'%gt%gt%gn', output(4,:));
  239. fclose(fid);
  240. %******* end of file *********