%% A MATLAB program - run by typing its name at the prompts: %% example>> arith %% %% NOTE: Requires 'Communications Toolbox' MATLAB add-on %% to run arithenco/arithdeco. %================================================================ % % Arithmetically encode a random message % %================================================================ % Generate a random message of length 1000 with alphabet consisting of % digits 1-5. msg = ceil(rand(1000, 1) .* 5); % Compute the counts of each symbol counts = histc(msg, unique(msg)); % Generate arithmetic code of the message code = arithenco(msg, counts); % Compute the number of bits per symbol bits_per_symbol = length(code) / length(msg) % Compute entropy prob = counts ./ sum(counts); msg_entropy = sum(-prob .* log2(prob)) %================================================================ % % Decode the message % %================================================================ msg2 = arithdeco(code, counts, length(msg)); % Now messages msg and msg2 should be identical. % Compare the two messages. If equal, prints 1. all(msg==msg2)