.
% 获得随机数,当作用户信息,调制到各个子载波上 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个符号是前导字 if(i < Nfft*4+1) % 短前导字
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
% 第一个长前导字
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
% 第二个长前导字
if (i>3*Nfft & i Imapped(i) = I_train_word_2(Nfft); Qmapped(i) = Q_train_word_2(Nfft); else Imapped(i) = I_train_word_2(rem(i,Nfft)); Word 文档 . Qmapped(i) = Q_train_word_2(rem(i,Nfft)); end end else % 前导字发送完后,发送用户信息,这里用随机数代替用户信息 % 中间查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 % 插入导频信息 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); % 对频域信息进行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; Word 文档 . end; % end of fft symbol generation for (ig=1:Symbol_num) % 插入循环前缀 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; % 加窗处理 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); % 进行中间变量的测试 % 将生成的OFDM时域信号依次写入一个数组里,形成一个连续的OFDM时域数据流 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时域信号进行两倍上采样 % 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 Word 文档 . % MODULATION % 将基带信号转换成中频信号 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(Sooty*SoutQ)))); figure(5); plot(20*log10(abs(fft(IHBF1out+j*QHBF1out)))); figure(6); plot(20*log10(abs(fft(Modout)))); 五、OFDM的仿真结果 1、输入时域I路数据信号 Word 文档 . 2、输入时域Q路数据信号 3、I路、Q路数据合并后的频域波形 Word 文档