?? ofdm_mod.m
字號:
%%scr
close all;
clear all;
Nfft=128*2; %256 進(jìn)行256點(diǎn)的FFT
Ng=32*2; %64
Nzero=0;
Nzerosub=floor((Nfft*0.5)/2); %for padding
Ntotal=Ng+2*Nzero+Nfft; %320
Constellation_num=16;
Symbol_num=20; %able
N=Symbol_num*Nfft; %total
Ns=Symbol_num*Ntotal;
Pnum_least(1:Symbol_num)=10000;
Pnum(1:Symbol_num)=0;
Ndata_o=Symbol_num*(Nfft/2-Nfft/16/2);
sita=0;
sita1=1;
sita2=0;
sita3=0; % 測試變量
sign=1;
%for filter
LHBF1=23; % 半帶濾波器長度
% I1=zeros(1,Ndata_o);
% I2=zeros(1,Ndata_o);
% I3=zeros(1,Ndata_o);
% I4=zeros(1,Ndata_o);
CHBF1(1:LHBF1)=0;
InsertGIout(1:Symbol_num,1:Ntotal)=0;
% 半帶濾波器系數(shù)
fid2=fopen('HBF23.txt','r');
CHBF1=fscanf(fid2,'%f');
fclose(fid2);
IHBF1in(1:LHBF1)=0;
QHBF1in(1:LHBF1)=0; % 進(jìn)行半帶濾波時的緩沖區(qū)
IHBF1out(1:2*Ns)=0;
QHBF1out(1:2:Ns)=0; % 兩倍上采樣后時域數(shù)據(jù)的存放數(shù)組
% I、Q分別表示I、Q兩路信號
ii=1;
k=1;
k1=1;
k2=1;
k3=1;
k4=1;
k5=1;
t=1;
t2=1;
t3=1;
SoutI(1:Ns)=0;
SoutQ(1:Ns)=0; % 沒有進(jìn)行兩倍上采樣之前的時域數(shù)據(jù)存放數(shù)組
% I、Q分別表示I、Q兩路信號
IFFToutI(1:Symbol_num,1:Nfft)=0;
IFFToutQ(1:Symbol_num,1:Nfft)=0;
IFFToutIQ(1:Symbol_num,1:Nfft)=0;
IFFToutIQS(1:Symbol_num,1:Nfft)=0; % 進(jìn)行IFFT運(yùn)算后OFDM符號存放數(shù)組
% I、Q分別表示I、Q兩路信號
% IQ表示復(fù)數(shù)信息
FFTout_t_1(1:Symbol_num,1:2*Nfft)=0;
FFTout_t_2(1:Symbol_num,1:4*Nfft)=0;
FFTout_t_3(1:Symbol_num,1:8*Nfft)=0; % 測試變量
ii=1;
M_pilot=8;
subcarrier(1:N)=0;
subcarriercom(1:N)=0;
%train_word
% 獲得短前導(dǎo)字頻域信息
fid3=fopen('data_I_train_word_re_i0_256.txt','r');
I_train_word_re_temp=fscanf(fid3,'%f');
fclose(fid3);
I_train_word_re = (I_train_word_re_temp)';
fid3=fopen('data_Q_train_word_re_i0_256.txt','r');
Q_train_word_re_temp=fscanf(fid3,'%f');
fclose(fid3);
Q_train_word_re = (Q_train_word_re_temp)';
% 獲得一個長前導(dǎo)字頻域信息
fid3=fopen('data_I_train_word_1_i0_256.txt','r');
I_train_word_1_temp=fscanf(fid3,'%f');
fclose(fid3);
I_train_word_1 = (I_train_word_1_temp)';
fid3=fopen('data_Q_train_word_1_i0_256.txt','r');
Q_train_word_1_temp=fscanf(fid3,'%f');
fclose(fid3);
Q_train_word_1 = (Q_train_word_1_temp)';
% 獲得二個長前導(dǎo)字頻域信息
fid3=fopen('data_I_train_word_2_i0_256.txt','r');
I_train_word_2_temp=fscanf(fid3,'%f');
fclose(fid3);
I_train_word_2 = (I_train_word_2_temp)';
fid3=fopen('data_Q_train_word_2_i0_256.txt','r');
Q_train_word_2_temp=fscanf(fid3,'%f');
fclose(fid3);
Q_train_word_2 = (Q_train_word_2_temp)';
l=1;
%%%%%%%%%%%%%%^^^^^^^^^^^^^^^^^^^
%window sequence
% 加窗處理的參數(shù)設(shè)置
wind=zeros(1,(Nfft+Ng));
windo=hamming(Nfft+Ng);
wind((Ng+1):Nfft)=windo((Ng+1):Nfft);
wind(1:Ng)=windo(Ng+1);
wind((Nfft+1):(Nfft+Ng))=windo(Nfft);
%%%%%%%%%%%%%%%%^^^^^^^^^^^^^^^^^^^
stst=1;
%%%%%%%%%%%%%%%$$$$$$$$$$$$$$$$$
% 獲得隨機(jī)數(shù),當(dāng)作用戶信息,調(diào)制到各個子載波上
fid3=fopen('I1.txt','r');
I1=fscanf(fid3,'%d,');
fclose(fid3);
fid3=fopen('I2.txt','r');
I2=fscanf(fid3,'%d,');
fclose(fid3);
fid3=fopen('I3.txt','r');
I3=fscanf(fid3,'%d,');
fclose(fid3);
fid3=fopen('I4.txt','r');
I4=fscanf(fid3,'%d,');
fclose(fid3);
% Bits Mapping to constellations
for i=1:N
% 前面4個符號是前導(dǎo)字
if(i < Nfft*4+1)
% 短前導(dǎo)字
if( i>0 & i < 2*Nfft+1)
if(rem(i,Nfft) == 0)
Imapped(i) = I_train_word_re(Nfft);
Qmapped(i) = Q_train_word_re(Nfft);
else
Imapped(i) = I_train_word_re(rem(i,Nfft));
Qmapped(i) = Q_train_word_re(rem(i,Nfft));
end
end
% 第一個長前導(dǎo)字
if( i>2*Nfft & i < 3*Nfft+1)
if(rem(i,Nfft) == 0)
Imapped(i) = I_train_word_1(Nfft);
Qmapped(i) = Q_train_word_1(Nfft);
else
Imapped(i) = I_train_word_1(rem(i,Nfft));
Qmapped(i) = Q_train_word_1(rem(i,Nfft));
end
end
% 第二個長前導(dǎo)字
if (i>3*Nfft & i<Nfft*4+1)
if(rem(i,Nfft) == 0)
Imapped(i) = I_train_word_2(Nfft);
Qmapped(i) = Q_train_word_2(Nfft);
else
Imapped(i) = I_train_word_2(rem(i,Nfft));
Qmapped(i) = Q_train_word_2(rem(i,Nfft));
end
end
else
% 前導(dǎo)字發(fā)送完后,發(fā)送用戶信息,這里用隨機(jī)數(shù)代替用戶信息
% 中間查0的虛擬子載波沒有用戶信息
if ((rem(i,Nfft) < (Nfft/2-Nzerosub+1)) | (rem(i,Nfft) > (Nfft/2+Nzerosub)))
if(rem(i,Nfft/(2*M_pilot))==0) % insert pilots
if (sign==1)
Isignal=3*1.414;
Qsignal=0;
else
Isignal=-3*1.414;
Qsignal=0;
end %% sub-carrier polit scheme is BPSK
stst=stst+1;
else
% 插入導(dǎo)頻信息
Isignal = 2*(I1(l)*1+I2(l)*2)-3;
Qsignal = 2*(I3(l)*1+I4(l)*2)-3;
l=l+1;
end
Imapped(i)=Isignal;
Qmapped(i)=Qsignal;
else
% 中間查0的虛擬子載波沒有用戶信息
Imapped(i)=0;
Qmapped(i)=0;
end
end
if (rem(i,Nfft)==0)
%%%%%%%%%%%
now_step = [num2str(ii)]
sign=rem(sign+1,2);
Imappedfs(1:Nfft)=Imapped(i-Nfft+1:i);
Qmappedfs(1:Nfft)=Qmapped(i-Nfft+1:i);
% 對頻域信息進(jìn)行IFFT變換,獲得時域信息
IFFToutI(ii,1:Nfft)=ifft(Imappedfs,Nfft);
IFFToutQ(ii,1:Nfft)=ifft(Qmappedfs,Nfft);
%IFFToutIQ(ii,1:Nfft)=IFFToutI+j*IFFToutQ;
IFFToutIQS(ii,1:Nfft)=ifft(Imappedfs(1:Nfft)+j*Qmappedfs(1:Nfft),Nfft);
IFFToutIQ(ii,1:Nfft)=IFFToutIQS(ii,1:Nfft);
ii=ii+1;
end;
end; % end of fft symbol generation
% Insert Guard Interval, refix & postfix zero. Pallel processing . serial output at 1/Ts rate
for (ig=1:Symbol_num)
% 插入循環(huán)前綴
InsertGIout(ig,1:Nzero)=0;
InsertGIout(ig,Ng+Nzero+1:(Ng+Nfft+Nzero))=IFFToutIQ(ig,1:Nfft);
InsertGIout(ig,Nzero+1:(Ng+Nzero))=IFFToutIQ(ig,(Nfft-Ng)+1:Nfft);
InsertGIout(ig,(Ng+Nfft+Nzero)+1:Ntotal)=0;
%%%%%%%%%%%%%%%^^^^^^^^^^^^^^^^wind
% 加窗處理
if(ig > 4)
InsertGIouta(ig,1:Ntotal)=InsertGIout(ig,1:Ntotal);
InsertGIout(ig,1:Ntotal)=InsertGIouta(ig,1:Ntotal).*wind;
end
%%%%%%%%%%%%%%^^^^^^^^^^^^^^^^^
FFTout_t(ig,1:Nfft)=fft(InsertGIout(ig,Ng+1-sita:Nfft+Ng-sita),Nfft); % 進(jìn)行中間變量的測試
% 將生成的OFDM時域信號依次寫入一個數(shù)組里,形成一個連續(xù)的OFDM時域數(shù)據(jù)流
for (is=1:Ntotal)
SoutI((ig-1)*Ntotal+is)=real(InsertGIout(ig,is));
SoutQ((ig-1)*Ntotal+is)=imag(InsertGIout(ig,is));
end;
end;
% 對生成的OFDM時域信號進(jìn)行兩倍上采樣
% sample rate interpolation and modulation
for(k=1:Ns)
% HBF1
for j1=1:2
IHBF1in(2:LHBF1)=IHBF1in(1:LHBF1-1);
QHBF1in(2:LHBF1)=QHBF1in(1:LHBF1-1);
if (j1==1)
IHBF1in(1)=SoutI(k);
QHBF1in(1)=SoutQ(k);
else
IHBF1in(1)=0;
QHBF1in(1)=0;
end;
IHBF1out(k1)=2*IHBF1in*CHBF1;
QHBF1out(k1)=2*QHBF1in*CHBF1;
if (rem(k1,2*Ntotal)==0)
FFTout_t_1(t,1:2*Nfft)=fft(IHBF1out(k1-2*Nfft+1-sita1:k1-sita1),2*Nfft)+j*fft(QHBF1out(k1-2*Nfft+1-sita1:k1-sita1),2*Nfft);
t=t+1;
end;
k1=k1+1;
end;% end of j1
end; %end of k
% MODULATION
% 將基帶信號轉(zhuǎn)換成中頻信號
M=2;
NN=length(IHBF1out);
phase0=pi/30;
error_f=0;
w=pi/M; % IF=8*(symbol rate) or (sample rate)/4
IMOD(1:NN)=0;
QMOD(1:NN)=0;
Modout(1:NN)=0;
for i=1:NN
IMOD(i)=cos(rem(i*w+phase0,2*pi))*IHBF1out(i);
QMOD(i)=-sin(rem(i*w+phase0,2*pi))*QHBF1out(i);
Modout(i)=IMOD(i)+QMOD(i);
end;
zB=20*log10(abs(fft(SoutI+j*SoutQ)));
zF=20*log10(abs(fft(Modout)));
% 記錄基帶信號的頻譜情況
fid=fopen('SoutI_4_BF.txt','w');
fprintf(fid,'%d,',zB);
fclose(fid);
% 記錄中頻信號的頻譜情況
fid=fopen('SoutI_4_IF.txt','w');
fprintf(fid,'%d,',zF);
fclose(fid);
% 觀測時域信號
figure(1);
plot(SoutI(1:length(SoutI)));
figure(2);
plot(SoutQ(1:length(SoutQ)));
% 觀察頻域情況
figure(3);
plot(real(FFTout_t(1,1:Nfft)));
figure(4);
plot(20*log10(abs(fft(SoutI+j*SoutQ))));
figure(5);
plot(20*log10(abs(fft(IHBF1out+j*QHBF1out))));
figure(6);
plot(20*log10(abs(fft(Modout))));
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -