Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.04 KB | None | 0 0
  1. %recovers message using iterative (graph-peeling) erasure decoding
  2. %recieved_codeword - result of sending message through bec, bits erased marked with -1
  3. function recovered_codeword = recover_lost_bits(recieved_codeword, parity_matrix)
  4.     recovered_codeword = recieved_codeword;
  5.     valid_checks = boolean(zeros(size(recieved_codeword)));
  6.     can_recover = true; %if number of valid checks is not growing it means we have too many erased bits
  7.     while ~is_fully_recovered(recovered_codeword) && can_recover
  8.         [new_valid_checks, recovered_codeword] = recover_all_possible_bits(recovered_codeword, parity_matrix, valid_checks);
  9.         can_recover = can_recover_more_bits(valid_checks, new_valid_checks);
  10.  
  11.         valid_checks = new_valid_checks;
  12.     end
  13.  
  14.     if ~is_fully_recovered(recovered_codeword) && ~can_recover
  15.         disp('RECOVERY FAILED, TOO MANY ERASED BITS!');
  16.     end
  17. end
  18.  
  19. %all bits are restored
  20. function is_fully_recovered = is_fully_recovered(codeword)
  21.     is_fully_recovered = all(codeword >= 0);
  22. end
  23.  
  24. %if number of valid checks is not growing it means we have too many erased bits
  25. function can_recover = can_recover_more_bits(valid_checks, new_valid_checks)
  26.     can_recover = sum(new_valid_checks - valid_checks) > 0;
  27. end
  28.  
  29. %one iteration over parity_check matrix
  30. function [new_valid_checks, recovered_codeword] = recover_all_possible_bits(codeword, parity_matrix, valid_checks)
  31.     new_valid_checks = valid_checks;
  32.     recovered_codeword = codeword;
  33.     for parity_check = 1 : size(parity_matrix, 1)
  34.         if(~valid_checks(parity_check)) % don't jump in if parity check satisfied
  35.             [recovered_codeword, is_check_valid]  = try_recovering_bits_from_parity_check_row(codeword, parity_matrix(parity_check,:));
  36.             if is_check_valid
  37.                 new_valid_checks(parity_check) = true;
  38.             end
  39.         end
  40.         codeword = recovered_codeword;
  41.     end
  42. end
  43.  
  44. function [recovered_codeword, is_check_valid] = try_recovering_bits_from_parity_check_row(codeword, parity_check)
  45.     is_check_valid = true;
  46.     recovered_codeword = codeword;
  47.     bit_positions = find(parity_check);
  48.     bit_values = codeword(bit_positions);
  49.  
  50.     if ~is_fully_recovered(bit_values)  %all ok, do nothing
  51.         [recovered_codeword, is_check_valid] = try_recovering_bit(bit_values, bit_positions, codeword);
  52.     end
  53. end
  54.  
  55. function [recovered_codeword, is_check_valid] = try_recovering_bit(bit_values, bit_positions, codeword)
  56.     is_check_valid = true;
  57.     recovered_codeword = codeword;
  58.  
  59.     unknown_positions = find(bit_values == -1);
  60.     number_of_unknowns = numel(unknown_positions);
  61.  
  62.     if number_of_unknowns == 1 % we can fix one unknown!
  63.         known_values = bit_values(bit_values > -1);
  64.         position_of_bit_to_recover = bit_positions(unknown_positions(1));
  65.         recovered_codeword(position_of_bit_to_recover) = mod(sum(known_values), 2); %to satisfy parity check sum of all elems must be 0
  66.     else
  67.         is_check_valid = false; %check not valid, we need to wait for next iteration
  68.     end    
  69. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement