OFDM matlab模擬不同保護間隔的ISI影響
OFDM matlab模擬不同保護間隔的ISI影響
主程式:
NgType=1; % 對於CP或ZP,NgType=1或2
if NgType1
nt=‘CP’;
elseif NgType2
nt=‘ZP’;
end
Ch=0; % 對於AWGN/多徑瑞利通道,channelCh=0/1
if Ch==0
chType=‘AWGN’;
Target_neb=100;
else
chType=‘CH’;
Target_neb=500;
end
PowerdB=[0 -8 -17 -21 -25]; % 通道抽頭功率特性(dB)
Delay=[0 3 5 6 8]; % 通道時延(取樣點)
Power=10.^(PowerdB/10); % 通道抽頭功率特性(線性尺度)
Ntap=length(PowerdB); % 通道抽頭數
Lch=Delay(end)+1; % 通道長度
Nbps=4; M=2^Nbps; % 調製階數=2/4/6:QPSK/16-QAM/64-QAM
Nfft=64; % FFT大小
Ng=Nfft/4; % GI(保護間隔)長度,沒保護間隔時,Ng=0
Nsym=Nfft+Ng; % 符號週期
Nvc=Nfft/4; % Nvc=0 :沒有VC(虛擬載波)
Nused=Nfft-Nvc;
EbN0=(0:3:30); % Eb/N0
N_iter=1e3; % 對於每一個EbN0的迭代次數
Nframe=3; % 每一幀的符號數
sigPow=0; % 初始訊號功率
file_name=[‘OFDM_BER_’ chType ‘’ nt '’ ‘GL’ num2str(Ng) ‘.dat’];
fid=fopen(file_name, ‘w+’);
norms=[1 sqrt(2) 0 sqrt(10) 0 sqrt(42)]; % BPSK 4-QAM 16-QAM
for i=0:length(EbN0)
randn(‘state’,0);rand(‘state’,0);
%Ber2=ber(); 初始化BER
Neb=0; % 初始化錯誤位元數
Ntb=0; % 初始化總位元數
for m=1:N_iter
% Tx_______________________________________________________________________________
X= randi([0 M-1],1,NusedNframe); % bit: 整數向量(問題:randint怎麼換成randi)
Xmod= qammod(X,M,‘gray’)/norms(Nbps);
if NgType~=2
x_GI=zeros(1,Nframe
elseif NgType2
x_GI= zeros(1,Nframe*Nsym+Ng);
% 用Ng個零擴充套件OFDM符號
end
kk1=(1:Nused/2);
kk2=(Nused/2+1:Nused);
kk3=1:Nfft;
kk4=1:Nsym;
for k=1:Nframe
if Nvc~=0
X_shift= [0 Xmod(kk2) zeros(1,Nvc-1) Xmod(kk1)];
else
X_shift= [Xmod(kk2) Xmod(kk1)];
end
x= ifft(X_shift);
x_GI(kk4)= guard_interval(Ng,Nfft,NgType,x);
kk1=kk1+Nused;
kk2= kk2+Nused;
kk3=kk3+Nfft;
kk4=kk4+Nsym;
end
if Ch
y= x_GI; % 沒有通道
else % 多徑衰落通道
channel=(randn(1,Ntap)+1irandn(1,Ntap)).sqrt(Power/2);
h=zeros(1,Lch); %通道脈衝響應
h(Delay+1)=channel;
y = conv(x_GI,h);
end
if i==0 % 只測量增加的AWGN噪聲的訊號功率
y1=y(1:NframeNsym);
sigPow = sigPow + y1y1’;
continue;
end
% 加AWGN噪聲________________________________________________
snr = EbN0(i)+10log10(Nbps(Nused/Nfft)); % SNR vs. Eb/N0 by Eq.(4.28)
noise_mag = sqrt((10.^(-snr/10))sigPow/2);
y_GI = y + noise_mag(randn(size(y))+1i*randn(size(y)));
% Rx_____________________________________________________________
kk1=(NgType==2)*Ng+(1:Nsym);
kk2=1:Nfft;
kk3=1:Nused;
kk4=Nused/2+Nvc+1:Nfft;
kk5=(Nvc~=0)+(1:Nused/2);
if Ch==1
H= fft([h zeros(1,Nfft-Lch)]); % 通道頻率響應
H_shift(kk3)= [H(kk4) H(kk5)];
end
for k=1:Nframe
Y(kk2)= fft(remove_GI(Ng,Nsym,NgType,y_GI(kk1)));
Y_shift=[Y(kk4) Y(kk5)];
if Ch==0
Xmod_r(kk3) = Y_shift;
else
Xmod_r(kk3)=Y_shift./H_shift; % 均衡器
end
kk1=kk1+Nsym;
kk2=kk2+Nfft;
kk3=kk3+Nused;
kk4=kk4+Nfft;
kk5=kk5+Nfft;
end
X_r=qamdemod(Xmod_r*norms(Nbps),M,'gray');
Neb=Neb+sum(sum(de2bi(X_r,Nbps)~=de2bi(X,Nbps))); % 判決
Ntb=Ntb+Nused*Nframe*Nbps; %[Ber,Neb,Ntb]=ber(bit_Rx,bit,Nbps);
if Neb>Target_neb,break;end
end
if i==0
sigPow= sigPow/Nsym/Nframe/N_iter;
else
Ber = Neb/Ntb;
fprintf('EbN0=%3d[dB], BER=%4d/%8d =%11.3e\n', EbN0(i), Neb,Ntb,Ber)
fprintf(fid, '%d\t%11.3e\n', EbN0(i), Ber);
if Ber<1e-6, break; end
end
end
if (fid~=0)
fclose(fid);
end
disp(‘Simulation is finished’)
ber_AWGN = ber_QAM(EbN0,M,‘AWGN’);
ber_Rayleigh = ber_QAM(EbN0,M,‘Rayleigh’);
semilogy(EbN0,ber_AWGN,‘r:’), hold on
semilogy(EbN0,ber_Rayleigh,‘m-’)
a= load(file_name);
semilogy(a(:,1),a(:,2),‘b–d’), grid on
xlabel(‘EbN0[dB]’), ylabel(‘BER’)
axis([a(1,1) 30 1e-4 1])
Ch=1; % 對於AWGN/多徑瑞利通道,channelCh=0/1
file_name=[‘OFDM_BER_’ chType ‘’ nt '’ ‘GL’ num2str(Ng) ‘.dat’];
fid=fopen(file_name, ‘w+’);
for i=0:length(EbN0)
randn(‘state’,0);rand(‘state’,0);
%Ber2=ber(); 初始化BER
Neb=0; % 初始化錯誤位元數
Ntb=0; % 初始化總位元數
for m=1:N_iter
% Tx______________________________________________________________
X= randi([0 M-1],1,NusedNframe); % bit: 整數向量(問題:randint怎麼換成randi)
Xmod= qammod(X,M,‘gray’)/norms(Nbps);
if NgType~=2
x_GI=zeros(1,NframeNsym);
elseif NgType2
x_GI= zeros(1,Nframe*Nsym+Ng);
% 用Ng個零擴充套件OFDM符號
end
kk1=(1:Nused/2);
kk2=(Nused/2+1:Nused);
kk3=1:Nfft;
kk4=1:Nsym;
for k=1:Nframe
if Nvc~=0
X_shift= [0 Xmod(kk2) zeros(1,Nvc-1) Xmod(kk1)];
else
X_shift= [Xmod(kk2) Xmod(kk1)];
end
x= ifft(X_shift);
x_GI(kk4)= guard_interval(Ng,Nfft,NgType,x);
kk1=kk1+Nused;
kk2= kk2+Nused;
kk3=kk3+Nfft;
kk4=kk4+Nsym;
end
if Ch0
y= x_GI; % 沒有通道
else % 多徑衰落通道
channel=(randn(1,Ntap)+1irandn(1,Ntap)).sqrt(Power/2);
h=zeros(1,Lch); %通道脈衝響應
h(Delay+1)=channel;
y = conv(x_GI,h);
end
if i==0 % 只測量增加的AWGN噪聲的訊號功率
y1=y(1:NframeNsym);
sigPow = sigPow + y1y1’;
continue;
end
% 加AWGN噪聲________________________________________________
snr = EbN0(i)+10log10(Nbps(Nused/Nfft)); % SNR vs. Eb/N0 by Eq.(4.28)
noise_mag = sqrt((10.^(-snr/10))sigPow/2);
y_GI = y + noise_mag(randn(size(y))+1i*randn(size(y)));
% Rx_____________________________________________________________
kk1=(NgType==2)*Ng+(1:Nsym);
kk2=1:Nfft;
kk3=1:Nused;
kk4=Nused/2+Nvc+1:Nfft;
kk5=(Nvc~=0)+(1:Nused/2);
if Ch==1
H= fft([h zeros(1,Nfft-Lch)]); % 通道頻率響應
H_shift(kk3)= [H(kk4) H(kk5)];
end
for k=1:Nframe
Y(kk2)= fft(remove_GI(Ng,Nsym,NgType,y_GI(kk1)));
Y_shift=[Y(kk4) Y(kk5)];
if Ch==0
Xmod_r(kk3) = Y_shift;
else
Xmod_r(kk3)=Y_shift./H_shift; % 均衡器
end
kk1=kk1+Nsym;
kk2=kk2+Nfft;
kk3=kk3+Nused;
kk4=kk4+Nfft;
kk5=kk5+Nfft;
end
X_r=qamdemod(Xmod_r*norms(Nbps),M,'gray');
Neb=Neb+sum(sum(de2bi(X_r,Nbps)~=de2bi(X,Nbps))); % 判決
Ntb=Ntb+Nused*Nframe*Nbps; %[Ber,Neb,Ntb]=ber(bit_Rx,bit,Nbps);
if Neb>Target_neb,break;end
end
if i==0
sigPow= sigPow/Nsym/Nframe/N_iter;
else
Ber = Neb/Ntb;
fprintf('EbN0=%3d[dB], BER=%4d/%8d =%11.3e\n', EbN0(i), Neb,Ntb,Ber)
fprintf(fid, '%d\t%11.3e\n', EbN0(i), Ber);
if Ber<1e-6, break; end
end
end
if (fid~=0)
fclose(fid);
end
disp(‘Simulation is finished’)
a= load(file_name);
semilogy(a(:,1),a(:,2),‘g–*’), grid on
NgType=2; % 對於CP或ZP,NgType=1或2
file_name=[‘OFDM_BER_’ chType ‘’ nt '’ ‘GL’ num2str(Ng) ‘.dat’];
fid=fopen(file_name, ‘w+’);
for i=0:length(EbN0)
randn(‘state’,0);rand(‘state’,0);
%Ber2=ber(); 初始化BER
Neb=0; % 初始化錯誤位元數
Ntb=0; % 初始化總位元數
for m=1:N_iter
% Tx______________________________________________________________
X= randi([0 M-1],1,NusedNframe); % bit: 整數向量(問題:randint怎麼換成randi)
Xmod= qammod(X,M,‘gray’)/norms(Nbps);
if NgType~=2
x_GI=zeros(1,NframeNsym);
elseif NgType2
x_GI= zeros(1,Nframe*Nsym+Ng);
% 用Ng個零擴充套件OFDM符號
end
kk1=(1:Nused/2);
kk2=(Nused/2+1:Nused);
kk3=1:Nfft;
kk4=1:Nsym;
for k=1:Nframe
if Nvc~=0
X_shift= [0 Xmod(kk2) zeros(1,Nvc-1) Xmod(kk1)];
else
X_shift= [Xmod(kk2) Xmod(kk1)];
end
x= ifft(X_shift);
x_GI(kk4)= guard_interval(Ng,Nfft,NgType,x);
kk1=kk1+Nused;
kk2= kk2+Nused;
kk3=kk3+Nfft;
kk4=kk4+Nsym;
end
if Ch0
y= x_GI; % 沒有通道
else % 多徑衰落通道
channel=(randn(1,Ntap)+1irandn(1,Ntap)).sqrt(Power/2);
h=zeros(1,Lch); %通道脈衝響應
h(Delay+1)=channel;
y = conv(x_GI,h);
end
if i==0 % 只測量增加的AWGN噪聲的訊號功率
y1=y(1:NframeNsym);
sigPow = sigPow + y1y1’;
continue;
end
% 加AWGN噪聲________________________________________________
snr = EbN0(i)+10log10(Nbps(Nused/Nfft)); % SNR vs. Eb/N0 by Eq.(4.28)
noise_mag = sqrt((10.^(-snr/10))sigPow/2);
y_GI = y + noise_mag(randn(size(y))+1i*randn(size(y)));
% Rx_____________________________________________________________
kk1=(NgType==2)*Ng+(1:Nsym);
kk2=1:Nfft;
kk3=1:Nused;
kk4=Nused/2+Nvc+1:Nfft;
kk5=(Nvc~=0)+(1:Nused/2);
if Ch==1
H= fft([h zeros(1,Nfft-Lch)]); % 通道頻率響應
H_shift(kk3)= [H(kk4) H(kk5)];
end
for k=1:Nframe
Y(kk2)= fft(remove_GI(Ng,Nsym,NgType,y_GI(kk1)));
Y_shift=[Y(kk4) Y(kk5)];
if Ch==0
Xmod_r(kk3) = Y_shift;
else
Xmod_r(kk3)=Y_shift./H_shift; % 均衡器
end
kk1=kk1+Nsym;
kk2=kk2+Nfft;
kk3=kk3+Nused;
kk4=kk4+Nfft;
kk5=kk5+Nfft;
end
X_r=qamdemod(Xmod_r*norms(Nbps),M,'gray');
Neb=Neb+sum(sum(de2bi(X_r,Nbps)~=de2bi(X,Nbps))); % 判決
Ntb=Ntb+Nused*Nframe*Nbps; %[Ber,Neb,Ntb]=ber(bit_Rx,bit,Nbps);
if Neb>Target_neb,break;end
end
if i==0
sigPow= sigPow/Nsym/Nframe/N_iter;
else
Ber = Neb/Ntb;
fprintf('EbN0=%3d[dB], BER=%4d/%8d =%11.3e\n', EbN0(i), Neb,Ntb,Ber)
fprintf(fid, '%d\t%11.3e\n', EbN0(i), Ber);
if Ber<1e-6, break; end
end
end
if (fid~=0)
fclose(fid);
end
disp(‘Simulation is finished’)
a= load(file_name);
semilogy(a(:,1),a(:,2),‘k–s’), grid on
legend(‘AWGN analytic’,‘Rayleigh fading analytic’, ‘AWGN,no guard interval’,‘Channel,CP:16’,‘Channel,ZP:16’)
功能函式:
function ber=ber_QAM(EbN0dB,M,AWGN_or_Rayleigh)
% 找到AWGN或瑞利通道中M-QAM的BER
% EbN0dB: AWGN通道的每位元訊號與噪聲功率比[dB]
% =rdB : Average SNR(2sigma Eb/N0)[dB] for Rayleigh channel
% M = 調製階數 (星座大小)
N= length(EbN0dB);
sqM= sqrt(M);
a= 2(1-power(sqM,-1))/log2(sqM);
b= 6log2(sqM)/(M-1);
if nargin<3
AWGN_or_Rayleigh=‘AWGN’;
end
if lower(AWGN_or_Rayleigh(1))==‘a’
ber = aQ(sqrt(b10.^(EbN0dB/10))); % ber=berawgn(EbN0dB,’QAM’,M) Eq.(4.25)
else % diversity_order=1; ber=berfading(EbN0dB,’QAM’,M,diversity_order)
rn=b10.^(EbN0dB/10)/2;
ber = 0.5a(1-sqrt(rn./(rn+1))); % Eq.(4.26)
end
function y = guard_interval(Ng,Nfft,NgType,ofdmSym)
if NgType1
y=[ofdmSym(Nfft-Ng+1:Nfft) ofdmSym(1:Nfft)];
elseif NgType2
y=[zeros(1,Ng) ofdmSym(1:Nfft)];
end
function y=remove_GI(Ng,Lsym,NgType,ofdmSym)
if Ng~=0
if NgType1
y=ofdmSym(Ng+1:Lsym); % 迴圈字首
elseif NgType2 % 迴圈字尾
y=ofdmSym(1:Lsym-Ng)+[ofdmSym(Lsym-Ng+1:Lsym) zeros(1,Lsym-2*Ng)];
end
else
y=ofdmSym;
end
function y=Q(x)
% 誤差函式: 1/sqrt(2*pi) * int_x^inf exp(-t^2/2) dt. % Eq.(4.27)
y=erfc(x/sqrt(2))/2;