当前位置:   article > 正文

信道估计matlab代码_信道估计代码

信道估计代码

主函数:

  1. clear;clc;close all;
  2. Nfft = 256; % 子载波数量
  3. Ng = Nfft/8; % 循环前缀长度
  4. Nofdm = Nfft + Ng; % OFDM符号长度
  5. Nsym = 100;
  6. Nps = 4; % 导频间隔
  7. Np = Nfft/Nps; % 导频数
  8. Nbps = 4; % 每符号=4bit
  9. M = 2^Nbps;
  10. A = sqrt(3/2/(M-1)*1); % QAM归一化因子
  11. SNR = 30;
  12. sq2 = sqrt(2);
  13. MSE = zeros(1,6);
  14. nose = 0;
  15. for nsym = 1:Nsym
  16. Xp = 2*(randn(1,Np)>0)-1;
  17. msgint = randi([0,15],Nfft-Np,1);
  18. Data = A*qammod(msgint,M,'gray');
  19. ip=0;
  20. pilot_loc = zeros(8,1);
  21. X = zeros(Nfft,1);
  22. kk=1;
  23. for k=1:Nfft
  24. if mod(k,Nps)==1
  25. X(k,:)=Xp(floor(k/Nps)+1);
  26. pilot_loc(kk)=k;
  27. kk = kk+1;
  28. ip=ip+1;
  29. else
  30. X(k,:) = Data(k-ip,:);
  31. end
  32. end
  33. x=ifft(X,Nfft); % IFFT
  34. xt = [x(Nfft-Ng+1:Nfft);x]; % 加CP
  35. h = [(randn+1j*randn),(randn+1j*randn)/2];
  36. H = fft(h,Nfft);
  37. channel_length = length(h);
  38. H_power_dB = 10*log10(abs(H.*conj(H)));
  39. y_channel = conv(xt,h);
  40. yt = awgn(y_channel,SNR,'measured');
  41. y = yt(Ng+1:Nofdm); % 去CP
  42. Y = fft(y);
  43. for m = 1:3
  44. if m==1
  45. H_est = LS_CE(Y,Xp,pilot_loc,Nfft,Nps,'linear');
  46. method = 'LS-linear';
  47. elseif m==2
  48. H_est = LS_CE(Y,Xp,pilot_loc,Nfft,Nps,'spline');
  49. method = 'LS-spline';
  50. else
  51. H_est = MMSE_CE(Y,Xp,pilot_loc,Nfft,Nps,h,SNR);
  52. method = 'MMSE';
  53. end
  54. H_est_power_dB = 10*log10(abs(H_est.*conj(H_est)));
  55. h_est = ifft(H_est);
  56. h_DFT = h_est(1:channel_length);
  57. H_DFT = fft(h_DFT,Nfft);
  58. H_DFT_power_dB = 10*log10(abs(H_DFT.*conj(H_DFT)));
  59. if nsym==1
  60. subplot(319+2*m)
  61. plot(H_power_dB,'b','linewidth',1);
  62. hold on;
  63. plot(H_est_power_dB,'r:+','Markersize',4);
  64. axis([0 33 -10 10]);
  65. xlabel('Subcarrier Index');
  66. ylabel('Power(dB)');
  67. legend('True Channel',method);
  68. set(gca,'fontsize',9);
  69. subplot(320+2*m)
  70. plot(H_power_dB,'b','linewidth',1);
  71. hold on;
  72. plot(H_DFT_power_dB,'r:+','Markersize',4);
  73. axis([0 33 -10 10]);
  74. xlabel('Subcarrier Index');
  75. ylabel('Power(dB)');
  76. legend('True Channel','DFT');
  77. set(gca,'fontsize',9);
  78. end
  79. MSE(m) = MSE(m)+(H-H_est)*(H-H_est)';
  80. MSE(m+3) = MSE(m+3)+(H-H_DFT)*(H-H_DFT)';
  81. end
  82. Y_eq =Y'./H_est;
  83. if nsym>=Nsym-10
  84. figure
  85. subplot(211)
  86. plot(Y,'.','Markersize',5);
  87. axis([-1.5 1.5 -1.5 1.5]);
  88. set(gca,'fontsize',9);
  89. hold on;
  90. subplot(212)
  91. plot(Y_eq,'.','Markersize',5);
  92. axis([-1.5 1.5 -1.5 1.5]);
  93. set(gca,'fontsize',9);
  94. end
  95. ip=0;
  96. Data_extracted = zeros(Nfft-Np,1);
  97. for k=1:Nfft
  98. if mod(k,Nps)==1
  99. ip=ip+1;
  100. else
  101. Data_extracted(k-ip)=Y_eq(k);
  102. end
  103. end
  104. msg_detected = qamdemod(Data_extracted/A,M,'gray');
  105. nose = nose + sum(msg_detected~=msgint);
  106. MSEs = MSE/(Nfft*Nsym);
  107. end

LS算法

  1. % LS信道估计
  2. function [H_LS]=LS_CE(Y,Xp,pilot_loc,Nfft,Nps,int_opt)
  3. % 输入
  4. % Y 频域接收信号
  5. % Nfft 载波数量
  6. % Nps 导频间隔
  7. % Xp 导频信号
  8. % pilot_loc 导频位置
  9. % int_opt 插值方式
  10. % 输出
  11. % H_LS LS信道估计
  12. Np = Nfft/Nps; % 导频个数
  13. k = 1:Np;
  14. LS_est(k) = Y(pilot_loc(k))'./Xp(k);
  15. H_LS = interpolate(LS_est,pilot_loc',Nfft,int_opt);
  16. end

MMSE算法

  1. % LS信道估计
  2. function [H_MMSE]=MMSE_CE(Y,Xp,pilot_loc,Nfft,Nps,h,SNR)
  3. % 输入
  4. % Y 频域接收信号
  5. % Nfft 载波数量
  6. % Nps 导频间隔
  7. % Xp 导频信号
  8. % pilot_loc 导频位置
  9. % h 信道冲击相应
  10. % SNR 信噪比
  11. % 输出
  12. % H_MMSE LS信道估计
  13. snr = 10^(SNR*0.1);
  14. Np=Nfft/Nps;
  15. k = 1:Np;
  16. H_tilde = Y(pilot_loc(k))'./Xp(k); % LS estimate
  17. k=0:length(h)-1; %k_ts = k*ts;
  18. hh = h*h';
  19. tmp = h.*conj(h).*k; %tmp = h.*conj(h).*k_ts;
  20. r = sum(tmp)/hh;
  21. r2 = tmp*k'/hh; %r2 = tmp*k_ts.'/hh;
  22. tau_rms = sqrt(r2-r^2); % rms delay
  23. df = 1/Nfft; %1/(ts*Nfft);
  24. j2pi_tau_df = 1j*2*pi*tau_rms*df;
  25. K1 = repmat((0:Nfft-1)',1,Np);
  26. K2 = repmat((0:Np-1),Nfft,1);
  27. rf = 1./(1+j2pi_tau_df*(K1-K2*Nps));
  28. K3 = repmat((0:Np-1).',1,Np);
  29. K4 = repmat((0:Np-1),Np,1);
  30. rf2 = 1./(1+j2pi_tau_df*Nps*(K3-K4));
  31. Rhp = rf;
  32. Rpp = rf2 + eye(length(H_tilde),length(H_tilde))/snr;
  33. H_MMSE = transpose(Rhp*((Rpp)\H_tilde'));
  34. end

插值算法

  1. function [H_interpolate] = interpolate(H_est,pilot_loc,Nfft,method)
  2. if pilot_loc(1)>1
  3. slope = (H_est(2)-H_est(1))/(pilot_loc(2)-pilot_loc(1));
  4. H_est = [H_est(1)-slope*(pilot_loc(1)-1),H_est];
  5. pilot_loc = [1,pilot_loc];
  6. end
  7. if pilot_loc(end)<Nfft
  8. slope = (H_est(end)-H_est(end-1))/(pilot_loc(end)-pilot_loc(end-1));
  9. H_est = [H_est,H_est(end)+slope*(Nfft-pilot_loc(end))];
  10. pilot_loc = [pilot_loc,Nfft];
  11. end
  12. H_interpolate = interp1(pilot_loc,H_est,(1:Nfft),method);

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/476326
推荐阅读
相关标签
  

闽ICP备14008679号