clear
clc

load('midsummer.mat')
%% Step 1 Turn the character array x into a vector of doubles via ASCII.
y = double(x); 
% Show the histogram of the data y, which can visualize the frequency of each symbol of ASCII.
hist(y,0:255)  % The range of values in the ASCII is 0 to 255,so we use the value range 0:255. 

%% Step 2 Construct a probabilistic model
% Use a vector to record the frequency of each symbol of ASCII in the picture we draw.
frequency = hist(y,0:255); 
% Calculate the probability of every symbol.
prob = frequency/length(y);

%% Step 3 Calculate the information entropy
% Not all the symbols of ASCII appear in this English text, so we use p>0 to find out all possible values of the random variable. 
% Here, the symbol of the text can be regarded as the random variable.
entropy = sum(-prob(prob>0) .* log2(prob(prob>0))); 

%% Now, we want to only pay attention to letters and ignore formatting characters and punctuation. 
% Filter out the letters in the English text via the value range of ASCII.
letters = y((y>=97) & (y<=122));
% Construct a probabilistic model.
freq_letters = hist(letters, 97:122);
prob_letters = freq_letters/length(letters);
% Also use prob_letter>0 to find out all possible values of the random variable.
entropy_letters = sum(-prob_letters(prob_letters>0) .* log2(prob_letters(prob_letters>0)));
