function [ NCSE ] = Symbolic_Ent( X, L )
% [ SymEnt ] = Symbolic_Ent( X, L )
% symbolicEnt Calculates the Symbolic Entropy for given data.
% Input -   X: 1-Dimensional binary array of data
%           L: Word length
% Output -  NCSE: Normalized Corrected Shannon Entropy
% Remarks
% - This code calculates the Symbbolic Entropy value for the provided data
%   at a given word length described by - Aziz, W., Arif, M., 2006.
%   "Complexity analysis of stride interval time series by threshold
%   dependent symbolic entropy." Eur. J. Appl. Physiol. 98: 30-40.
% Jun 2017 - Created by William Denton, unonbcf@unomaha.edu
%% Begin code: (Do NOT Edit)
%% Correct orientation of array.
[r,c] = size(X);
if r > c
    X = X';
end
%% Convert binary values to decimal.
words = zeros(length(X)-L+1,1);
for i = 1:length(X)-L+1
    words(i,1) = bin2dec(num2str(X(i:i+L-1)));
end
%% Calculate probability.
max_words = 2^L;
for i = 1:max_words
    P(i) = sum(words == i-1)/length(words);
    H(i) = P(i)*log2(P(i));
end
H = -sum(H(~isnan(H)));
%% Normalized Corrected Shannon Entropy
So = length(unique(words));
Sm = max_words;
CSE = H+(So-1) / (2*Sm*log(2));
CSEm = -log2(1/Sm) + (Sm-1) / (2*Sm*log(2));
NCSE = CSE/CSEm;
%% Print out Symbolic Entropy Value.
fprintf('Normalized Corrected Shannon Entropy = %2.3f bits\r',NCSE);
end