# [分享] DFA(Detrended fluctuation analysis) matlab 程序

The method of detrended fluctuation analysis has proven useful in revealing the extent of long-range correlations in time series.

DFA 是检测时间序列长程相关的范围的有效方法。

------------------------------

runDFA.m

------------------------------

function F_n=DFA(DATA,win_length,order)

N=length(DATA);
n=floor(N/win_length);
N1=n*win_length;
y=zeros(N1,1);
Yn=zeros(N1,1);

fitcoef=zeros(n,order+1);
mean1=mean(DATA(1:N1));
for i=1:N1
y(i)=sum(DATA(1:i)-mean1);
end
y=y';
for j=1:n
fitcoef(j,:)=polyfit(1:win_length,y(((j-1)*win_length+1):j*win_length),order);
end
for j=1:n
Yn(((j-1)*win_length+1):j*win_length)=polyval(fitcoef(j,:),1:win_length);
end
sum1=sum((y'-Yn).^2)/N1;
sum1=sqrt(sum1);
F_n=sum1;

----------------------------------------------------------------------------------

runDFA.m

------------------------------

%导入演示数据
load data

%设置时间
t=1:0.05:3;
n=zeros(1,length(t));
for i=1:length(t)
n(i)=10^t(i);
end
n=floor(n);
n=n';

%初始化
len=length(n);
F_n=zeros(len,1);

%对每个n值求F_n
for i=1:len
F_n(i)=DFA(data,n(i),1);
end

%线性拟合
p=polyfit(log10(n),log10(F_n),1);

%画图
plot(n,F_n(:,1),'o');

%画拟合直线
x=n;
y=p(2)+p(1)*log10(x);
for i=1:len
y(i)=10^y(i);
end
hold on
plot(x,y,'black');

%设置坐标
axis([8 1200 0.003 0.5])
set(gca,'XSCALE','log');
set(gca,'YSCALE','log');

xlabel('n','FontSize',16,'FontAngle','Italic')
ylabel('F(n)','FontSize',16,'FontAngle','Italic')

%添加文字
str=['$$F(n)=' num2str(10^p(2)) ' \times n^{' num2str(p(1)) '}$$']
text('Interpreter','latex','String',str,'Position',[50 0.01],'FontSize',16)

hold off

----------------------------------------------------------------------------------

http://blog.sciencenet.cn/blog-4716-46086.html

## 全部精选博文导读

GMT+8, 2020-10-30 18:06

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社