Matlab仿真DF和AF源程序.doc_第1页
Matlab仿真DF和AF源程序.doc_第2页
Matlab仿真DF和AF源程序.doc_第3页
Matlab仿真DF和AF源程序.doc_第4页
Matlab仿真DF和AF源程序.doc_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

版权所有,如转载,请注明出处。说明: 该程序是使用Matlab仿真协同通信的DF(解码转发)基本性能,基本模型S-R-D三个节点,接收端使用MRC(最大比合并)。固定DF由于SR信道的错误,其性能受到限制,在大SNR情况下不能获得有效分集。而当SR距离足够近的时候,可能获得一定的分集增益。 该程序也可以作为学习Matlab 通信仿真的入门实例。使用的基本函数:随机数函数 :rand randn 文件操作:fopen fprintf fclose 时间函数:datestr(now)画图函数:semilogylegendaxis通信基本概念和函数: 瑞利信道及其产生 高斯噪声的产生蒙特卡罗仿真 MRC最大比合并程序一:DF_SNR_SR_distance.m 主程序%written by xiyong8260 25/08/2009%this code is for fixed DF performance evaluation, compare the diffirent SR distance%该仿真用来评估不同的SR距离下的固定DF转发性能%for S-R-D three nodes%the SD and RD channel is Rayleigh fading, and the average SNR is varied.clear all;%the times of Monte Carlo%蒙特卡罗仿真的次数Monte_MAX=10(7);%the length of data frameM2=10(0);MIN_SNR=0;MAX_SNR=30;INTERVAL=2;%功率划分因子,在协同情况下,为了保证总的功率一定,每个节点使用1/2的功率发送POW_DIV=1/2;%open a new record file%打开一个新的文件,如果文件已经存在,那么清除以前的内容fid=fopen(record.txt,w);fprintf(fid,%sn,datestr(now);fclose(fid);%set the SD distance and RD distance%设置SD距离和RD的距离,归一化为1sd_distance=1;rd_distance=1;%变化不同的SR距离sr_distances=0.1,0.5,0.7,0.9;%使用循环的方式仿真不同的SR距离性能for sr_INDEX=1:2 %通过sr_INDEX变量获得真正的SR距离 sr_distance=sr_distances(sr_INDEX); %snrcount这个变量用来对下面的SNR循环进行计数 snrcount=0; %变化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_dsr0)*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_dsd0)*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_combine0)*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); end% SNR=MIN_SNR:MAX_SNR %以追加的方式打开文件,将数据写入记录文件 fid=fopen(record.txt,a); fprintf(fid,n%ber_dsd isnber_dsd%d= ,sr_INDEX); fprintf(fid,%d ,ber_dsd); fprintf(fid,;n); fprintf(fid,n%fixed ber_coop isnber_coop%d= ,sr_INDEX); fprintf(fid,%d ,ber_coop); fprintf(fid,;n); fclose(fid); end%Sr indexSNR1=MIN_SNR:INTERVAL: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);程序二:xy_noise.m 高斯噪声函数的产生function M=xy_noise(M2)M=sqrt(1/2)*(randn(1,M2)+j*randn(1,M2);程序三:xy_RayleighCH.m瑞利信道的产生function H=xy_RayleighCH(M2)H=sqrt(1/2)*(randn(1,M2)+j*randn(1,M2);record.txt文件样例%19-Sep-2009 18:27:18%ber_dsd isber_dsd1= 2.306000e-002 1.564000e-002 9.620000e-003 6.000000e-003 3.870000e-003 2.600000e-003 ;%fixed ber_coop isber_coop1= 5.540000e-003 2.710000e-003 1.070000e-003 3.300000e-004 1.800000e-004 4.000000e-005 ;%ber_dsd isber_dsd2= 2.348000e-002 1.516000e-002 9.400000e-003 6.480000e-003 4.060000e-003 2.380000e-003 ;%fixed ber_coop isber_coop2= 7.070000e-003 3.320000e-003 1.740000e-003 8.300000e-004 5.100000e-004 3.000000e-004 ;由于蒙特卡罗仿真时间耗时较长,使用文件操作的好处是可以中途看数据,在计算机出现故障或者断电情况下将已经仿真的数据保存起来。程序四:未经过解释的AF源程序%written by xiyong8260 19/10/2009%this code is for AF performance evaluation%for S-R-D three nodes%all the channel is Rayleigh fading, and the average SNR is varied.clear all;%the times of Monte CarloMonte_MAX=10(7);%the length set of data frameM=1,10,100,1000,10000;MIN_SNR=0;MAX_SNR=30;INTEVAL=2;POW_DIV=1/2;fid=fopen(record.txt,w);fprintf(fid,%sn,datestr(now);fclose(fid);sr_distances=0.1,0.5,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_dsd0)*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);%关键点之二:合并时候的噪声功率归一化处理 R_com

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论