OFDM_LS_directly.m
上传用户:dgwtdq
上传日期:2013-04-19
资源大小:2k
文件大小:4k
源码类别:

matlab例程

开发平台:

Matlab

  1. %16QAM-OFDM system;
  2. clear all;
  3. clear all;
  4. N = 1024;                                                        %the total number of all subcarriers
  5. Np = 128;                                                         %the number of pilot carriers
  6. B = 2e6;                                                         %bandwidth 2MHz
  7. T = 1/B;
  8. Ng = 256;                                                        %the length of Guard Interval
  9. Xp = 3 + 1.0*j ;                                                      %pilot value
  10. t= 0;  % erro code number
  11. for u = 1:20                                             % run 20 times
  12. %-------------------------------------Data generator--------------------------------------------------
  13. xr1 = rand(1,N-Np);
  14. Xre = (-3) * (xr1 < 0.25) + (-1) * (0.25 <= xr1 & xr1 < 0.5)...
  15.     + (0.5 <= xr1 & xr1 < 0.75) + 3 * (0.75 <= xr1 & xr1 < 1);
  16. xr2 = rand(1,N-Np);
  17. Xim = (-3) * (xr2< 0.25) + (-1) * (0.25 <= xr2 & xr2 < 0.5)...
  18.     + (0.5 <= xr2 & xr2 < 0.75) + 3 * (0.75 <= xr2 & xr2 < 1);
  19. X = complex(Xre,Xim);                                            %16QAM
  20. % Xre = (-1) * (xr1 < 0.5) + 1 * (0.5 <= xr1 & xr1 < 1);
  21. % xr2 = rand(1,N-Np);
  22. % Xim = (-1) * (xr2< 0.5) + 1 * (0.5 <= xr2 & xr2 < 1);
  23. % X = complex(Xre,Xim);                                            %QPSK
  24. %----------------------------------  insert pilot in carriers    -------------------------------------
  25. % pilots locate at 8*i + 3;
  26. Xc = zeros(1,N);                                                 %the carrier value
  27. m = 0;
  28. for i = 1 : N
  29.     if(mod(i,8) == 3)
  30.          Xc(i) = Xp;
  31.          m = m + 1;
  32.     else
  33.         Xc(i) = X(i - m);
  34.     end
  35. end
  36. %----------------------------------OFDM modulation-------------------------------------------------------
  37. Ym = ifft(Xc,N);
  38. %----------------------------------Insert GI  CP---------------------------------------------------------
  39. Y = [Ym(769:N),Ym];
  40. %----------------------------------pass channel-----------------------------------------------------------
  41. Yc = ray_channel(Y,T);
  42. %---------------------------------add noice -------------------------------------------------------------
  43. SNR = 40;
  44. % for S=1:21
  45.   %SNR= 10log10(Es/N0)
  46. % N0 = var(Yc)/(10^(SNR/10));
  47. % sgma = sqrt(N0/2);
  48. % Yawgn=Yc+sgma*randn(1,length(Yc))+sgma*randn(1,length(Yc))*j;
  49. Yawgn = awgn(Yc,SNR,'measured');
  50. %-------------------------------------------------------------------------------------------------------     
  51. % ----------------------------------------------OFDM RECEIVER-------------------------------------------  
  52. %----------------------------------------------OFDM demodulation----------------------------------------
  53. Yr = fft(Yawgn,N);
  54. %-----------------------------------------------Pilot signal Extraction---------------------------------
  55. c = 1;
  56. for i = 3:8:N
  57.     Yp(c) = Yr(i);
  58.     c = c + 1;
  59. end
  60. %----------------------------------- Pilot signal Estimation with LS Algorithm -------------------------   
  61. Hp = Yp ./ Xp;
  62. % figure(1)
  63. % plot(abs(Hp))
  64. %---------------------------------Interpolation Appoach-----------------------------------------------------
  65. pilot_loca = 3:8:N;
  66. Hk = interp1(pilot_loca,Hp,1:N);
  67. H =  fft(Yc,N) ./ fft(Ym,N);
  68. figure(3)
  69. plot(abs(Hk))
  70. hold on
  71. plot(abs(H),'r')
  72. %-------------------------------------data correction ------------------------------------------------------
  73. Xcc = Yr ;
  74. Xcc_line = Yr ./ Hk;
  75. figure(4)
  76. plot(Xcc_line,'+')
  77. hold on
  78. plot(X,'r*')
  79. %----------------------------------------16QAM demodulation--------------
  80. Xdre = zeros(1,N);
  81. Xdim = zeros(1,N);
  82. for i = 1 :N
  83.     if(real(Xcc_line(i)) < -2)
  84.         Xdre(i) = -3;
  85.     elseif((real(Xcc_line(i)) >= -2)&(real(Xcc_line(i)) < 0))
  86.         Xdre(i) = -1;
  87.     elseif((real(Xcc_line(i)) >= 0)&(real(Xcc_line(i)) < 2))
  88.         Xdre(i) = 1;    
  89.     else
  90.         Xdre(i) = 3;
  91.     end
  92. end
  93. for i = 1 :N
  94.     if(imag(Xcc_line(i)) < -2)
  95.         Xdim(i) = -3;
  96.     elseif((imag(Xcc_line(i)) >= -2)&(imag(Xcc_line(i)) < 0))
  97.         Xdim(i) = -1;
  98.     elseif((imag(Xcc_line(i)) >= 0)&(imag(Xcc_line(i)) < 2))
  99.         Xdim(i) = 1;    
  100.     else
  101.         Xdim(i) = 3;
  102.     end
  103. end
  104.  Xd = complex(Xdre,Xdim); 
  105. Xq = zeros(1,N-Np);
  106. m = 0;
  107. for i = 1 : N;
  108.     if(mod(i,8) == 3)
  109.         m = m + 1;
  110.    else
  111.     Xq(i - m) = Xd(i) ;
  112.     end
  113. end
  114. for i= 1:Np
  115.     if(Xq(i) ~= X(i))
  116.         t = t + 1;
  117.     end
  118. end
  119. end
  120. BER = t/(u * (N-Np));
  121. % Xq(1:10)'
  122. % X(1:10)'
  123. %Xcc_lowpass(1:10)
  124. BER
  125. SNR
  126. % t = t/u
  127. % SNR
  128. % figure(10)
  129. % plot(SNR,abs(BER))