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

通讯编程

开发平台:

Matlab

  1. %简单main函数,用来测试基本的协同过程
  2. tic
  3. nr_of_iterations=1000; %循环次数
  4. SNR=[-13:2.5:12];%dB形式
  5. use_direct_link=1;%直接传输
  6. use_relay=1;%通过伙伴传输
  7. global statistic;
  8. %statistic = generate_statistic_structure;
  9. global signal;%发送信号的全局变量
  10. signal=generate_signal_structure;%发送信号结构体,包括传输比特数、传输字符数、每个字符包含的比特数、信号的调制方式、发送信号的比特序列、
  11.                                  %发送信号的字符序列,传输完毕后接收到的比特序列,用来求误码率.
  12. signal(1).modulation_type='QPSK'
  13. signal.nr_of_bits=2^5;
  14. signal.position_x=-1;
  15. siganl.position_y=0;
  16. calculate_signal_parameter;%发送信号准备完毕
  17. channel=generate_channel_structure;
  18. channel(1).attenuation(1).pattern='Rayleigh';
  19. channel.attenuation(1).block_length=1;
  20. channel(2)=channel(1);
  21. channel(3)=channel(1);
  22. channel(1).attenuation.distance=1;
  23. channel(2).attenuation.distance=1;
  24. channel(3).attenuation.distance=1;
  25. rx=generate_rx_structure;
  26. rx(1).combining_type='ESNRC';
  27. rx.rx_position_x=1;
  28. rx.rx_position_y=0;
  29. rx.sd_weight=3;
  30. global relay;
  31. relay=generate_relay_structure;
  32. relay(1).mode='DAF';       
  33. relay.magic_genie=1;
  34. relay.rx=rx;
  35. h = waitbar(0,'Please wait...');
  36. BER=zeros(size(SNR));
  37. for iSNR=1:size(SNR,2)
  38.     waitbar(iSNR/size(SNR,2));
  39.     channel(1).noise(1).SNR=SNR(iSNR);
  40.     channel(2).noise(1).SNR=SNR(iSNR);
  41.     channel(3).noise(1).SNR=SNR(iSNR);  
  42.     for it=1:nr_of_iterations
  43.         %Reset receiver
  44.         rx=rx_reset(rx);
  45.         relay.rx=rx_reset(relay.rx);
  46.         
  47.         %direct link
  48.         if(use_direct_link==1)
  49.         [channel(1),rx]=add_channel_effect(channel(1),rx,signal.symbol_sequence);
  50.         rx=rx_correct_phaseshift(rx,channel(1).attenuation.phi);
  51.         end
  52.         
  53.         
  54.         %Multi-hop
  55.         if (use_relay==1)
  56.             %sender to relay
  57.             [channel(2),relay.rx]=add_channel_effect(channel(2),relay.rx,signal.symbol_sequence);
  58.             relay=prepare_relay2send(relay,channel(2));
  59.             %relay to destination
  60.             [channel(3),rx]=add_channel_effect(channel(3),rx,relay.signal2send);
  61.             switch relay.mode
  62.                 case 'AAF'
  63.                     rx=rx_correct_phaseshift(rx,channel(2).attenuation.phi+channel(3).attenuation.phi);
  64.                 case 'DAF'
  65.                     rx=rx_correct_phaseshift(rx,channel(3).attenuation.phi);
  66.                 otherwise
  67.                     error(['Relay-mode unknown:',relay.mode]);
  68.             end
  69.         end
  70.         %receiver
  71.         [received_symbol,signal.received_bit_sequence]=rx_combine(rx,channel,use_relay);
  72.         
  73.         BER(iSNR)=BER(iSNR)+sum(not(signal.received_bit_sequence==signal.bit_sequence));
  74.     end%end of iteration
  75.     
  76.     BER(iSNR)=BER(iSNR)./it./signal.nr_of_bits;
  77.      %计算两个等同发送者的理论ber 
  78.     SNR_avg(iSNR)=10^(SNR(iSNR)/10);
  79.     theo_ber(iSNR)=ber_2_sender(SNR_avg(iSNR),signal.modulation_type);
  80.     %计算只有单链路的理论ber
  81.     single_link_theo_ber(iSNR)=ber(2*SNR_avg(iSNR),'QPSK','Rayleigh');
  82. end; 
  83. close(h);
  84. %画出实际和理论误码率图
  85. %figure,
  86. %semilogy(SNR+3,BER,'b->',3+SNR,theo_ber,'k-',3+SNR,single_link_theo_ber,'b-');
  87. %legend('Combining type FRC:2:1 QPSK Rayleign','theory 2sender','theory single link');
  88. %xlabel('Eb/No (dB)');
  89. %ylabel('BER');
  90. %grid on;
  91. %hold on;
  92. % ------------------------------------
  93. % Present the result of the simulation
  94. txt_distance = [' - distance: ',...
  95. num2str(channel(1).attenuation.distance), ':',...
  96. num2str(channel(2).attenuation.distance), ':',...
  97. num2str(channel(3).attenuation.distance)];
  98. txt_distance='';
  99. if (use_relay == 1)
  100.       if (relay.magic_genie == 1)
  101.           txt_genie = ' - Magic Genie';
  102.       else
  103.           txt_genie = '';
  104.       end
  105.      txt_combining = [' - combining: ', rx(1).combining_type];
  106.      switch rx(1).combining_type
  107.         case 'FRC'
  108.              txt_combining = [txt_combining, ' ',...
  109.               num2str(rx(1).sd_weight),':1'];
  110.      end
  111.      add2statistic(3+SNR,BER,[signal.modulation_type, ' - ',...
  112.      relay.mode, txt_combining, txt_distance, txt_genie])
  113. else
  114.     switch channel(1).attenuation.pattern
  115.         case 'no'
  116.             txt_fading = ' - no fading';
  117.         otherwise
  118.             txt_fading = ' - Rayleigh fading';
  119.     end
  120.     add2statistic(3+SNR,BER,[signal.modulation_type,txt_fading]);
  121. end
  122. % % -----------------
  123. % % Graphs to compare
  124. SNR_linear = 10.^(SNR/10);
  125. %add2statistic(SNR+3,single_link_theo_ber,'BPSK - single link transmiss')
  126. %add2statistic(SNR+3,theo_ber,'QPSK - 2 senders')
  127. show_statistic;
  128. toc