Advertisement
makispaiktis

Main parameters - Hamming error probabilities for n=2,3,4

Jan 14th, 2022 (edited)
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.77 KB | None | 0 0
  1. %% Close all figures, clear workspace and console
  2. close all;
  3. clear;
  4. clc;
  5.  
  6. %% NICE COMBINATIONS
  7. % n = 2 and er = 0.3
  8. % n = 3 and er = 0.1
  9. % n = 4 and er = 0.05
  10.  
  11. %% Application - Hamming Code
  12. target = 500;
  13. n_List = [2, 3, 4];
  14. er_List = [0.3, 0.1, 0.05]
  15. percentage100_List = zeros(1, length(n_List));
  16. for i = 1:length(n_List)
  17.     n = n_List(i);
  18.     er = er_List(i);
  19.     percentage100 = HAMMING(n, er, target);
  20.     percentage100_List(i) = percentage100;
  21. end
  22. n_List
  23. er_List
  24. percentage100_List
  25.  
  26. subplot(1, 2, 1);
  27. plot(n_List, er_List);
  28. title("Each n has its own 'good' error rate");
  29. xlabel('n');
  30. ylabel('Error rate: er = er(n)');
  31.  
  32. subplot(1, 2, 2);
  33. plot(n_List, percentage100_List);
  34. title("Percentage of correction of a single bit");
  35. xlabel('n');
  36. ylabel('Percentage (%) of correction');
  37.  
  38.  
  39.  
  40. % Auxiliary Function
  41. function y = HAMMING(n, er, target)
  42.  
  43.     %% Application - Hamming Code
  44.     m = 2^n - n - 1;         % message bits || rows of G
  45.     H = ham_par(n);          % Creating a Hamming Code Parity Matrix
  46.     P = H(:, 1:(2^n-1-n));  
  47.     G = [eye(2^n-1-n) P'];   % Creating Generator Matrix
  48.  
  49.     % Creating messages words
  50.     u_list = dec2bin(0:2^m-1)-'0';
  51.  
  52.     % Creating codewords
  53.     codewords = ones(2^m,2^n-1);
  54.     rows_codewords = size(codewords, 1);
  55.     cols_codewords = size(codewords, 2);
  56.  
  57.     for i = 1:rows_codewords
  58.         codewords(i,:) = mod(u_list(i,:)*G,2); % EINAI MOD 2?
  59.     end
  60.  
  61.  
  62.  
  63.  
  64.  
  65.     %% Create the stats
  66.     counter_rounds = 0;
  67.     num_of_corrections = 0;
  68.  
  69.     while counter_rounds < target
  70.  
  71.         %% Simulating the Binary Symmetric Channel
  72.         in_data = codewords(randi([1 rows_codewords]), :); % Random codeword
  73.         % in_data = codewords(2,:);
  74.         x = zeros(1, cols_codewords); % x(0,1) to x(-1,0)
  75.  
  76.         for i = 1:cols_codewords    
  77.             if in_data(i) == 0
  78.                 x(i) = 1;
  79.             else
  80.                 x(i) = -1;
  81.             end  
  82.         end
  83.  
  84.         [out_data,err] = bsc(in_data,er);              
  85.         initial_errors = sum(err);
  86.  
  87.  
  88.  
  89.  
  90.         if initial_errors == 1
  91.  
  92.             counter_rounds = counter_rounds + 1;
  93.             disp("******** Counter " + num2str(counter_rounds) + " ********");
  94.             %% Begin the process
  95.             y = zeros(1, cols_codewords);     % out_data(0,1) to out_data(1,-1)
  96.             for i = 1:length(out_data)  
  97.                 if out_data(i) == 0
  98.                     y(i) = 1;
  99.                 else
  100.                     y(i) = -1;
  101.                 end    
  102.             end
  103.  
  104.             %% Print the state
  105.             disp("x = " + num2str(x));
  106.             disp("y = " + num2str(y));
  107.             disp("Initial errors: "+num2str(initial_errors));
  108.  
  109.             %% Implementing the Graph
  110.  
  111.             pos = lk(x, y, er);   % old chan_node
  112.             var_nodes = cell(1, 2^n-1);
  113.             for i = 1:2^n-1
  114.  
  115.                var_nodes{i} = connections(H,i,pos);
  116.  
  117.             end
  118.             check_nodes = cell(1,n);
  119.  
  120.             for i = 1:n
  121.                check_nodes{i} = var2check(H,i,pos,0);
  122.             end
  123.  
  124.             % Iterations
  125.             limit = 100;
  126.             counter = 0;
  127.             SUM = -1;
  128.             while (counter <= limit) && SUM ~= 0
  129.  
  130.                 counter = counter + 1;
  131.  
  132.                 for i = 1:n
  133.                     check_nodes =...
  134.                     var2check_updated(var_nodes,check_nodes,pos,i);
  135.                 end
  136.  
  137.                 check_to_var = check2var(check_nodes, n);
  138.                 var_rec_sum = var_rec(check_to_var, pos);
  139.                 var_values = map_detection(var_rec_sum);
  140.                 % x^ = 1 - 2x => x = (1 - x^) / 2
  141.                 % x = x, x^ = var_values
  142.                 var_values_01 = (1 - var_values) / 2;
  143.                 temp = mod(H*var_values_01',2);
  144.                 SUM = sum(temp);
  145.                 var_nodes =...
  146.                 edges_values(var_nodes, check_to_var);
  147.  
  148.             end
  149.  
  150.             disp(' ')
  151.             %disp("x^= " + num2str(var_values));
  152.             %disp("x = " + num2str(x))
  153.             disp("x = " + num2str(in_data));
  154.             disp("x^= " + num2str(var_values_01));
  155.             final_errors = sum(abs(x-var_values))/2;
  156.             disp("Final errors: " + num2str(final_errors));
  157.             display("**********************")
  158.             display(' ')
  159.             if final_errors == 0
  160.                 num_of_corrections = num_of_corrections + 1;
  161.             end
  162.  
  163.         end
  164.  
  165.     end
  166.  
  167.  
  168.     %% Display overall stats
  169.     disp("Iterations: " + num2str(target));
  170.     disp("Corrected codewords with 1 initial mistake: " + num2str(num_of_corrections))
  171.     percentage100 = 100 * num_of_corrections / target;
  172.     disp("Percentage of correction: " + num2str(percentage100) + "%");
  173.     y = percentage100;
  174.    
  175. end
  176.  
  177.  
  178.  
  179.  
  180.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement