MarouaneTheViper

Untitled

Apr 21st, 2020
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. % number of bits, Signal to Noise Ratio in dB then Watts
  2. numbits = 1e6;
  3. SNR = 0 : 2 : 30;
  4. snr = 10.^(SNR/10);
  5. % Random array of bits (0 or 1)
  6. rand_bits = randi([0 1], 1, numbits);
  7.  
  8. % case OOK
  9. % Initializing zero arrays to be manipulated later
  10. no_error1 = zeros(1, length(SNR));
  11. noise1 = zeros(size(rand_bits));
  12. BER1 = zeros(1, length(SNR));
  13. Rx_sequence1 = zeros(size(rand_bits));
  14. Tx_sequence1 = zeros(size(rand_bits));
  15.  
  16. % Calculating Power to be used in noise equation
  17. power1 = mean(abs(rand_bits.^2));
  18. % Adding noise, then deciding which bits are ones and which are zeroes.
  19. % Afterwards, the results are compared to the original array of bits
  20. % rand_bits and the number of error bits and their ratio are stored
  21. for i = 1 : length(snr)
  22. noise1 = (sqrt(power1/(snr(i)))) * (1/sqrt(2)) * (randn(1,length(rand_bits)) + 1j * randn(1, length(rand_bits)));
  23. Rx_sequence1 = rand_bits + noise1;
  24. Tx_sequence1 = real(Rx_sequence1) > 1/2;
  25. [no_error1(i), BER1(i)] = biterr(Tx_sequence1, rand_bits);
  26. end
  27. figure
  28. semilogy(SNR, BER1, 'ro-');
  29. title('Modulation Types (Coding) Comparison')
  30.  
  31. % case PRK %
  32. no_error2 = zeros(1, length(SNR));
  33. noise2 = zeros(size(rand_bits));
  34. BER2 = zeros(1, length(SNR));
  35. Rx_sequence2 = zeros(size(rand_bits));
  36. Tx_sequence2 = zeros(size(rand_bits));
  37.  
  38. % PRK formula that returns 1 if 1 is entered, -1 if 0 is entered
  39. PRK = 2 * rand_bits - 1;
  40. % Calculating Power to be used in noise equation
  41. power2 = mean(abs(PRK.^2));
  42. % Adding noise, then deciding which bits are ones and which are zeroes.
  43. % Afterwards, the results are compared to the original array of bits
  44. % rand_bits and the number of error bits and their ratio are stored
  45. for i = 1: length(snr)
  46. noise2 = (sqrt(power2/snr(i))) * (1/sqrt(2)) * (randn(1, length(PRK)) + 1j * randn(1, length(rand_bits))) ;
  47. Rx_sequence2 = PRK + noise2;
  48. Tx_sequence2 = real(Rx_sequence2) > 0;
  49. [no_error2(i), BER2(i)] = biterr(Tx_sequence2, rand_bits);
  50. end
  51. hold on
  52. semilogy(SNR, BER2, 'gx-')
  53.  
  54. % case FSK %
  55. no_error3 = zeros(1, length(SNR));
  56. noise3 = zeros(size(rand_bits));
  57. BER3 = zeros(1, length(SNR));
  58. Rx_sequence3 = zeros(size(rand_bits));
  59. Tx_sequence3 = zeros(size(rand_bits));
  60. fsk = zeros(size(rand_bits));
  61. % FSK formula: returns 1 if 0 is entered, 1i if 1 is entered
  62. for i = 1:length(fsk)
  63. if rand_bits(i) == 0
  64. fsk(i) = 1;
  65. else
  66. fsk(i) = 1i;
  67. end
  68. end
  69. % Calculating Power to be used in noise equation
  70. power3 = mean(abs(fsk.^2));
  71. % Adding noise, then deciding which bits are ones and which are zeroes.
  72. % Afterwards, the results are compared to the original array of bits
  73. % rand_bits and the number of error bits and their ratio are stored
  74. for i = 1: length(snr)
  75. noise3 = (sqrt(power3/snr(i))) * (1/sqrt(2)) * (randn(1, length(fsk)) + 1i * randn(1, length(fsk)));
  76. Rx_sequence3 = fsk + noise3;
  77. Tx_sequence3 = imag(Rx_sequence3) > real(Rx_sequence3);
  78. [no_error3(i), BER3(i)] = biterr(Tx_sequence3, rand_bits);
  79. end
  80.  
  81. hold on
  82. semilogy(SNR, BER3, 'b*-')
  83. legend('OOK','PRK','FSK');
  84. % The results of all Biterrors from all 3 modulation techniques are
  85. % compared on semilogy and it is deduced that PRK is the most efficient
Advertisement
Add Comment
Please, Sign In to add comment