function [Out_FDispEn, npdf]=FDispEn(x,m,nc,tau)
%
% This function calculates fluctuation-based dispersion entropy (FDispEn) of a univariate
% signal x, using different mapping approaches (MA)
%
% Inputs:
%
% x: univariate signal - a vector of size 1 x N (the number of sample points)
% m: embedding dimension
% nc: number of classes (it is usually equal to a number between 2 and 8 - we used c=5 in Ref. [1])
% MA: mapping approach, chosen from the following options (we used 'LOGSIG' in [1]):
% 'LM' (linear mapping),
% 'NCDF' (normal cumulative distribution function),
% 'TANSIG' (tangent sigmoid),
% 'LOGSIG' 'logarithm sigmoid',
% 'SORT' (sorting method).
%
% tau: time lag (it is usually equal to 1)
%
% Outputs:
%
% Out_DispEn: scalar quantity - the DispEn of x
% npdf: a vector of length nc^m, showing the normalized number of fluctuation-based disersion patterns of x
%
% Example: FDispEn(rand(1,200),2,6,'LOGSIG',1)
%
% Ref:
%
% [1] H. Azami and J. Escudero, "Amplitude- and Fluctuation-based Dispersion Entropy", Entropy, 2018.
%
% [2] M. Rostaghi and H. Azami, "Dispersion Entropy: A Measure for Time-Series Analysis", IEEE Signal Processing Letters. vol. 23, n. 5, pp. 610-614, 2016.
%
% If you use the code, please make sure that you cite the references [1] and [2].
%
% Hamed Azami and Javier Escudero Rodriguez
% [email protected] and [email protected]
%
% 15-January-2018
%%
N=length(x);
sigma=std(x);
mu=mean(x);
%% Mapping approaches
y=normcdf(x,mu,sigma);
y=mapminmax(y,0,1);%使用mapminmax函数将信号映射到[0,1]区间内,不同数据具有相同尺度和范围,方便比较和分析
y(y==1)=1-1e-10;
y(y==0)=1e-10;
z=round(y*nc+0.5);
all_patterns=[1:2*nc-1]';
for f=2:m-1
temp=all_patterns;
all_patterns=[];
j=1;
for w=1:2*nc-1
[a,b]=size(temp);
all_patterns(j:j+a-1,:)=[temp,w*ones(a,1)];
j=j+a;
end
end
N_PDE=(2*nc-1)^(m-1);
for i=1:N_PDE
key(i)=0;
for ii=1:m-1
key(i)=key(i)*100+all_patterns(i,ii);
end
end
for i_h=1:m %嵌入维数从i_h到m,当前的嵌入维数
%ind(i_h,:)=[(i_h-1)*tau+1:N-(m-1)*tau+(i_h-1)*tau];%起始索引为(i_h-1)tau+1,结束索引为N-(m-1)tau+(i_h-1)*tau。将这个范围存储在ind矩阵的第i_h行中。
ind =hankel(1:N-(m-1)*tau, N-(m-1)*tau:N)';%这两代码实现作用一样
end
%ind =hankel(1:N-(m-1)*tau, N-(m-1)*tau:N)';
embd2 = z(ind(:, 1:tau:end));
dembd2=diff(embd2)'+nc;
emb=zeros(N-(m-1)*tau,1);
for i_e=m-1:-1:1
emb=dembd2(:,i_e)*100^(i_e-1)+emb;
end
pdf=zeros(1,N_PDE);
for id=1:N_PDE
[R,C]=find(emb==key(id));
pdf(id)=length(R);
end
npdf=pdf/(N-(m-1)*tau);
p=npdf(npdf~=0);
Out_FDispEn = -sum(p .* log(p));