Advertisement
makispaiktis

Repetition Code plots - Errors and average error

Jan 14th, 2022 (edited)
1,634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.31 KB | None | 0 0
  1. %% Close all figures, clear workspace and console
  2. close all;
  3. clear;
  4. clc;
  5.  
  6.  
  7. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. %% Application - Repetition Code
  10. %% Create the data
  11. counter_rounds = 0;
  12. target = 100;
  13. num_of_corrections = 0;
  14. er = 0.1;
  15. repeat = 3;
  16. LEN = 20;
  17. %% Create the stats
  18. rounds_list = 1:target;
  19. xy_mistakes_list = zeros(1, target);
  20. message_mistakes_list = zeros(1, target);
  21. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  22. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23.  
  24.  
  25.  
  26.  
  27. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  28. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  29. %% MAIN FUNCTION - PLOTS
  30. while counter_rounds < target
  31.    
  32.     counter_rounds = counter_rounds + 1;
  33.     disp("******** Counter " + num2str(counter_rounds) + " ********");
  34.    
  35.     %% Simulating the Repetition
  36.     message = randi([0 1], 1, LEN);
  37.     x = zeros(1, repeat * LEN);
  38.     for i = 1:LEN
  39.         for j = 1:repeat
  40.             x(j + (i - 1)*repeat) = message(i);
  41.         end
  42.     end
  43.     % Example
  44.     % message = [1, 0, 1]
  45.     % x = [1,1,1, 0,0,0, 1,1,1]
  46.    
  47.     [y, err] = bsc(x,er);
  48.     message_guess = zeros(1, LEN);
  49.     % This is not a Hamming code, so it can correct many errors
  50.     for i = 1:LEN
  51.         bitstream = y((i-1)*repeat + 1 : i*repeat);
  52.         SUM = sum(bitstream);
  53.         if SUM > repeat / 2
  54.             bit_guess = 1;
  55.         elseif SUM < repeat / 2
  56.             bit_guess = 0  ;  
  57.         else
  58.             bit_guess = randi([0 1]);
  59.         end
  60.         message_guess(i) = bit_guess;
  61.     end
  62.     x;
  63.     y;
  64.     message_guess;
  65.     % Mistakes - Display and lists
  66.     xy_mistakes = sum(abs(x - y));
  67.     message_mistakes = sum(abs(message - message_guess));
  68.     disp("x-y mistakes:    " + num2str(xy_mistakes));
  69.     disp("message mistakes:" + num2str(message_mistakes));
  70.     display(' ');
  71.     xy_mistakes_list(counter_rounds) = xy_mistakes;
  72.     message_mistakes_list(counter_rounds) = message_mistakes;
  73. end
  74. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  75. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  76.  
  77.  
  78.  
  79. %% Display overall stats
  80. xy_average = sum(xy_mistakes_list) / length(xy_mistakes_list);
  81. message_average = sum(message_mistakes_list) / length(message_mistakes_list);
  82. xy_average_list = xy_average * ones(1, target);
  83. message_average_list = message_average * ones(1, target);
  84.  
  85. disp("Average mistakes in x-y (codewords):  " + num2str(xy_average));
  86. disp("Average mistakes in original message: " + num2str(message_average));
  87.  
  88. subplot(1,2,1);
  89. plot(rounds_list, xy_mistakes_list, 'red');
  90. hold on
  91. plot(rounds_list, message_mistakes_list, 'green');
  92. title("Errors with: er = " + num2str(er));
  93. xlabel('# of channel simulation');
  94. ylabel('# of errors');
  95. legend('Errors in codeword sent', 'Errors in message sent');
  96.  
  97. subplot(1,2,2);
  98. plot(rounds_list, xy_average_list, 'red');
  99. hold on
  100. plot(rounds_list, message_average_list, 'green');
  101. title("Average error with: er = " + num2str(er));
  102. xlabel('# of channel simulation');
  103. ylabel('Average # of errors');
  104. legend('Average error in codewords', 'Average error in messages');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement