Contents

Elias codes of the fiirst 2^n integers

n= 5;

Find the binary represenations of the fiirst 2^n integers (each row in B will be a binary representation of an integer

B = [0;1];
nB = 2;
for i = 2:n
    B = [ zeros(nB,1) B; ones(nB,1) B];
    nB= nB*2;
end
B
B =

     0     0     0     0     0
     0     0     0     0     1
     0     0     0     1     0
     0     0     0     1     1
     0     0     1     0     0
     0     0     1     0     1
     0     0     1     1     0
     0     0     1     1     1
     0     1     0     0     0
     0     1     0     0     1
     0     1     0     1     0
     0     1     0     1     1
     0     1     1     0     0
     0     1     1     0     1
     0     1     1     1     0
     0     1     1     1     1
     1     0     0     0     0
     1     0     0     0     1
     1     0     0     1     0
     1     0     0     1     1
     1     0     1     0     0
     1     0     1     0     1
     1     0     1     1     0
     1     0     1     1     1
     1     1     0     0     0
     1     1     0     0     1
     1     1     0     1     0
     1     1     0     1     1
     1     1     1     0     0
     1     1     1     0     1
     1     1     1     1     0
     1     1     1     1     1

Find the Elias codewords for all integers between 1 and (2^n-1)

Elias = [];
for ii = 1:(size(B,1)-1)
    integerii = ii;
    % take the binary representation of integer "ii"
    integeri = B(ii+1,:);

Find the most significant (leading) one in the binary representation:

    in1 = find(integeri==1,1,'first');
    integeri(1:in1)=[];
    % The bits in the vector "integeri" will be appended to the last part
    % of the Elias codeword. The first part of the codeword describes "N",
    % which says how many bits we have in the binary vector "integeri"
    N = length(integeri);
    % Find the binary representation of "N"
    binaryN = B(N+1,:);
    in1 = find(binaryN==1,1,'first');
    binaryN(1:(in1-1))=[];
    % Now the binary vector "binaryN" gets transmitted, by using the
    % repetition coding
    if (integerii >= 4)
        % always the Elias codewords starts with 1, for integerii >= 4
        eliascodeword = 1;
        for ij = 2:(length(binaryN)-1)
            % now repeat twice each bit from binaryN, until next to last position
            eliascodeword = [eliascodeword binaryN(ij) binaryN(ij)];
        end
        if( binaryN(end) == 0)
            % the last bit is encoded by 01 or 10, depending on the bit
            % being 0 or 1
            eliascodeword = [eliascodeword 0 1];
        else
            eliascodeword = [eliascodeword 1 0];
        end
        % finally append the bits from the binary vector "integeri"
        eliascodeword = [eliascodeword integeri];
    else
        switch integerii
            case 1
                eliascodeword = [ 0 0];
            case 2
                eliascodeword = [ 0 1 0] ;
            otherwise
                eliascodeword = [ 0 1 1] ;
        end
    end
    Elias = [Elias length(eliascodeword)];
    if( integerii < 16 )
        Pair_Integer_Codeword = [ integerii eliascodeword ]
    end
Pair_Integer_Codeword =

     1     0     0

Pair_Integer_Codeword =

     2     0     1     0

Pair_Integer_Codeword =

     3     0     1     1

Pair_Integer_Codeword =

     4     1     0     1     0     0

Pair_Integer_Codeword =

     5     1     0     1     0     1

Pair_Integer_Codeword =

     6     1     0     1     1     0

Pair_Integer_Codeword =

     7     1     0     1     1     1

Pair_Integer_Codeword =

     8     1     1     0     0     0     0

Pair_Integer_Codeword =

     9     1     1     0     0     0     1

Pair_Integer_Codeword =

    10     1     1     0     0     1     0

Pair_Integer_Codeword =

    11     1     1     0     0     1     1

Pair_Integer_Codeword =

    12     1     1     0     1     0     0

Pair_Integer_Codeword =

    13     1     1     0     1     0     1

Pair_Integer_Codeword =

    14     1     1     0     1     1     0

Pair_Integer_Codeword =

    15     1     1     0     1     1     1

end

Build the table with the codelength of codewords for Elias code

Elias
hh = hist(Elias,1:24);
[(1:24)' hh']
Elias =

  Columns 1 through 13

     2     3     3     5     5     5     5     6     6     6     6     6     6

  Columns 14 through 26

     6     6     9     9     9     9     9     9     9     9     9     9     9

  Columns 27 through 31

     9     9     9     9     9


ans =

     1     0
     2     1
     3     2
     4     0
     5     4
     6     8
     7     0
     8     0
     9    16
    10     0
    11     0
    12     0
    13     0
    14     0
    15     0
    16     0
    17     0
    18     0
    19     0
    20     0
    21     0
    22     0
    23     0
    24     0