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

matlab例程

开发平台:

Matlab

  1. % Program 5-6
  2. %
  3. % Simulation program to realize DS-CDMA system
  4. %
  5. % dscdma.m
  6. %
  7. % Programmed by M.Okita and H.Harada
  8. %**************************** Preparation part *****************************
  9. sr   = 256000.0;                                                    % symbol rate
  10. ml   = 2;                                                           % number of modulation levels
  11. br   = sr * ml;                                                     % bit rate
  12. nd   = 100;                                                         % number of symbol
  13. ebn0 = 3;                                                           % Eb/No
  14. %************************** Filter initialization **************************
  15. irfn   = 21;                                                        % number of filter taps
  16. IPOINT =  8;                                                        % number of oversample
  17. alfs   =  0.5;                                                      % roll off factor
  18. [xh]   = hrollfcoef(irfn,IPOINT,sr,alfs,1);                         % T FILTER FUNCTION
  19. [xh2]  = hrollfcoef(irfn,IPOINT,sr,alfs,0);                         % R FILTER FUNCTION
  20. %********************** Spreading code initialization **********************
  21. user  = 1;                                                          % number of users
  22. seq   = 1;                                                          % 1:M-sequence  2:Gold  3:Orthogonal Gold
  23. stage = 3;                                                          % number of stages
  24. ptap1 = [1 3];                                                      % position of taps for 1st
  25. ptap2 = [2 3];                                                      % position of taps for 2nd
  26. regi1 = [1 1 1];                                                    % initial value of register for 1st
  27. regi2 = [1 1 1];                                                    % initial value of register for 2nd
  28. %******************** Generation of the spreading code *********************
  29. switch seq
  30. case 1                                                              % M-sequence
  31.     code = mseq(stage,ptap1,regi1,user);
  32. case 2                                                              % Gold sequence
  33.     m1   = mseq(stage,ptap1,regi1);
  34.     m2   = mseq(stage,ptap2,regi2);
  35.     code = goldseq(m1,m2,user);
  36. case 3                                                              % Orthogonal Gold sequence
  37.     m1   = mseq(stage,ptap1,regi1);
  38.     m2   = mseq(stage,ptap2,regi2);
  39.     code = [goldseq(m1,m2,user),zeros(user,1)];
  40. end
  41. code = code * 2 - 1;
  42. clen = length(code);
  43. %************************** Fading initialization **************************
  44. rfade  = 0;                                                         % Rayleigh fading 0:nothing 1:consider
  45. itau   = [0,8];                                                     % delay time
  46. dlvl1  = [0.0,40.0];                                                % attenuation level
  47. n0     = [6,7];                                                     % number of waves to generate fading
  48. th1    = [0.0,0.0];                                                 % initial Phase of delayed wave
  49. itnd1  = [3001,4004];                                               % set fading counter
  50. now1   = 2;                                                         % number of directwave + delayed wave
  51. tstp   = 1 / sr / IPOINT / clen;                                    % time resolution
  52. fd     = 160;                                                       % doppler frequency [Hz]
  53. flat   = 1;                                                         % flat Rayleigh environment
  54. itndel = nd * IPOINT * clen * 30;                                   % number of fading counter to skip
  55. %**************************** START CALCULATION ****************************
  56. nloop = 1000;                                                       % simulation number of times
  57. noe   = 0;
  58. nod   = 0;
  59. for ii=1:nloop
  60.     
  61. %****************************** Transmitter ********************************
  62.     data = rand(user,nd*ml) > 0.5;
  63.     
  64.     [ich, qch]  = qpskmod(data,user,nd,ml);                         % QPSK modulation
  65.     [ich1,qch1] = spread(ich,qch,code);                             % spreading
  66.     [ich2,qch2] = compoversamp2(ich1,qch1,IPOINT);                  % over sampling
  67.     [ich3,qch3] = compconv2(ich2,qch2,xh);                          % filter
  68.     
  69.     if user == 1                                                    % transmission
  70.         ich4 = ich3;
  71.         qch4 = qch3;
  72.     else
  73.         ich4 = sum(ich3);
  74.         qch4 = sum(qch3);
  75.     end
  76.     
  77. %***************************** Fading channel ******************************
  78.  
  79.     if rfade == 0
  80.         ich5 = ich4;
  81.         qch5 = qch4;
  82.     else
  83.         [ich5,qch5] = sefade(ich4,qch4,itau,dlvl1,th1,n0,itnd1, ... % fading channel
  84.                              now1,length(ich4),tstp,fd,flat);
  85.         itnd1 = itnd1 + itndel;
  86.     end
  87.     
  88. %******************************** Receiver *********************************
  89.   
  90.     spow = sum(rot90(ich3.^2 + qch3.^2)) / nd;                      % attenuation Calculation
  91.     attn = sqrt(0.5 * spow * sr / br * 10^(-ebn0/10));
  92.     
  93.     [ich6,qch6] = comb2(ich5,qch5,attn);                            % Add White Gaussian Noise (AWGN)
  94.     [ich7,qch7] = compconv2(ich6,qch6,xh2);                         % filter
  95.     
  96.     sampl = irfn * IPOINT + 1;
  97.     ich8  = ich7(:,sampl:IPOINT:IPOINT*nd*clen+sampl-1);
  98.     qch8  = qch7(:,sampl:IPOINT:IPOINT*nd*clen+sampl-1);
  99.     
  100.     [ich9 qch9] = despread(ich8,qch8,code);                         % despreading
  101.     
  102.     demodata = qpskdemod(ich9,qch9,user,nd,ml);                     % QPSK demodulation
  103.     
  104. %************************** Bit Error Rate (BER) ***************************
  105.     noe2 = sum(sum(abs(data-demodata)));
  106.     nod2 = user * nd * ml;
  107.     noe  = noe + noe2;
  108.     nod  = nod + nod2;
  109.     
  110.     fprintf('%dt%en',ii,noe2/nod2);
  111.     
  112. end
  113. %******************************** Data file ********************************
  114. ber = noe / nod;
  115. fprintf('%dt%dt%dt%en',ebn0,noe,nod,noe/nod);                   % fprintf: built in function
  116. fid = fopen('BER.dat','a');
  117. fprintf(fid,'%dt%et%ft%ftn',ebn0,noe/nod,noe,nod);             % fprintf: built in function
  118. fclose(fid);
  119. %******************************** end of file ********************************