Advertisement
makispaiktis

Repetition Code stats for 1-run

Jan 14th, 2022 (edited)
1,392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.42 KB | None | 0 0
  1. %% Close all figures, clear workspace and console
  2. close all;
  3. clear;
  4. clc;
  5.  
  6.  
  7.  
  8. %% Application - Repetition Code
  9. %% Create the stats
  10. counter_rounds = 0;
  11. target = 1;
  12. num_of_corrections = 0;
  13.  
  14. while counter_rounds < target
  15.    
  16.     counter_rounds = counter_rounds + 1;
  17.     disp("Counter " + num2str(counter_rounds));
  18.    
  19.     %% Simulating the Repetition
  20.     er = 0.1;
  21.     repeat = 3;
  22.     LEN = 10;
  23.     message = randi([0 1], 1, LEN)
  24.     x = zeros(1, repeat * LEN);
  25.     for i = 1:LEN
  26.         for j = 1:repeat
  27.             x(j + (i - 1)*repeat) = message(i);
  28.         end
  29.     end
  30.     % Example
  31.     % message = [1, 0, 1]
  32.     % x = [1,1,1, 0,0,0, 1,1,1]
  33.    
  34.     [y, err] = bsc(x,er);
  35.     message_guess = zeros(1, LEN);
  36.     % This is not a Hamming code, so it can correct many errors
  37.     for i = 1:LEN
  38.         bitstream = y((i-1)*repeat + 1 : i*repeat);
  39.         SUM = sum(bitstream);
  40.         if SUM > repeat / 2
  41.             bit_guess = 1;
  42.         elseif SUM < repeat / 2
  43.             bit_guess = 0  ;  
  44.         else
  45.             bit_guess = randi([0 1]);
  46.         end
  47.         message_guess(i) = bit_guess;
  48.     end
  49.     x
  50.     y
  51.     message_guess
  52.     % Mistakes
  53.     xy_mistakes = sum(abs(x - y));
  54.     message_mistakes = sum(abs(message - message_guess));
  55.     disp("x-y mistakes:    " + num2str(xy_mistakes));
  56.     disp("message mistakes:" + num2str(message_mistakes));
  57.    
  58. end
  59.  
  60.  
  61. %% Display overall stats
  62.  
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement