close all;
clear;
clc;
% echo on
M=input('Please input modulation order M:_(like 4 or 16), or press Enter directly to exit\n');
if(isempty(M)==1)
disp('You have exited');
return;
end
SNRindB=input('Please input SNR(dB) range:_(like 0:1:2), or press Enter directly to exit\n');
if(isempty(SNRindB)==1)
disp('You have exited');
return;
end
frame=input('Please input frame number:_(like 1e1), or press Enter directly to exit\n');
if(isempty(frame)==1)
disp('You have exited');
return;
end
iteration=input('Please input max number of iterations:_(like 10), or press Enter directly to exit\n');
if(isempty(iteration)==1)
disp('You have exited');
return;
end
N=288;
R=1/2;
Eav=1;
num_of_subcarriers_used=144;
segment=(N/R)/(num_of_subcarriers_used*log2(M));
num_of_subcarriers=256;
L=32;
AWGN_numofbiterror=zeros(1,length(SNRindB));
AWGN_numofframeerror=zeros(1,length(SNRindB));
AWGN_iteration_in_SNR=zeros(length(SNRindB),iteration);%统计译码迭代次数
Rayleigh_numofbiterror=zeros(1,length(SNRindB));
Rayleigh_numofframeerror=zeros(1,length(SNRindB));
Rayleigh_iteration_in_SNR=zeros(length(SNRindB),iteration);
MFB_numofbiterror=zeros(1,length(SNRindB));
MFB_numofframeerror=zeros(1,length(SNRindB));
MFB_iteration_in_SNR=zeros(length(SNRindB),iteration);%统计译码迭代次数
for f=1:frame
% AWGN_frameerror=zeros(1,length(SNRindB));
% Rayleigh_frameerror=zeros(1,length(SNRindB));
% MFB_frameerror=zeros(1,length(SNRindB));
bit0=randi([0 1],N,1);
AWGN_decision=zeros(N,1);
Rayleigh_decision=zeros(N,1);
MFB_decision=zeros(N,1);
%编码
bit=LDPC_Encode_half(bit0);
% new_bit=LDPC_Encode_two_thirds(bit);
%调制
new_bit=reshape(bit,segment*log2(M),num_of_subcarriers_used);%串并转换 %reshape 函数是逐列开始转换,即按矩阵的每一列开始读取,并按列转换为新矩阵
S=qammod(new_bit,M,'InputType','bit','UnitAveragePower',true);
new_S=zeros(num_of_subcarriers,segment);
new_S([2:num_of_subcarriers_used/2+1,...
num_of_subcarriers-num_of_subcarriers_used/2+1:num_of_subcarriers],:)=S.';
s=sqrt(num_of_subcarriers).*ifft(new_S,num_of_subcarriers);%IDFT
x=[s(num_of_subcarriers-L+1:num_of_subcarriers,:);s];%加循环前缀
x=reshape(x,1,(num_of_subcarriers+L)*segment);%并串转换
%Rayleigh信道
hq=sqrt(1/(2*L)).*randn(L,1);
hr=sqrt(1/(2*L)).*randn(L,1);
h=complex(hq,hr);%h
SUM=h'*h;
newh=zeros(num_of_subcarriers,1);
newh(1:L,1)=h;
H=fft(newh);%信道在各载波的频响
H_used=H([2:num_of_subcarriers_used/2+1,...
num_of_subcarriers-num_of_subcarriers_used/2+1:num_of_subcarriers],1);
modul_H=abs(H_used);
for l=1:length(SNRindB)
noise_power=Eav*10^(-SNRindB(l)/10);
%接收信号及解调软信息
%AWGN
AWGN_y=awgn(x,SNRindB(l));
AWGN_y=reshape(AWGN_y,num_of_subcarriers+L,segment);%串并转换
AWGN_r=AWGN_y(L+1:L+num_of_subcarriers,:);%去除循环前缀
AWGN_R=sqrt(1/num_of_subcarriers).*fft(AWGN_r,num_of_subcarriers);
awgn_R=AWGN_R([2:num_of_subcarriers_used/2+1,...
num_of_subcarriers-num_of_subcarriers_used/2+1:num_of_subcarriers],:);
LLR_AWGN=qamdemod(awgn_R.',M,'OutputType','approxllr',...
'UnitAveragePower',true,'NoiseVariance', noise_power);
LLR_AWGN=reshape(LLR_AWGN,1,N/R);
%MFB
MFB_y=awgn(sqrt(SUM).*x,SNRindB(l));
MFB_y=reshape(MFB_y,num_of_subcarriers+L,segment);%串并转换
MFB_r=MFB_y(L+1:L+num_of_subcarriers,:);%去除循环前缀
new_MFB_r=MFB_r./sqrt(SUM);%均衡
MFB_R=sqrt(1/num_of_subcarriers).*fft(new_MFB_r,num_of_subcarriers);
mfb_R=MFB_R([2:num_of_subcarriers_used/2+1,...
num_of_subcarriers-num_of_subcarriers_used/2+1:num_of_subcarriers],:);
LLR_MFB=qamdemod(mfb_R.',M,'OutputType','approxllr',...
'UnitAveragePower',true,'NoiseVariance', noise_power/SUM);
LLR_MFB=reshape(LLR_MFB,1,N/R);
%Rayleigh
R_y=conv(x,h);
Rayleigh_y=awgn(R_y(1,1:(num_of_subcarriers+L)*segment),SNRindB(l));
Rayleigh_y=reshape(Rayleigh_y,num_of_subcarriers+L,segment);%串并转换
Rayleigh_r=Rayleigh_y(L+1:L+num_of_subcarriers,:);%去除循环前缀
Rayleigh_R=sqrt(1/num_of_subcarriers).*fft(Rayleigh_r,num_of_subcarriers);
rayleigh_R=Rayleigh_R([2:num_of_subcarriers_used/2+1,...
num_of_subcarriers-num_of_subcarriers_used/2+1:num_of_subcarriers],:);
new_rayleigh_R=diag(1./H_used,0)*rayleigh_R;%均衡
LLR_Rayleigh=qamdemod(new_rayleigh_R.',M,'OutputType','approxllr',...
'UnitAveragePower',true,'NoiseVariance', noise_power*modul_H.^-2);
LLR_Rayleigh=reshape(LLR_Rayleigh,1,N/R);
%译码
%AWGN
[AWGN_decision,AWGN_num_of_iteration]=LDPC_Decode_half(LLR_AWGN,iteration);
AWGN_iteration_in_SNR(l,AWGN_num_of_iteration)=...
AWGN_iteration_in_SNR(l,AWGN_num_of_iteration)+1;%迭代次数对应位置加1
%MFB
[MFB_decision,MFB_num_of_iteration]=LDPC_Decode_half(LLR_MFB,iteration);
MFB_iteration_in_SNR(l,MFB_num_of_iteration)=...
MFB_iteration_in_SNR(l,MFB_num_of_iteration)+1;%迭代次数对应位置加1
%Rayleigh
[Rayleigh_decision,Rayleigh_num_of_iteration]=...
LDPC_Decode_half(LLR_Rayleigh,iteration);
Rayleigh_iteration_in_SNR(l,Rayleigh_num_of_iteration)=...
Rayleigh_iteration_in_SNR(l,Rayleigh_num_of_iteration)+1;
%统计
%AWGN
% for i=1:num
% if(AWGN_decision(i)~=bit0(i))
% AWGN_numofbiterror(l)=AWGN_numofbiterror(l)+1;
% AWGN_frameerror(l)=1;
% end
% end
AWGN_error=sum(xor(AWGN_decision,bit0));
AWGN_numofbiterror(l)=AWGN_numofbiterror(l)+AWGN_error;
if (AWGN_error~=0)
AWGN_numofframeerror(l)=AWGN_numofframeerror(l)+1;
end
%MFB
% for i=1:num
% if(MFB_decision(i)~=bit0(i))
% MFB_numofbiterror(l)=MFB_numofbiterror(l)+1;
% MFB_frameerror(l)=1;
% end
% end
MFB_error=sum(xor(MFB_decision,bit0));
MFB_numofbiterror(l)=MFB_numofbiterror(l)+MFB_error;
if (MFB_error~=0)
MFB_numofframeerror(l)=MFB_numofframeerror(l)+1;
end
%Rayleigh
% for i=1:N
% if(Rayleigh_decision(i)~=bit0(i))
% Rayleigh_numofbiterror(l)=Rayleigh_numofbiterror(l)+1;
% Rayleigh_frameerror(l)=1;
% end
% end
Rayleigh_error=sum(xor(Rayleigh_decision,bit0));
Rayleigh_numofbiterror(l)=Rayleigh_numofbiterror(l)+Rayleigh_error;
if (+Rayleigh_error~=0)
Rayleigh_numofframeerror(l)=Rayleigh_numofframeerror(l)+1;
end
end
% echo off;
end
AWGN_pf=AWGN_numofframeerror/frame;
AWGN_pb=AWGN_numofbiterror/(frame*N);
Rayleigh_pf=Rayleigh_numofframeerror/frame;
Rayleigh_pb=Rayleigh_numofbiterror/(frame*N);
MFB_pf=MFB_numofframeerror/frame;
MFB_pb=MFB_numofbiterror/(frame*N);
figure(1);
%bxy=spcrv([SNRindB;smld_bit_err_prb],3);
semilogy(SNRindB,AWGN_pb,'r*-');
hold on
semilogy(SNRindB,Rayleigh_pb,'ks--');
hold on
semilogy(SNRindB,MFB_pb,'bp-.');
grid on
legend('AWGN','Rayleigh慢衰落','Rayleigh慢衰落下的MFB','location','southwest');
%title('OFDM系统误比特率')
xlabel('SNR/dB')
ylabel('BER')
figure(2);
%fxy=spcrv([SNRindB;smld_frame_err_prb],3);
semilogy(SNRindB,AWGN_pf,'r*-');
hold on
semilogy(SNRindB,Rayleigh_pf,'ks--');
hold on
semilogy(SNRindB,MFB_pf,'bp-.');
grid on
legend('AWGN','Rayleigh慢衰落','Rayleigh慢衰落下的MFB','location','southwest');
%title('OFDM系统误帧率')
xlabel('SNR/dB')
ylabel('FER')