%变化SNR循环
for SNR=MIN_SNR:INTERVAL:MAX_SNR
sig=10^(SNR/10);
%just display something on the screen.
%仅仅是在计算机上显示一些信息,因为仿真往往很漫长,只是想知道仿真到哪里了^.^
datestr(now)
M2
SNR
snrcount=snrcount+1;
%用来对错误的bit计数
err_num_dsd=0;
err_num_coop=0;
for tries=0:Monte_MAX
%flag indicate if the cooperate is used, if 1, use cooperation,else direct tran is perferred.
%一个标志,表明是否转发
tx_coop=1;
%generate the source BPSK signal, 0,1
%产生一个长度为M2的0,1随机数
X1=rand(1,M2)>0.5;
%generate the source BPSK signal,+1,-1
%产生一个长度为M2的BPSK随机信号
Xs=X1*2-1;
%generate the source-relay channel, the channel keep constant in a frame
%产生SR信道,假设信道在一帧内保持不变
CH_sr=xy_RayleighCH(1)/(sr_distance)^2;
%the received signal of source-relay transmission
%中继接收到的源的信号,y=hx+n
R_dsr=CH_sr*sqrt(POW_DIV*sig).*Xs+xy_noise(M2);
%the decode signal of source-relay transmission
%中继对信道进行估计,匹配后硬判决得到的源的信号,根据MRC(最大比合并)的基本原理
Xr=(conj(CH_sr)*sqrt(POW_DIV*sig).*R_dsr>0)*2-1;
%如果中继解码错误,则不转发
if (sum(Xs~=Xr)>0) tx_coop=0; end
%如果使用固定DF,我们强制设置tx_coop为1
tx_coop=1;
%generate the source-destination channel, the channel keep constant in a frame
%产生SD信道,假设信道在一帧内保持不变
CH_sd=xy_RayleighCH(1)/(sd_distance)^2;
%the received signal of direct source-destination transmission
%假设不使用协同,直接传输情况下目的节点接收到的源的信号,y=hx+n,这里,源节点使用全功率发送
R_dsd=CH_sd*sqrt(sig).*Xs+xy_noise(M2);
%the decode signal of direct source-destination transmission
%目的节点对信道进行估计,匹配后硬判决得到的源的信号,根据MRC(最大比合并)的基本原理
Y_dsd=(conj(CH_sd)*sqrt(sig).*R_dsd>0)*2-1;
%the received signal of cooperative source-destination transmission
%在使用协同情况下,目的节点接收到的源的信号,y=hx+n,这里,源节点使用半功率发送
R_csd=CH_sd*sqrt(POW_DIV*sig).*Xs+xy_noise(M2);
%generate the relay-destination channel, the channel keep constant in a frame
%产生RD信道,假设信道在一帧内保持不变
CH_rd=xy_RayleighCH(1)/(rd_distance)^2;
if (tx_coop==1)
%the received signal of cooperative relay-destination transmission
%在使用协同情况下,目的节点接收到的来自中继的信号
R_crd=CH_rd*sqrt(POW_DIV*sig).*Xr+xy_noise(M2);
%MRC的合并信号
R_combine=conj(CH_sd)*sqrt(POW_DIV*sig).*R_csd+conj(CH_rd)*sqrt(POW_DIV*sig).*R_crd;
else
%if direct transmission is chosen, the source just retransmit the signal again.
%如果不使用协同,源节点重复发送自己的数据,假设两次重复发送期间信道保持不变
R_crd=CH_sd*sqrt(POW_DIV*sig).*Xs+xy_noise(M2);
%MRC的合并信号
R_combine=conj(CH_sd)*sqrt(POW_DIV*sig).*R_csd+conj(CH_sd)*sqrt(POW_DIV*sig).*R_crd;
end
%the decode signal of of cooperative MRC combining
%MRC后的判决信号
Y_combine=(R_combine>0)*2-1;
%the number of error bit
%统计每一帧里面错误的比特数目
err_num_dsd=sum(Xs~=Y_dsd)+err_num_dsd;
err_num_coop=sum(Xs~=Y_combine)+err_num_coop;
end %try=0:Monte_MAX
%the real ber
%计算每个SNR下的平均错误BER
ber_dsd(snrcount)=err_num_dsd/(M2*Monte_MAX);
ber_coop(snrcount)=err_num_coop/(M2*Monte_MAX);