cdma_codes.m
上传用户:pjcrystal
上传日期:2007-08-24
资源大小:5k
文件大小:2k
源码类别:

matlab例程

开发平台:

Matlab

  1.  %G:扩频增益
  2.  %K_in:小区用户数
  3.  %K_out:小区外用户数
  4.  %code_type:0=random 1=hadamard 2=Gold
  5.  % coed_seed:seed for random gen of codes
  6.  function [C]=cdma_codes(K_in,K_out,code_type,G,code_seed)
  7. % K_in=1;
  8. % K_out=0;
  9. % code_type=2;
  10. % G=31;
  11. % code_seed=rand('state');
  12.  K=K_in+K_out;
  13.  c=zeros(G,K);
  14.  switch code_type
  15.      case 0,%random
  16.          rand('state',code_seed);
  17.          c=2*round(rand(G,K))-1;
  18.      case 1,%hadamard
  19.          if rem(G,4)
  20.              error('Spreading gain must be modulo 4 for hadamard codes');
  21.          else 
  22.              codes=hadamard(G);
  23.              if K_in<=G
  24.                  c=codes(:,1:K_in);
  25.              else
  26.                  for i=1:floor(K_in/G)%floor(a)是取a整数最近的数值
  27.                      c(:,(i-1)*G+1:i*G)=codes(:,1:G)
  28.                  end;
  29.                  c(:,floor(K_in/G)*G+1:K_in)=codes(:,1:K_in-floor(K_in/G)*G);
  30.              end;
  31.              rand('state',code_seed);
  32.              c=[c codes(:,ceil(G*rand(1,K_out)))];
  33.          end
  34.              case 2,%Gold
  35.                  if G<=127
  36.                      codes=GoldLow(G,K_in+K_out);
  37.                  else
  38.                      n=log2(G+1);
  39.                      codes=Gold(n,G);
  40.                  end
  41.                  if K_in<=G
  42.                      c=codes(:,1:K_in);
  43.                  else
  44.                      for i=1:floor(K_in/G)
  45.                          c(:,(i-1)*G+1:i*G)=codes(:,1:G)
  46.                      end;
  47.                          c(:,floor(K_in)*G+1:K_in)=codes(:,1:K_in-floor(K_in/G)*G);
  48.                  end;
  49.                      rand('state',code_seed);
  50.                      c=[c codes(:,ceil(G*rand(1,K_out)))];
  51.                      otherwise
  52.                          error([Code_type',num2str(code_type),'is invalid']);
  53.                  end
  54.                  for i=1:K
  55.                      C(:,i)=c(:,i);%/norm(c(:,i));
  56.                  end