Advertisement
makispaiktis

Repetition Code - Different repeat factor

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