%EUSIPCO 2012
%Imperial College London 
%Andriy Gelman, Jon Onativia and Pier Luigi Dragotti 

function hcode = encode_variational_direction(sig)   
    
    if ~isempty(sig) > 0
    %Huffman variational direction
    %symbols = [0 45 -45 90 -90 135 -135 180];
    %p = [0.453 0.244 0.244 0.022 0.022 0.006 0.006 0.003];
    %Construct Dictionary table
    dict = cell(8,2);
    dict{1,1} = 0;      dict{1,2} = [0];
    dict{2,1} = 45;     dict{2,2} = [1 0];
    dict{3,1} = -45;    dict{3,2} = [1 1 0];
    dict{4,1} = 90;     dict{4,2} = [1 1 1 0];
    dict{5,1} = -90;    dict{5,2} = [1 1 1 1 0];
    dict{6,1}= 135;     dict{6,2} = [1 1 1 1 1 0];
    dict{7,1} = -135;   dict{7,2} = [1 1 1 1 1 1 0];
    dict{8,1} = 180;    dict{8,2} = [1 1 1 1 1 1 1];

    sig(sig == -180) = 180;
    %encode the variational direction using the huffman code
    hcode =huffmanenco(sig, dict);
    else
        hcode = [];
    end
end
