% MATLAB Starter Code for Arithmetic Coding
% 
% You should additionally write and submit the files:
%   arithmeticEncode.m
%   arithmeticDecode.m
% 

rand('seed', 0); % for reproducibility

% Define the source distribution
numSymbols = 5;
p = [0.01; 0.1 + rand(numSymbols - 1, 1)];
p = p/sum(p);

% Short (length-6) Example
input = [randi([2 numSymbols], 6, 1); 1];
code = arithmeticEncode(p, input);
decodeStr = arithmeticDecode3(p, code);
disp(code)
disp(input)
disp(decodeStr)

% Longer (length-30) Example
input = [randi([2 numSymbols], 30, 1); 1];
code = arithmeticEncode(p, input); % Code
decodeStr = arithmeticDecode(p, code);
% display a small subsequence, for verification
disp(code(10:15))
[str1(10:15), decodeStr(10:15)]

% check that arithmeticDecode(p, arithmeticDecode(p, input)) == input
disp(['Input and decoded output are equal: ' all(decodeStr == str1)])
