octavehuffman-codeoctave-gui

Octave - Huffman code doesn't work - All elements of SIG must be integers in the range [1,N]


I have a problem in Octave using huffmandict and huffmanenco.

Here is my error :

error: huffmanenco: all elements of SIG must be integers in the range [1,N]

Here is my code :

inputSig = [1 1 2 6 6 6 6 4 5 5];
list_symb = [1 2 6 4 5];
list_proba = [0.2, 0.1, 0.4, 0.1, 0.2];
dict = huffmandict(list_symb,list_proba);
code = huffmanenco(inputSig,dict);

my dict is

dict =
{
 [1,1] =  1
 [1,2] = 0   1
 [1,3] = 0   0   1
 [1,4] = 0   0   0   0
 [1,5] = 0   0   0   1
}

So my error is with the line

code = huffmanenco(inputSig,dict);

because the lenght of my dict is 5 and my lenght of my inputSig is 10.

How can I do my huffman coding without this error?

However, this code seems to work on Matlab.


Solution

  • You say

    because the length of my dict is 5 and the length of my inputSig is 10.

    That's not quite why you get this error. From the documentation:

    A restriction is that a signal set must strictly belong in the range '[1,N]' with 'N = length (dict)'.

    In other words, your 'dict' only contains 5 cells, but your 'inputSig' contains integers in the range [1,6] instead of [1,5].

    Therefore, you basically have to 'recode' / map your signal in the range [1,5] (i.e. the range [1,5] will become indices/labels, to an array of your actual symbols).

    E.g.

    inputSig   = [1 1 2 6 6 6 6 4 5 5];
    list_symb  = unique( inputSig );
    list_proba = [0.2, 0.1, 0.4, 0.1, 0.2];
    dict       = huffmandict( list_symb, list_proba );
    [~, idx]   = ismember( inputSig, list_symb );
    code       = huffmanenco( idx, dict )
    % code = 0 1 0 1 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 1 0 0 0 0 0 0 0 0
    

    PS. For completion, the obvious next question is how do you decode this, given the whole 'actual symbol vs indices' business. Simple; you use the decoded output (which corresponds to indices) and use it as an index vector to the list_symb vector, thus retrieving the original symbols. I.e.:

    deco = huffmandeco ( code, dict )
    % deco = 1 1 2 5 5 5 5 3 4 4
    
    list_symb( deco )
    % ans  = 1 1 2 6 6 6 6 4 5 5