一、简介
二、源代码
clear all
close all
%% 参数设定
N_FFT=256;%FFT点数
fs=4*10^6;%基带采样频率4MHz
fsub=15.625*10^3;%子载波频率间隔15.625KHz
%% 训练符号
%短训练符号频域表示
F_Shortpreamble(1:256)=[0,0,0,0,-1-1j,0,0,0,1-1j,0,0,0,1+1j,0,0,0,-1-1j,0,0,0,-1+1j,0,0,0,1-1j,0,0,0,1+1j,0,0,0, -1+1j,0,0,0, 1-1j,0,0,0, -1-1j,0,0,0, 1+1j,0,0,0, -1+1j,0,0,0, -1-1j,0,0,0, 1+1j,0,0,0, 1-1j,0,0,0,-1-1j,0,0,0, 1-1j,0,0,0, 1+1j,0,0,0, -1-1j,0,0,0, -1+1j,0,0,0, -1+1j,0,0,0, -1-1j,0,0,0, 1-1j,0,0,0, -1+1j,0,0,0,1+1j,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,1+1j,0,0,0,1+1j,0,0,0,1+1j,0,0,0,1-1j,0,0,0,-1+1j,0,0,0,1+1j,0,0,0,1+1j,0,0,0,1+1j,0,0,0,1-1j,0,0,0,-1+1j,0,0,0,1+1j,0,0,0,1+1j,0,0,0,1+1j,0,0,0,1-1j,0,0,0,-1+1j,0,0,0,1-1j,0,0,0,1-1j,0,0,0,1-1j,0,0,0,-1-1j,0,0,0,1+1j,0,0,0,-1+1j,0,0,0,-1+1j,0,0,0,-1+1j,0,0,0,1+1j,0,0,0,-1-1j,0,0,0];
%短训练符号时域表示
T_Shortpreamble(1:256)=ifft(F_Shortpreamble);
%长训练符号频域表示
F_Longpreamble(1:256)=[ 0, -1-j, 1+j, -1+j, -1+j, -1-j, 1+j, 1+j, 1+j, -1-j, 1+j, 1-j, 1-j, 1-j, -1+j,-1+j,-1+j, -1+j, 1-j, -1-j, -1-j, -1+j, 1-j, 1+j, 1+j, -1+j, 1-j, 1-j, 1-j, -1+j, 1-j, -1-j, -1-j, -1-j, 1+j,1+j, 1+j,1+j, 1-j, -1+j, -1+j, 1+j, -1-j, 1-j, 1-j, 1+j, -1-j, -1-j, -1-j, 1+j, -1-j, -1+j, -1+j, -1+j, 1-j, 1-j, 1-j, 1-j, -1+j,1+j, 1+j, -1-j, 1+j, -1+j, -1+j, -1-j, 1+j, 1+j, 1+j, -1-j, 1+j, 1-j, 1-j, 1-j, -1+j, -1+j, -1+j, -1+j, 1-j, -1-j,-1-j, 1-j, -1+j, -1-j, -1-j, 1-j, -1+j, -1+j, -1+j, 1-j, -1+j,1+j, 1+j, 1+j, -1-j, -1-j, -1-j, -1-j, 1+j, 1-j, 1-j, 0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1-j,1-j, -1-j, 1+j, 1-j, 1-j, -1+j, 1-j, 1-j, 1-j, 1+j, -1-j, 1+j, 1+j, -1-j, 1+j, -1-j, -1-j, 1-j, -1+j, 1-j, 1-j, -1-j, 1+j,1-j, 1-j, -1+j, 1-j, 1-j, 1-j, 1+j, -1-j, 1+j, 1+j, -1-j, 1+j, -1-j, -1-j, 1-j, -1+j, 1-j, 1-j, -1-j, 1+j, 1-j, 1-j, -1+j,1-j, 1-j, 1-j, 1+j, -1-j, 1+j, 1+j, -1-j, 1+j, -1-j, -1-j, 1-j, -1+j, 1+j, 1+j, 1-j, -1+j, 1+j, 1+j, -1-j, 1+j, 1+j,1+j, -1+j, 1-j, -1+j, -1+j, 1-j, -1+j, 1-j, 1-j,1+j, -1-j, -1-j, -1-j, -1+j, 1-j, -1-j, -1-j, 1+j, -1-j, -1-j, -1-j, 1-j,-1+j, 1-j, 1-j, -1+j, 1-j, -1+j,-1+j, -1-j, 1+j];
%长训练符号频域表示
T_Longpreamble(1:256)=ifft(F_Longpreamble);
%% 卷积编码
%[133 171]卷积码其实是卷积码(2,1,7)的最佳编码形式
signal = randi([0 1],1,1920);%编码器输入48比特
trellis=poly2trellis(7,[133 171]);%内部参数前者是卷积码的约束长度N,后者是根据输入输出连线情况的一个m*n矩阵,m为输入信号的个数,n为输出信号的个数。
con_code=convenc(signal,trellis);
figure
subplot(2,1,1)
stairs(signal(1:50));
ylabel('幅度');
title('卷积编码前的数据');
subplot(2,1,2)
stairs(con_code(1:100));
title('卷积编码后的数据');
ylabel('幅度');
%% QPSK映射
bits_per_symbol=2;%每符号含比特数,QPSK调制
QPSK_code_bin=reshape(con_code,1920,bits_per_symbol);%1920是数据长度,进行矩阵变换,将1*3840的矩阵变换为1920*2的矩阵,前1920为第一列,后1920位第二列
QPSK_code_dec=2*QPSK_code_bin(:,1)+QPSK_code_bin(:,2);
txSig = pskmod(QPSK_code_dec,4,pi/4);
scatterplot(txSig)%星座图
%% 反傅里叶变换
temp=zeros(1,256);
temp([13 38 63 88 168 193 218 243])=1;%内插1
for m=1:10
temp_data(m,2:12)=txSig((m-1)*192+1:(m-1)*192+11);
temp_data(m,14:37)=txSig((m-1)*192+12:(m-1)*192+35);
temp_data(m,39:62)=txSig((m-1)*192+36:(m-1)*192+59);
temp_data(m,64:87)=txSig((m-1)*192+60:(m-1)*192+83);
temp_data(m,89:100)=txSig((m-1)*192+84:(m-1)*192+95);
temp_data(m,156:167)=txSig((m-1)*192+96:(m-1)*192+107);
temp_data(m,169:192)=txSig((m-1)*192+108:(m-1)*192+131);
temp_data(m,194:217)=txSig((m-1)*192+132:(m-1)*192+155);
temp_data(m,219:242)=txSig((m-1)*192+156:(m-1)*192+179);
temp_data(m,244:256)=txSig((m-1)*192+180:(m-1)*192+192);
temp_data(m,:)=temp+temp_data(m,:);
f_data(m,:)=ifft(temp_data(m,:))*16;
figure
plot(0:1/(255*4*10^6):1/(4*10^6),abs(f_data(m,:))');
title('时域波形');
xlabel('t/s');
ylabel('幅度');
end
%% 加CP
data_cp=[f_data(:,193:end),f_data(:,:)];%数据加CP
Shortpreamble_cp=[T_Shortpreamble(:,193:end),T_Shortpreamble(:,:)];%短训练集加CP
Longpreamble_cp=[T_Longpreamble(:,193:end),T_Longpreamble(:,:)];%长训练集加CP
%% 并串变换
OFDM_frame=[Shortpreamble_cp.*32,Longpreamble_cp,reshape(data_cp.',[],1).'];
%% 上变频
fc=4*4000000; % 取载波fc为基带带宽的4倍
%设采样频率为载波频率的四倍,则每个基带时隙内要输出16*320个符号;
t=0:80*10^(-6)/(320*16):80*10^(-6)/320*length(OFDM_frame)-80*10^(-6)/(320*16);
carrier=exp(j*(2*pi*(fc+25)*t));
sam=kron(OFDM_frame,ones(1,16));
frame_up=real(sam.*carrier);%上变频
figure
subplot(2,1,1)
plot(t,abs(frame_up));
xlabel('t/s');
ylabel('幅度');
title('上变频后的时域波形');
subplot(2,1,2)
plot(abs(OFDM_frame));
title('OFDM帧时域波形');
xlabel('t/s');
ylabel('幅度');
for SNR=2:100
% SNR=6;
%% 信道
rxSig = awgn(frame_up,SNR/2);%高斯白噪声信道
%rxSig=frame_up;
% figure
% plot(rxSig,'r');
% hold on
% plot(frame_up,'b')
%% 下变频
carrier_cos=cos(2*pi*fc*t);
carrier_sin=-sin(2*pi*fc*t);%两路解调信号
carrier_cos_reshape=reshape(carrier_cos,16,length(carrier_cos)/16);
carrier_sin_reshape=reshape(carrier_sin,16,length(carrier_sin)/16);
frame_up_reshape=reshape(rxSig,16,length(rxSig)/16);
rece_cos=frame_up_reshape'*carrier_cos_reshape/16*2;
rece_sin=frame_up_reshape'*carrier_sin_reshape/16*2;
for ii=1:length(OFDM_frame)
rece_real(ii)=rece_cos(ii,ii);
rece_im(ii)=rece_sin(ii,ii);
end
rece_down=rece_real+1i*rece_im; %%下变频结束,得到基带复数信号
%% AGC自动增益控制
y=agc(rece_down,20,length(rece_down));
% figure
% subplot(2,1,1)
% plot(abs(rece_down));
% ylabel('幅度');
% title('下变频后的信号');
% subplot(2,1,2)
% plot(abs(y));
% title('自动增益控制后的信号');
% ylabel('幅度');
%% 帧检测
FrameStart=frame_detection(y,340);
frame=[y(FrameStart+1:end),zeros(1,256)];
%% 去CP与FFT去导频
for i=1:10
data_rx(i,:)=frame(65+i*320:(i+1)*320);
% data_rx(i,:)=y(65+(i+1)*320:(i+2)*320);
data_r(i,:)=fft(data_rx(i,:))/sqrt(N_FFT);
data(i,1:11)=data_r(i,2:12);
data(i,12:35)=data_r(i,14:37);
data(i,36:59)= data_r(i,39:62);
data(i,60:83)=data_r(i,64:87);
data(i,84:95)=data_r(i,89:100);
data(i,96:107)=data_r(i,156:167);
data(i,108:131)=data_r(i,169:192);
data(i,132:155)=data_r(i,194:217);
data(i,156:179)=data_r(i,219:242);
data(i,180:192)=data_r(i,244:256);
end
三、运行结果
四、备注
2014a