This_one.m
上传用户:sdyljs
上传日期:2019-09-30
资源大小:1k
文件大小:3k
源码类别:

通讯/手机编程

开发平台:

Matlab

  1.  
  2. %%%%%%%%%%%%%%%%%%%%%%%%%METHOD%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3.  
  4. %(a) Generate random binary sequence of +1抯 and -1抯.
  5. %(b) Given different Power to the source and the relay as well
  6. %(c) Multiply the symbols with the channel and then add white Gaussian noise.
  7. %(d) At any receiver, equalize (divide) the received symbols with the known channel
  8. %(e) Perform hard-decision-decoding and count the bit errors
  9. %(f) Plot the power given to both source(Ps) and relay(Pr) with both SNR and BER
  10. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
  11.                %Total Power available PT=Ps+Pr=100dB
  12. clear
  13. N = 100; % bit stream
  14. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%SOURCE_NODE%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  15. ip = rand(1,N)>0.5; % generating 0,1 with equal probability
  16. s = 2*ip-1; % BPSK modulation 0 -> -1; 1 -> 0 
  17. Ps=linspace(1,100,N);%Power given to the source(in dB)
  18. for j=1:length(Ps)
  19.     
  20. %%%%%%%%%%%%%%%%%%%%%%%%%%%%DIFINING_CHANNEL_AND_NOISE%%%%%%%%%%%%%%%%%%%%%
  21.     
  22.    n = 1/sqrt(2)*(randn(1,N) + 1i*randn(1,N)); % white gaussian noise, 0dB variance 
  23.    h = 1/sqrt(2)*(randn(1,N) + 1i*randn(1,N)); % Rayleigh channel
  24.    ys=sqrt(Ps(j)).*h.*+ n;%Signal and noise added together(broadcasted in the first time slot)
  25.    SNR1=(Ps(j).*h.^2)./n;%SNR_Souce-Relay,Souce-destination-link
  26.    
  27. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%RELAY_NODE%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  28.                       %Pr=100-Ps Power given to the relay
  29. % equalization
  30.    yHat = ys./sqrt(Ps(j)).*h;
  31. % hard-decision-decoding
  32.    ipHat = real(yHat)>0;% signal at the relay after decoding
  33. s1=2*ipHat-1;%BPSK modulation(before sending in the second time slot)
  34. yr= sqrt(100-Ps(j)).*h.*s1+ n; %signal broadcasted by the relay in the second time slot
  35. SNR2=((100-Ps(j)).*h.^2)./n;%SNR_Relay-Destination-link
  36. %%%%%%%%%%%%%%%%%%%%%%%%%DESTINATION_NODE%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  37. yd=ys+yr;
  38. SNR3=max(SNR1,SNR2);% Selection_combining(SC)
  39. SNR_RC=SNR1+SNR2;%Ratio_combining
  40. % equalization 
  41.    yHat_dest =ys./sqrt(Ps(j)).*h+yr./sqrt(80-Ps(j)).*h;
  42. % hard-decision-decoding
  43. ipHat_dest=real(yHat_dest)>0;
  44. % counting the errors
  45.    nErr(j) = size(find((ip- ipHat_dest)),2);
  46. end
  47. BER=real(nErr/N);%bit error rate
  48. %plotting
  49. close all
  50. figure
  51. subplot(2,1,1)
  52. plot(Ps,SNR3,'r')
  53. xlabel('Ps,Pr')
  54. ylabel('SNR')
  55. hold on
  56. plot(100-Ps,SNR3,'g')
  57. legend('Source-Power','Relay-Power')
  58. grid on
  59. subplot(2,1,2)
  60. plot(Ps,BER,'r')
  61. hold on
  62. plot(100-Ps,BER,'g')
  63. xlabel('Ps,Pr')
  64. ylabel('BER')
  65. grid on
  66. legend('Source-Power','Relay-Power')
  67. %%%%%%%%%%%%%%%%%%%%%%%RATION_COMBINING%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  68. %plotting
  69. % close all
  70. % figure
  71. % subplot(2,1,1)
  72. % plot(Ps,SNR_RC,'r--')
  73. % xlabel('Ps,Pr')
  74. % ylabel('SNR-RC')
  75. % hold on
  76. % plot(100-Ps,SNR_RC,'g--')
  77. % legend('Source-Power;SNR-RC','Relay-Power;SNR-RC')
  78. % grid on
  79. % subplot(2,1,2)
  80. % plot(Ps,SNR3,'r')
  81. % hold on
  82. % plot(100-Ps,SNR3,'g')
  83. % xlabel('Ps,Pr')
  84. % ylabel('SNR3')
  85. % grid on
  86. % legend('Source-Power;SNR3','Relay-Power;SNR3')