Advertisement
makispaiktis

Main stats and plots (Hamming probabilities plot for n=3)

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