Final assignment
leolei
In this final assignment, we respectively attempt to build a complete ofdm system and the
following is a combine summary of our each’s work.
part one
-->code:
-->main.m
clc
clear
close all
%% 基础信息
m = 2;%调制等级(QAM)
N = 100;%发射 OFDM 块个数
num_bit = N*m*48/2;%输入二进制比特个数
pilot_out = zeros(N,52)';%插入导频输出
map_out = zeros(N,64)';%mapping 输出
%insert-pilot&mapping 索引
%###############
DataSubcPatt = [1:5 7:19 21:26 27:32 34:46 48:52];
PilotSubcPatt = [6 20 33 47];
UsedSubcIdx = [7:32 34:59];
Reorder = [33:64 1:32];
%###############
%% 测试
N_tn2 = 250;
snr = 12:0.1:16;
for tn1=1:length(snr)
counter = 0;
for tn2=1:N_tn2
%% %%%%%%%%%%%%################发射端###########################
%%%%%%%信道编码
in_bit = randi([0,1],1,num_bit);%输入随机二进制流
trellis = poly2trellis(7,[133,171]);%生成卷积编码码树
conv_encode_bit = convenc(in_bit,trellis);%1/2 卷积信道编码
%%%%%%%交织
interleaved_bits = tx_interleaver(conv_encode_bit,48, m);
%%%%%%%m 阶 QAM 调制
qam_in = reshape(interleaved_bits,num_bit*2/m,m);%串并转换
qam_out = qammod(bi2de(qam_in),2.^m,'UnitAveragePower',true);%qam 调制
%%%%%%%mapping
%插入导频
pilot_in = reshape(qam_out,48,N);%串并转换
pilot_out(DataSubcPatt,:) = pilot_in;
pilot_out(PilotSubcPatt,:) = 1;%映射
%映射至子载波
map_out(UsedSubcIdx,:) = pilot_out;%映射
map_out(Reorder,:) = map_out;%重新排序
%%%%%%%IFFT(ofdm-mod)
ofdm = ifft(map_out,64,1);
%%%%%%%add_cp
s_cp = [ofdm(64-15:64,:);ofdm];
s_cp = reshape(s_cp,N*80,1);
%%%%%%%training
[trained_sent,longtrain] = trainning(s_cp);
%% %%%%%%%%%%%%################信道##############################
SNR = snr(tn1);
h = [1,0.1,0.2];
%%%%%%%加入空白前缀
zero_time = 200;
add_zeroinfo = [zeros(1,zero_time)';trained_sent;zeros(1,1000)'];
%%%%%%%加入频偏
period = length(add_zeroinfo);
axis_time = 1:period;
bias = 0.01;
fre_bias = exp(-1i*2*pi/64*axis_time*bias)';
add_fre_bias = add_zeroinfo.*fre_bias;
%%%%%%%%过多径信道
after_h = filter(h,1,add_fre_bias);
%%%%%%%%加噪声
after_noise = awgn(after_h,SNR,'measured');
%% %%%%%%%%%%%%################接收端############################
%%%%%%%包检测 ok
pkt_detect_index = pkt_detect(after_noise);
%%%%%%%频率同步 ok
freq_synq_out = freq_synq(after_noise,pkt_detect_index);
bias_detect = 2*freq_synq_out/pi;
bias_detect_perf(tn2) = bias-bias_detect;
freq_compensation = exp(-1i*2*pi/64*axis_time*bias_detect)';
after_freq_compen = after_noise.*freq_compensation;
%after_freq_compen = after_noise;
err_fre_synq = sum((add_zeroinfo - after_freq_compen).^2)/period;
%%%%%%%%精准时间同步 ok
start_time = fine_time_synq(after_freq_compen,trained_sent(257:320));
r_cp = after_freq_compen(start_time+128:start_time+128+80*N-1);
%%%%%%%remove_cp
r_cp = reshape(r_cp,80,N);
r = r_cp(17:end,:);
%%%%%%%FFT(ofdm-demod)
iofdm = fft(r,64,1);
%%%%%%%demapping
demap_in(Reorder,:) = iofdm;%排序恢复
demap_out = demap_in(UsedSubcIdx,:);
%%%%%%%信道估计 ok
[HK_esti,err_esti] = channel_esti(h,after_freq_compen(start_time:start_time+127),longtrain);
channel_esti_perf(tn2) = err_esti;
%%%信道均衡
for n=1:length(demap_out)
after_equa(:,n) = demap_out(:,n)./HK_esti;
end
%%%%%%%%
depilot_out = after_equa(DataSubcPatt,:);
pilot_syms_out = after_equa(PilotSubcPatt,:);
%%%%%%%%相位补偿
phase_esti = angle(sum(pilot_syms_out));
phase_comp = exp(-j*phase_esti);
after_phase_comp = depilot_out.*repmat(phase_comp,48,1);
%%%%%%%m 阶 QAM 解调
deqam_in = reshape(after_phase_comp,num_bit*2/m,1);
deqam_bit = de2bi(qamdemod(deqam_in,2.^m,'UnitAveragePower', true));
deqam_out = reshape(deqam_bit,1,num_bit*2);
%%%%%%%解交织
deint_bits = rx_deinterleave(deqam_out, 48, m);
%%%%%%%信道译码
conv_decode_bit = vitdec(deint_bits,trellis,35,'trunc','hard');
%%%%%%%输出检测误码率和丢包率
out_bit = conv_decode_bit;
BER_tn2(tn2) = sum(abs(in_bit-out_bit))/num_bit;
if BER_tn2(tn2)~=0
counter = counter+1;
end
BER_tn2(tn2) = BER_tn2(tn2) + 1e-20;
end
BER_tn1(tn1) = sum(BER_tn2)/N_tn2;
PER(tn1) = counter/N_tn2;
MSE_bias(tn1) = sum(bias_detect_perf.^2)/N_tn2;
MSE_channel_esti(tn1) = sum(channel_esti_perf.^2)/N_tn2;
disp(tn1);
end
figure;
subplot(211);
plot(snr,PER);
title('PER');
xlabel('snr');
ylabel('PER');
subplot(212);
semilogy(snr,BER_tn1,'rd--');
title('BER');
xlabel('snr');
ylabel('BER');
grid on;
figure;
plot(snr,MSE_bias);
title('CFO');
xlabel('snr');
ylabel('CFO-detect-MSE');
figure;
plot(snr,MSE_channel_esti);
xlabel('snr');
ylabel('channel-esti-MSE');
title('channel-esti');
used functions:
-->trainning.m
function [trained_bit,longtrain] = trainning(in_bits)
map_s = zeros(1,64);
map_l = zeros(1,64);
trained_l = zeros(1,160);
UsedSubcIdx = [7:32 34:59];
Reorder = [33:64 1:32];
shorttrain_bit = [0 0 1+1i 0 0 0 -1-1i 0 0 0 1+1i 0 0 0 -1-1i 0 0 0 -1-1i 0 0 0 1+1i 0 0 0 0 0 0
-1-1i 0 0 0 -1-1i 0 0 0 1+1i 0 0 0 1+1i 0 0 0 1+1i 0 0 0 1+1i 0 0];
shorttrain_bit = shorttrain_bit/sqrt(13/6);
longtrain_bit = [1 1 -1 -1 1 1 -1 1 -1 1 1 1 1 1 1 -1 -1 1 1 -1 1 -1 1 1 1 1 1 -1 -1 1 1 -1 1 -1
1 -1 -1 -1 -1 -1 1 1 -1 -1 1 -1 1 -1 1 1 1 1];
map_s(UsedSubcIdx) = shorttrain_bit;
map_s(Reorder) = map_s;
map_l(UsedSubcIdx) = longtrain_bit;
map_l(Reorder) = map_l;
sent_s = ifft(map_s,64);
sent_s = sent_s(1:16);
trained_s = sent_s;
sent_l = ifft(map_l,64);
for i=1:9
trained_s = [trained_s sent_s];
end
trained_l(1:32) = sent_l(33:64);
trained_l(33:96) = sent_l;
trained_l(97:160) = sent_l;
x = reshape([trained_s trained_l],320,1);
trained_bit = [x;in_bits];
longtrain = longtrain_bit;
-->pkt_detect.m
function pkt_detect_index = pkt_detect(in_bits)
LA = 32;
count = 1;
standard = 0.85;
detect_time = 1000;
for n=1:detect_time
C(n) = abs(sum(in_bits(n:(n+LA-1)).*conj(in_bits((n+16):(n+15+LA)))));
P(n) = sum(abs(in_bits((n+16):(n+LA+15))).^2);
if ((C(n)/P(n))>standard)&(count==1)
count = n;
end
end
axis = 1:detect_time;
m = C./P;
% plot(axis,m);
% title(sprintf('pkt-start-time: %d',count));
pkt_detect_index = count;
-->freq_synq.m
function freq_synq_out = freq_synq(in_bits,pkt_detect_index)
offset = 20;
synq_bit = in_bits(pkt_detect_index:end);
R = sum(synq_bit(offset:(16*2+offset)).*conj(synq_bit((offset+16):(3*16+offset))));
angle_R = angle(R);
freq_synq_out = angle_R;
-->fine_time_synq.m
function fine_time_index = fine_time_synq(in_bits,train_sequence)
match_filter =conj(train_sequence);
detect_time = 1000;
for n=1:detect_time
time_synq_out(n) = sum(in_bits(n:n+63).*match_filter);
end
axis = 1:detect_time;
[B,I] = maxk(time_synq_out,2);
% plot(axis,time_synq_out);
% title(sprintf('start-time: %d,%d',I));
fine_time_index = min(I);
-->channel_esti.m
function [HK_esti,err] = channel_esti(hn,Yn,longtrain)
Reorder = [33:64 1:32];
UsedSubcIdx = [7:32 34:59];
HK(Reorder) = fft(hn,64);
HK = HK(:,UsedSubcIdx);
Yn = reshape(Yn,64,2);
iofdm = fft(Yn,64,1);
reorder(Reorder,:) = iofdm;
extract = reorder(UsedSubcIdx,:);
fre_tr_syms = mean(extract,2);
HK_esti = reshape(longtrain,52,1).*fre_tr_syms;
axis = 1:52;
% plot(axis,HK,'b');hold on
% plot(axis,real(0.99*HK_esti),'r');
% legend('HK','HK-esti');
err = sum(abs(HK-HK_esti').^2)/52;
end
output of simulation:
1.package detect
snr = 20dB (with 200 zero symbols ahead)
2.frequency synchronization
the bias between the cfo detected and theory
3.fine time synchronization
snr = 20dB (with 200 zero symbols ahead)
4.channel estimation
single estimation