function [H,MSC, LS]=PLSEc(z,n,tau)
% PLSEc(z,n,tau) computes the permutation largest slope entropy H
% for determining wether a dynamics is regular or not. H=0 indicates a
% regular dynamics (periodic, quasi-periodic,...) and H>0 a non-regular one (chaotic, stochastic). 
% Inputs:
            % z: time series data to be characterized
            % n: embedding dimension
            % tau: delay time of samples (tau=1 for efficient period detection)
% Outputs
            % MSC: most significant cycle or period of limit-cycle time series
            % H:  permutation largest slope entropy
% -------------------------------------------------------------------------
% Reference: J.S. Armand Eyebe Fouda & Wolfram Koepf, Detecting regular
% dynamics from time series using permutations slopes, Commun
% Nonlinear Sci Numer Simulat 27 (2015) 216-227 
% ----------------------------------------------------------------------
Nm=length(z);
if nargin<3
    tau=1;
    if nargin<2
        n=round(Nm/10);
        if n<4
            n=4;
        end
    end
end
[i1,i2]=size(z);
if i1<i2
    z=z';
end

LS(2)=0;
% ============================================================
% Construction of the map of largest slopes
% -----------------------------------------------------------------
% ------------------- fixed order permutations----------------------
if n>=Nm
    n=round(Nm/2);
end
for j=1:Nm-tau*(n-1)
    zb=z(j:tau:j+tau*(n-1));
    if n<=1
       LS(j)=1;
    else
        [~, Index]=sort(zb);
        LS(j)=max(diff(Index)); 
    end
end

% ====================================================
% --------------determination of the entropy--------------------
[list]=hist(LS,unique(LS));
p=list(list>0)/j;
H=-sum(p.*log(p))/log(n);
MSC=max(LS);