- 经验
- 9
- 分贝
- 0
- 家园分
- 29
- 在线时间:
- 6 小时
- 最后登录:
- 2017-3-24
- 帖子:
- 6
- 精华:
- 0
- 注册时间:
- 2015-9-25
- UID:
- 1157171
注册:2015-9-251
|
发表于 2016-11-1 09:01:15
|显示全部楼层
主要仿真代码
%信道估计
%基于 LS和DFT 对信道进行估计用线性插入
clear all;clc;
Nfft=32;Ng=Nfft/8;Nofdm=Nfft+Ng;
pilot_loc=[1 5 9 13 17 21 25 29 32];%l插入位置
Nps=length(pilot_loc);%每个ofdm的插入序列
Nbps=4;M=2^Nbps;%调制方式
SNR=30;
Xp=2*(randn(1,Nps)>0)-1;%插入序列的产生
msgint=randint(1,Nfft-Nps,M);%随机产生比特流
data=qammod(msgint,M);%16QAM调制
%插入导频
X=[Xp(1),data(1:3),Xp(2),data(4:6),Xp(3),data(7:9),Xp(4),data(10:12),Xp(5),data(13:15),Xp(6),data(16:18),Xp(7),data(19:21),Xp(8),data(22:23),Xp(9)];
%OFDM 的调制
x=ifft(X,Nfft);xt=[x(Nfft-Ng+1:Nfft) x];%傅里叶逆变换和加入循环前缀
h=[(randn+1i*randn),(randn+1i*randn)/2];%产生两径的信道
%信道的频域和长度
H=fft(h,Nfft);ch_length=length(h);
H_power_dB=10*log10(abs(H.*conj(H)));
y_channel=conv(xt,h);%通过信道的信号
yt=awgn(y_channel,SNR,'measured');
y=yt(Ng+1:Nofdm);Y=fft(y);%移去 CP 和FFTLS
%基于LS 信道估计
H_est1=LS_CE(Y,Xp,pilot_loc,Nfft);
H_est_power_dB1=10*log10(abs(H_est1.*conj(H_est1)));
%基于DFT的信道估计
h_est2=ifft(H_est1);h_DFT2=h_est2(1:ch_length);
H_est2=fft(h_DFT2,Nfft);
H_est_power_dB2=10*log10(abs(H_est2.*conj(H_est2)));
%比较LS和DFT两种估计的性能
subplot(2,1,1);
plot(1:Nfft,H_power_dB,'-',1:Nfft,H_est_power_dB1,'s');
legend('True Channel','LS');
xlabel('subcarrier index');ylabel('Power[dB]');
subplot(2,1,2);
plot(1:Nfft,H_power_dB,'-',1:Nfft,H_est_power_dB2,'s');
legend('True Channel','LS with DFT');
xlabel('subcarrier index');ylabel('Power[dB]');
function H_LS=LS_CE(Y,Xp,pilot_loc,Nfft)
%LS 信道估计函数
%输入
%Y为频域接收信号
%Xp=插入信号
%pilot_loc=插入导频的位置
%N为FFT的大小
k=1:length(pilot_loc);
LS_est(k)=Y(pilot_loc(k))./Xp(k);
%线性插入
H_LS=interp1(pilot_loc,LS_est,[1:Nfft],'linear');
clear all;
close all;
clc;
fprintf('OFDM信道估计仿真\n\n');
num_sc = 1024; %载波数目 2048简化为1024
num_symbol = 14; %OFDM符号个数
data_symbol = [0,1,2,4,5,6,7,8,9,11,12,13];
pilot_symbol = [3,10];
cp_len = [160,144,144,144,144,144,144,160,144,144,144,144,144,144];
modulation_mode = 16;%调制方式
q = 20;
time_offset = 1; %us
time_offset = ceil(time_offset * 30.72); %20M 带宽h
SNR = 0:2:20;%信噪比取值
num_loop = 15;%循环次数
num_ant = 2;
num_bit_err=zeros(length(SNR),num_loop);
num_bit_err_dft=zeros(length(SNR),num_loop);
num_bit_err_ls=zeros(length(SNR),num_loop);
MSE=zeros(length(SNR),num_loop);
MSE1=zeros(length(SNR),num_loop);
MSE2=zeros(length(SNR),num_loop);
%Rx为发送信号产生
%产生发送的随机序列
BitsLen = num_sc * length(data_symbol);
BitsTx = randint(1,BitsLen);
%符号调制-
Modulated_Sequence=qammod(BitsTx,modulation_mode);
%产生导频序列
prime_num = primes(num_sc);
prime_num = max(prime_num);
m = 0 : num_sc-1;
xq = exp(-j*(pi*q*m.*(m+1)/prime_num));
pilot_symbols = xq( mod(m, prime_num) +1 );
pilot_symbols = pilot_symbols.';
%导频插入
piloted_ofdm_syms = zeros(num_sc, num_symbol);
piloted_ofdm_syms(:, data_symbol + 1) = reshape(Modulated_Sequence, num_sc, length(data_symbol));
piloted_ofdm_syms(:, pilot_symbol + 1) = repmat(pilot_symbols, 1, length(pilot_symbol));
%IFFT变换
time_signal = sqrt(num_sc) * ifft(piloted_ofdm_syms);
%加循环前缀
Tx_data_trans = [];
for k = 1 : num_symbol
add_cyclic_signal = [time_signal(num_sc - cp_len(k)+1 : num_sc, k); time_signal(:, k)].';
Tx_data_trans = [Tx_data_trans,add_cyclic_signal];
end
%主程序循环
for c1 = 1:length(SNR)
fprintf('\n\n\n仿真信噪比为%f\n\n',SNR(c1));
for num1 = 1:num_loop
%信道处理
%添加高斯白噪声
Tx_signal_power = sum(abs(Tx_data_trans(:)).^2) / length(Tx_data_trans(:));
noise_var = Tx_signal_power/(10^(SNR(c1)/10));
Rx_data(1, :) = awgn(Tx_data_trans, SNR(c1), 'measured');
Rx_data(2, :) = awgn(Tx_data_trans, SNR(c1), 'measured');
%添加 正时偏
Rx_data(1, :) = [zeros(1,time_offset),Rx_data(1, 1:length(Tx_data_trans)-time_offset),];
Rx_data(2, :) = [zeros(1,time_offset),Rx_data(2, 1:length(Tx_data_trans)-time_offset),];
%信号接收、去循环前缀、FFT变换
total_len=0;
for k=1 : num_symbol
Rx_signal_matrix(:, k) = Rx_data(1, total_len+cp_len(k)+1 : total_len+cp_len(k)+num_sc).';
total_len = total_len+cp_len(k)+num_sc;
end
Rx_carriers_ant0 = fft(Rx_signal_matrix)/sqrt(num_sc);
%Ant1
total_len=0;
for k=1 : num_symbol
Rx_signal_matrix(:, k) = Rx_data(2, total_len+cp_len(k)+1 : total_len+cp_len(k)+num_sc).';
total_len = total_len+cp_len(k)+num_sc;
end
Rx_carriers_ant1 = fft(Rx_signal_matrix)/sqrt(num_sc);
%导频和数据提取
Rx_pilot_ant0 = Rx_carriers_ant0(:, pilot_symbol+1);
Rx_data_ant0 = Rx_carriers_ant0(:, data_symbol+1);
Rx_pilot_ant1 = Rx_carriers_ant1(:, pilot_symbol+1);
Rx_data_ant1 = Rx_carriers_ant1(:, data_symbol+1);
%导频位置LS信道估计
pilot_patt = repmat(pilot_symbols, 1, length(pilot_symbol));
pilot_esti_ant0 = Rx_pilot_ant0.*conj(pilot_patt);
pilot_esti_ant1 = Rx_pilot_ant1.*conj(pilot_patt);
%LS估计的信道系数平铺
LS_channel_coeff_ant0 = [repmat(pilot_esti_ant0(:, 1), 1, length(data_symbol)/2), ...
repmat(pilot_esti_ant0(:, 2), 1, length(data_symbol)/2)];
LS_channel_coeff_ant1 = [repmat(pilot_esti_ant1(:, 1), 1, length(data_symbol)/2), ...
repmat(pilot_esti_ant1(:, 2), 1, length(data_symbol)/2)];
%导频位置DFT信道估计
Tx_pilot_estimate_ifft_ant0 = ifft(pilot_esti_ant0);
Tx_pilot_estimate_ifft_ant1 = ifft(pilot_esti_ant1);
stem(1:1200, Tx_pilot_estimate_ifft_ant0(:,1));
grid on;
h_len = cp_len(2);
Tx_pilot_estimate_ifft_ant0(h_len+1 : num_sc-h_len, :) = 0;
Tx_pilot_estimate_dft_ant0 = fft(Tx_pilot_estimate_ifft_ant0);
Tx_pilot_estimate_ifft_ant1(h_len+1 : num_sc-h_len, :) = 0;
Tx_pilot_estimate_dft_ant1 = fft(Tx_pilot_estimate_ifft_ant1);
%FT估计的信道系数平铺
DFT_channel_coeff_ant0 = [repmat(Tx_pilot_estimate_dft_ant0(:, 1), 1, length(data_symbol)/2),...
repmat(Tx_pilot_estimate_dft_ant0(:, 2), 1, length(data_symbol)/2)];
DFT_channel_coeff_ant1 = [repmat(Tx_pilot_estimate_dft_ant1(:, 1), 1, length(data_symbol)/2),...
repmat(Tx_pilot_estimate_dft_ant1(:, 2), 1, length(data_symbol)/2)];
%ZF OR MRC EQ
Tx_data_estimate_dft = (Rx_data_ant0.*conj(DFT_channel_coeff_ant0) + Rx_data_ant1.*conj(DFT_channel_coeff_ant1))...
./((abs(DFT_channel_coeff_ant0).^2) + (abs(DFT_channel_coeff_ant0).^2));
Tx_data_estimate_ls = (Rx_data_ant0.*conj(LS_channel_coeff_ant0) + Rx_data_ant1.*conj(LS_channel_coeff_ant1))...
./((abs(LS_channel_coeff_ant0).^2) + (abs(LS_channel_coeff_ant1).^2));
%DFT符号解调
demod_in_dft=Tx_data_estimate_dft(:).';
demod_out_dft=qamdemod(demod_in_dft,modulation_mode);
%LS符号解调 demod_in_ls=Tx_data_estimate_ls(:).';
demod_out_ls=qamdemod(demod_in_ls,modulation_mode);
%误码率的计算 for i=1:length(BitsTx)
if demod_out_dft(i)~=BitsTx(i)
num_bit_err_dft(c1,num1)=num_bit_err_dft(c1,num1)+1;
end
if demod_out_ls(i)~=BitsTx(i)
num_bit_err_ls(c1,num1)=num_bit_err_ls(c1,num1)+1;
end
end
end
end
BER_dft=mean(num_bit_err_dft.')/length(BitsTx);
BER_ls=mean(num_bit_err_ls.')/length(BitsTx);
主程序循环换结束
figure;
semilogy(SNR,BER_dft,'-mp',SNR,BER_ls,'-k+');
xlabel('SNR');
ylabel('BER');
legend('DFT信道估计','LS信道估计');
title('OFDM系统的LS和DFT信道估计');
grid on;
|
|