clear all;
Monte_MAX=10^(5);
%the length set of data frame
M=[1,10,100,1000];
MIN_SNR=0;
MAX_SNR=30;
INTEVAL=2;
POW_DIV=1/2;
%/fid=fopen('record.txt','w');
%fprintf(fid,'%%%s\n',datestr(now));
%close(fid);
%sr_distance=[0.1,0.5,0.7,0.9];
sd_distance=1;
rd_distance=1;
sr_distance=1;
for M_INDEX=1:4
%the length of data frame
M2=M(M_INDEX);
count=0;
for SNR=MIN_SNR:INTEVAL:MAX_SNR
sig=10^(SNR/10);
count=count+1;
%just display something on the screen.
datestr(now)
M2
SNR
err_num_dsd=0;
err_num_coop=0;
for tries=0:Monte_MAX
%generate the source BPSK signal, 0,1
X1=rand(1,M2)>0.5;
%generate the source BPSK signal,+1,-1
Xs=X1*2-1;
%the transmitted signal
Xst=sqrt(POW_DIV*sig)*Xs; %%%%??????
%generate the source-relay channel, the channel is varied each bit
CH_sr=xy_RayleighCH(1)/(sr_distance)^2;
%the received signal of coop source-relay transmission
R_dsr=CH_sr*Xst+xy_noise(M2); %中继协作节点接收到的信号
%关键点之一:放大系数的选择
%the amplified signal of relay to destination
amp_coff=sqrt(POW_DIV*sig/(1+POW_DIV*sig*CH_sr*conj(CH_sr)));
Xr=R_dsr*amp_coff; %中继转发的信号
%the real ber, just for test
%cBER=sum(Xs~=Xr)/M2;
%if (sum(Xs~=Xr)>0) tx_coop=0; end
tx_coop=1;
%generate the source-destination channel
CH_sd=xy_RayleighCH(1)/(sd_distance)^2;
%the received signal of direct source-destination transmission
R_dsd=CH_sd*sqrt(sig).*Xs+xy_noise(M2);%源节点直传到目的节点的信号, %%%%??????
%the decode signal of direct source-destination transmission
Y_dsd=(conj(CH_sd)*sqrt(sig).*R_dsd>0)*2-1;%对直传接收信号译码
%the received signal of cooperative source-destination transmission
R_csd=CH_sd*Xst+xy_noise(M2);
%generate the relay-destination channel, the channel is varied each bit
CH_rd=xy_RayleighCH(1)/(rd_distance)^2;
%the received signal of cooperative relay-destination transmission
R_crd=CH_rd*Xr+xy_noise(M2);
%关键点之二:合并时候的噪声功率归一化处理 MRC最大比合并
R_combine=conj(CH_sd)*sqrt(POW_DIV*sig)*R_csd+amp_coff*sqrt(POW_DIV*sig)...
*conj(CH_sr)*conj(CH_rd).*R_crd/(1+amp_coff^2*conj(CH_rd)*CH_rd);
%the decode signal of cooperative MRC combining
Y_combine=(R_combine>0)*2-1;
%the number of packet error rate, if one bit is different, then the packet is discarded
if sum(Xs~=Y_dsd)>0
err_num_dsd=err_num_dsd+1;
end
if sum(Xs~=Y_combine)>0
err_num_coop= err_num_coop+1;
end
end %try=0:Monte_MAX
%the real ber
ber_dsd(count)=err_num_dsd/(Monte_MAX);
ber_coop(count)=err_num_coop/(Monte_MAX);
% ber_dsd(count)=err_num_dsd;
%ber_coop(count)=err_num_coop;
end % SNR=MIN_SNR:MAX_SNR
fid=fopen('record.txt','a');
fprintf(fid,'\n%%ber_dsd is\nber_dsd_ad%d=[ ',M_INDEX);
fprintf(fid,'%d ',ber_dsd);
fprintf(fid,'];\n');
fprintf(fid,'\n%%fixed ber_coop is\nber_coop_AF%d=[ ',M_INDEX);
fprintf(fid,'%d ',ber_coop);
fprintf(fid,'];\n');
fclose(fid);
end %M index
SNR1=MIN_SNR:INTEVAL:MAX_SNR;
%瑞利信道下的直接发送的理论BER表达式
sig1=10.^(SNR1/10);
u1=sqrt(sig1./(1+sig1));
Pe10=1/2*(1-u1);
%瑞利信道下的二阶分集理论BER表达式,使用半功率
sig2=1/2*10.^(SNR1/10);
u2=sqrt(sig2./(1+sig2));
Pe20=1/4*(1-u2).^(2).*(2+u2);
%使用半对数画出SNR和BER的曲线
semilogy(SNR1,ber_dsd,'-o',SNR1,ber_coop,'-+',SNR1,Pe10,'-+',SNR1,Pe20,'-*');
legend('direct','cooperative','L=1, therotic','L=2 therotic');
grid on;
ylabel('The AVERAGE BER');
xlabel('SNR(dB)');
title('the cooperative BER of BPSK in Rayleigh fading channel');
axis([MIN_SNR,MAX_SNR,10^(-6),1]);
- 1
- 2
- 3
前往页