main1.m
上传用户:cjx922
上传日期:2017-12-13
资源大小:205k
文件大小:5k
源码类别:

通讯编程

开发平台:

Matlab

  1. %简单main函数,用来测试基本的协同过程
  2. tic
  3. nr_of_iterations=1000; %循环次数
  4. SNR=[-10:2.5:15];%dB形式
  5. use_direct_link=1;%直接传输
  6. use_relay=1;%通过伙伴传输
  7. P=2;%总功率为2
  8. global signal;%发送信号的全局变量
  9. signal=generate_signal_structure;%发送信号结构体,包括传输比特数、传输字符数、每个字符包含的比特数、信号的调制方式、发送信号的比特序列、
  10.                                  %发送信号的字符序列,传输完毕后接收到的比特序列,用来求误码率.
  11. signal(1).modulation_type='QPSK'
  12. signal.nr_of_bits=2^10;
  13. signal.position_x=-1;
  14. siganl.position_y=0;
  15. calculate_signal_parameter;%发送信号准备完毕
  16. channel=generate_channel_structure;
  17. channel(1).attenuation(1).pattern='Rayleigh';
  18. channel.attenuation(1).block_length=1;
  19. channel(2)=channel(1);
  20. channel(3)=channel(1);
  21. channel(1).attenuation.distance=1;
  22. channel(2).attenuation.distance=0.75;
  23. channel(3).attenuation.distance=0.25;
  24. rx=generate_rx_structure;
  25. rx(1).combining_type='ESNRC';
  26. rx.rx_position_x=1;
  27. rx.rx_position_y=0;
  28. rx.sd_weight=2;
  29. rx(2)=rx(1);%负责没有协同情况下的接收
  30. global relay;
  31. relay=generate_relay_structure;
  32. relay(1).mode='AAF';       
  33. relay.magic_genie=0;
  34. relay.rx=rx(1);
  35. h = waitbar(0,'Please wait...');
  36. BER=zeros(size(SNR));
  37. BER_1sender=zeros(size(SNR));
  38. for iSNR=1:size(SNR,2)
  39.     waitbar(iSNR/size(SNR,2));
  40.     channel(1).noise(1).SNR=SNR(iSNR);
  41.     channel(2).noise(1).SNR=SNR(iSNR);
  42.     channel(3).noise(1).SNR=SNR(iSNR);  
  43.     for it=1:nr_of_iterations
  44.         %Reset receiver
  45.         rx(1)=rx_reset(rx(1));
  46.         rx(2)=rx_reset(rx(2));%负责非协同情况
  47.         relay.rx=rx_reset(relay.rx);
  48.         
  49.         [channel(1)]=get_channel_muti_parameter(channel(1),signal.symbol_sequence,1);
  50.         [noise_vector_sd,channel(1),asd]=get_channel_white_noise(channel(1),signal.symbol_sequence);
  51.         
  52.         [channel(2)]=get_channel_muti_parameter(channel(2),signal.symbol_sequence,1);
  53.         [noise_vector_sr,channel(2),asr]=get_channel_white_noise(channel(2),signal.symbol_sequence);
  54.         
  55.         [channel(3)]=get_channel_muti_parameter(channel(3),signal.symbol_sequence,1);
  56.         [noise_vector_rd,channel(3),ard]=get_channel_white_noise(channel(3),signal.symbol_sequence);
  57.         
  58.         %功率分配
  59.         [Ps,Pr]=power_allocate(P,asd,asr,ard,'ESNRC','Best','AAF',rx(1).sd_weight);
  60.         
  61.         %direct link
  62.         if(use_direct_link==1)
  63.         [rx(1)]=add_PA_and_channel_effect(channel(1),signal.symbol_sequence,rx(1),Ps,noise_vector_sd);
  64.         rx(1)=rx_correct_phaseshift(rx(1),channel(1).attenuation.phi);
  65.         end
  66.         
  67.         
  68.         %Multi-hop
  69.         if (use_relay==1)
  70.             %sender to relay
  71.             [relay.rx]=add_PA_and_channel_effect(channel(2),signal.symbol_sequence,relay.rx,Ps,noise_vector_sr);
  72.             relay=prepare_relay2send(relay,channel(2),Ps,Pr);
  73.             %relay to destination
  74.             [rx(1)]=add_PA_and_channel_effect(channel(3),relay.signal2send,rx(1),Pr,noise_vector_rd);
  75.             switch relay.mode
  76.                 case 'AAF'
  77.                     rx(1)=rx_correct_phaseshift(rx(1),channel(2).attenuation.phi+channel(3).attenuation.phi);
  78.                 case 'DAF'
  79.                     rx(1)=rx_correct_phaseshift(rx(1),channel(3).attenuation.phi);
  80.                 otherwise
  81.                     error(['Relay-mode unknown:',relay.mode]);
  82.             end
  83.         end
  84.       
  85.         
  86.         %receiver
  87.         [received_symbol,signal.received_bit_sequence]=rx_combine(rx(1),channel,use_relay,Ps);
  88.         
  89.         BER(iSNR)=BER(iSNR)+sum(not(signal.received_bit_sequence==signal.bit_sequence));
  90.         
  91.         %不协同情况,只有一条单链路
  92.         [rx(2)]=add_PA_and_channel_effect(channel(1),signal.symbol_sequence,rx(2),P,noise_vector_sd);
  93.         rx(2)=rx_correct_phaseshift(rx(2),channel(1).attenuation.phi);
  94.         [received_single_symbol,single_received_bit_sequence]=rx_combine(rx(2),channel,0,P);
  95.         BER_1sender(iSNR)=BER_1sender(iSNR)+sum(not(single_received_bit_sequence==signal.bit_sequence));
  96.     end%end of iteration
  97.     
  98.     BER(iSNR)=BER(iSNR)./it./signal.nr_of_bits;
  99.     BER_1sender(iSNR)=BER_1sender(iSNR)./it./signal.nr_of_bits;
  100.     %计算只有单链路的理论ber
  101.     SNR_avg(iSNR)=10^(SNR(iSNR)/10);
  102.     single_link_theo_ber(iSNR)=ber(SNR_avg(iSNR),'QPSK','Rayleigh');
  103. end; 
  104. close(h);
  105. %画出实际和理论误码率图
  106. %figure,
  107. %semilogy(SNR,single_link_theo_ber,'g-^');
  108. semilogy(SNR,BER,'r-<');
  109. %semilogy(SNR,BER,'g-+',SNR,BER_1sender,'r->');%SNR,single_link_theo_ber,'k-');
  110. %legend('Combining type FRC:2:1 QPSK Rayleign','theory 2sender','theory single link');
  111. xlabel('Eb/No (dB)');
  112. ylabel('BER');
  113. grid on;
  114. hold on;