Bassel_11

Untitled

May 11th, 2023
1,344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.18 KB | None | 0 0
  1. action = input('Do you want to encode or decode a message? ', 's');
  2.  
  3. if strcmp(action, 'encode')
  4.     % Encoding code
  5.     p = input('Enter the parity check matrix: ');
  6.     k = input('Enter the message length: ');
  7.     n = input('Enter the codword length: ');
  8.     g = [p eye(k)];
  9.     u = input('Enter the message: ');
  10.     disp('The encoded message:');
  11.     v = encode(u, n, k, 'linear' ,g)';
  12.     disp(v);
  13. elseif strcmp(action, 'decode')
  14.     % Decoding code
  15.     g = input('Enter the generator matrix: ');
  16.     [k, n] = size(g);
  17.     x = n - k;
  18.     h = gen2par(g);
  19.     v = input('Enter the received message: ');
  20.     ht = h';
  21.     sv = v * ht;
  22.     s = mod(sv, 2);
  23.     if sum(s) == 0
  24.         disp('No error');
  25.         disp('The decoded message:');
  26.         u = v(x+1:n);
  27.         disp(u);
  28.     else
  29.         for i = 1:size(ht, 1)
  30.             if ht(i,1:x) == s
  31.                 v(i) = 1 - v(i);
  32.                 break;
  33.             end
  34.         end
  35.         disp('The error is in bit:');
  36.         disp(i);
  37.         disp('The corrected code vector:');
  38.         disp(v);
  39.         disp('The decoded message:');
  40.         u = v(x+1:n);
  41.         disp(u);
  42.     end
  43. else
  44.     disp('Invalid input');
  45. end
Advertisement
Add Comment
Please, Sign In to add comment