%% Define data filenames
data_file = ['..' filesep 'Data' filesep 'coriolan.xml'];
%data_file = ['..' filesep 'Data' filesep 'mary.txt'];

%% Define compressed data filename
compressed_file = 'compressed.mat';

%% Read data from the data file
fid = fopen(data_file,'r');
msg = fread(fid, 'uchar');
fclose(fid);

%% Determine the alphabet
% Note: As per documentation:
%    alphabet(n) = msg 
%    msg(m) = alphabet
%
[alphabet, m, n] = unique(msg);
counts = histc(n, 1:length(alphabet));

%% Plot a bar graph of the counts
bar(counts);

%% Find entropy
prob = counts ./ sum(counts);
ent = sum(-log2(prob).*prob);

%% Estimate compression ratio
compression_ratio = 8/ent;
disp(['Estimated compression ratio: ', num2str(compression_ratio)]);


%% Encode arithmetically
%% alphabet(n) = msg, so we can encode n instead
code = arithenco(n, counts);

%% Pack code data into words
% Since code consists of bits, they get
% packed into 32-bit words
%
% Note: bwpack is in the image toolkit and
% it operates on B/W images.
%
packed_code = bwpack(code);

%% Save compressed data to a file
% As per documentation: -v6 disables compression
% We disabled compression using 'Preferences'
save(compressed_file, 'alphabet', 'counts', 'packed_code');
