Advertisement
makispaiktis

Main stats (Hamming correction for n=4)

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