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:

1. 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.
2. 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.
3. 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:

 `% Given Code ` `code = {``'0'``,``'01'``,``'011'``,``'0111'``};  ` `disp(code)  ` ` `  `i = 1; ` `flag1 = 0; ` ` `  `% Storing the code in temp variable  ` `temp = code;   ` ` `  `% Create datastore ` `ds = {};   ` ` `  `while` `i < length(temp) ` `  ``j = 1; ` `  ``while` `j < length(temp) ` `    ``c1 = temp{i}; ` `    ``c2 = temp{j}; ` `    ``l1 = length(c1); ` `    ``l2 = length(c2);          ` ` `  `    ``% To find dangling suffix ` `    ``if` `l1 < l2 ` `      ``if` `c1 == c2(1 : l1) ` `         `  `        ``% Extract dangling suffix after comparing codewords ` `        ``tsuffix = c2(l1 + 1 : l2); ` `        ``l = length(ds); ` `         `  `        ``% Append tsuffix to ds ` `        ``if` `l == 0 ` `           `  `          ``% If no dangling suffix available then tsuffix  ` `          ``% is new dangling suffix   ` `          ``ds = {tsuffix};  ` `        ``else` `          ``k = 1; ` `          ``while` `k <= l ` `             `  `            ``% Compare new tsuffix with 'k'th dangling suffix ` `            ``flag = strcmp(tsuffix, ds{k}); ` `            ``if` `flag ~= 1;  ` `              ``k = k + 1; ` `            ``end` `          ``end`           `          ``if` `flag == 0 ` `           `  `            ``% Attach tsuffix to dangling suffix array ` `            ``ds = [ds {tsuffix}]; ` `            ``l = length(ds); ` `          ``end` `        ``end`      `       `  `        ``% Append tsuffix to code ` `        ``lt = length(temp); ` `        ``k = 1; ` `        ``while` `k <= lt ` `         `  `          ``% Compare each extracted dangling suffix  ` `          ``% with 'k'th original codeword ` `          ``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 ` `         `  `          ``% Attach extracted dangling suffix array to codeword array ` `          ``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```