Uniquely Decodable Code using MATLAB
Last Updated :
02 Sep, 2020
A code is distinct if each codeword is recognizable from every other (i.e., the planning from source messages to codewords is coordinated). A distinct code is extraordinarily decodable if each codeword is recognizable when drenched in a grouping of codewords or if the first source arrangement can be remade consummately from the encoded binary sequence.
Test for Unique Decodability:
- Consider two codewords A and B. A is of k bits and B is of n bits(k<n), if the first k bits of B are identical to A, then A is called a prefix of B, the remaining last n-k bits are called as the dangling suffix. Example: A = 010, B = 01001, the dangling suffix is 01.
- Construct a list of all the code words, examine all pair of code words to see if any codeword is a prefix of another, whenever such a pair is present add a dangling suffix to the list unless you have added the same dangling suffix to the list in a previous iteration.
- Keep repeating the procedure for the extended list until all codewords are compared.
Outcomes:
- You get a dangling suffix which is a code word.
- There is no dangling suffix present.
- The dangling suffix is not a code word.
- If you get point 5 as an outcome the code is not uniquely decodable.
- In all other cases, it is uniquely decodable.
Examples:
'0, 01, 011, 0111' is an Uniquely decodable code
'0, 1, 00, 11' is not an uniquely decodable code
Code:
code = { '0' , '01' , '011' , '0111' };
disp(code)
i = 1;
flag1 = 0;
temp = code;
ds = {};
while i < length(temp)
j = 1;
while j < length(temp)
c1 = temp{i};
c2 = temp{j};
l1 = length(c1);
l2 = length(c2);
if l1 < l2
if c1 == c2(1 : l1)
tsuffix = c2(l1 + 1 : l2);
l = length(ds);
if l == 0
ds = {tsuffix};
else
k = 1;
while k <= l
flag = strcmp(tsuffix, ds{k});
if flag ~= 1;
k = k + 1;
end
end
if flag == 0
ds = [ds {tsuffix}];
l = length(ds);
end
end
lt = length(temp);
k = 1;
while k <= lt
flag = strcmp(tsuffix, temp{k});
if flag == 1
flag1 = 1;
disp( 'Code is not Uniquely decodable' );
break ;
else
k = k + 1;
end
end
if flag == 0
temp = {temp(:) tsuffix};
lt = length(temp);
end
end
end
if flag1 == 1
break ;
end
j = j + 1;
end
if flag1 == 1
break ;
end
i = i + 1;
end
if flag1 == 0
disp( 'Code is Uniquely Decodable' );
end
|
Output:
'0' '01' '011' '0111'
Code is Uniquely Decodable
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...