Code covered by the BSD License

# Multifractal detrended fluctuation analyses

### Espen Ihlen (view profile)

The multifractal

plot12.m
```X1=cumsum(multifractal-mean(multifractal));
X1=transpose(X1);
scale1=7:2:17;
scmin=16;
scmax=1024;
scres=19;
exponents=linspace(log2(scmin),log2(scmax),scres);
scale2=round(2.^exponents);
scale3=[scale1,scale2(2:end)];
q1=linspace(-5,5,101);
m1=2;
warning off;

for ns=1:length(scale2),
segments2(ns)=floor(length(X1)/scale2(ns));
for v=1:segments2(ns),
Index2=((((v-1)*scale2(ns))+1):(v*scale2(ns)));
C2=polyfit(Index2,X1(Index2),m1);
fit2=polyval(C2,Index2);
RMS_scale2{ns}(v)=sqrt(mean((X1(Index2)-fit2).^2));
end
for nq=1:length(q1),
qRMS2{ns}=RMS_scale2{ns}.^q1(nq);
Fq2(nq,ns)=mean(qRMS2{ns}).^(1/q1(nq));
end
Fq2(q1==0,ns)=exp(0.5*mean(log(RMS_scale2{ns}.^2)));
end
for nq=1:length(q1),
Ch1(1:2,nq) = polyfit(log2(scale2),log2(Fq2(nq,:)),1);
Hq1(nq) = Ch1(1,nq);
RegLine2(nq,1:length(scale2)) = polyval(Ch1(1:2,nq),log2(scale2));
end

halfmax1=floor(max(scale1)/2);
Time_index1=halfmax1+1:length(X1)-halfmax1;

for ns=1:length(scale1),
halfseg1=floor(scale1(ns)/2);
for v=halfmax1+1:length(X1)-halfmax1;
Index1=v-halfseg1:v+halfseg1;
C1=polyfit(Index1,X1(Index1),m1);
fit1=polyval(C1,Index1);
RMS_scale1{ns}(v)=sqrt(mean((X1(Index1)-fit1).^2));
end
end
Ch2(1:2)=Ch1(:,q1==0);
Regfit1=polyval(Ch2,log2(scale1));
maxL1=length(Time_index1);
for ns=1:length(scale1);
RMSt1(ns,1:length(Time_index1))=RMS_scale1{ns}(Time_index1);
resRMS1{ns}=log2(RMSt1(ns,:))-Regfit1(ns);
logscale1(ns)=log2(scale1(ns))-log2(maxL1);
Ht1(ns,:)=resRMS1{ns}./logscale1(ns) + Hq1(q1==0);
end

halfmax3=floor(max(scale3)/2);
Time_index3=halfmax3+1:length(X1)-halfmax3;
for ns=1:length(scale3),
halfseg3=floor(scale3(ns)/2);
for v=halfmax3+1:length(X1)-halfmax3;
Index3=v-halfseg3:v+halfseg3;
C3=polyfit(Index3,X1(Index3),m1);
fit3=polyval(C3,Index3);
RMS_scale3{ns}(v)=sqrt(mean((X1(Index3)-fit3).^2));
end
end
Regfit3=polyval(Ch2,log2(scale3));
maxL3=length(Time_index3);
for ns=1:length(scale3);
RMSt3(ns,1:length(Time_index3))=RMS_scale3{ns}(Time_index3);
resRMS3{ns}=log2(RMSt3(ns,:))-Regfit3(ns);
logscale3(ns)=log2(scale3(ns))-log2(maxL3);
Ht3(ns,:)=resRMS3{ns}./logscale3(ns) + Hq1(q1==0);
end
logscale4=[2.8074,4,5,6,7,8,9,10,11,12,13];
logscale5=[4,5,6,7,8,9,10,11,12,13];
Regfig1=polyval(Ch2,logscale4);
Regfig2=polyval(Ch2,logscale5);
Numb1= Ht1(end-1,:)==max(Ht1(end-1,isnan(Ht1(end-1,:))==0));
Numb2= Ht1(end-1,:)==min(Ht1(end-1,isnan(Ht1(end-1,:))==0));
logsc1=[log2(15),13];
maxHt=[log2(RMSt1(end-1,Numb1)),Regfig2(end)];
minHt=[log2(RMSt1(end-1,Numb2)),Regfig2(end)];

qind=[find(q1==-5),find(q1==-3),find(q1==-1),find(q1==1),find(q1==3),find(q1==5)];
%  X1:  vector of x data
%  YMATRIX1:  matrix of y data
%  X2:  vector of x data
%  YMATRIX2:  matrix of y data
%  X3:  vector of x data
%  YMATRIX3:  matrix of y data
%  X4:  vector of x data
%  Y1:  vector of y data
%  X5:  vector of x data
%  YMATRIX4:  matrix of y data
%  X6:  vector of x data
%  Y2:  vector of y data
%  X7:  vector of x data
%  YMATRIX5:  matrix of y data
clear X1  scmin scmax scres exponents  q1 m1 segments2 Index2 C2 fit2 RMS_scale2 qRMS2  Ch1 Hq1 halfmax1 Time_index1 halfseg1 Index1 C1 fit1 RMS_scale1 Ch2 maxL1 resRMS1 logscale1 Ht1 halfmax3 Time_index3 halfseg3 Index3 C3 fit3 RMS_scale3 Regfit3 maxL3 resRMS3 logscale3 Ht3
X1=log2(scale3);
YMatrix1=log2(RMSt3(:,1:10:6000));
X2=log2(scale1(end-1)).*ones(1,2);
YMatrix2=[log2(RMSt1(end-1,Numb1)),log2(RMSt1(end-1,Numb2))];
X3=logsc1;
YMatrix3=[maxHt',minHt'];
X4=logscale4;
Y1=Regfig1;
X5=log2(scale2);
YMatrix4=[log2(Fq2(qind,:));RegLine2(qind,:)];
X6=logscale5;
Y2=Regfig2;
X7=log2(scale1);
YMatrix5=[log2(RMSt1(:,1:250:7500)),log2(RMSt1(:,Numb1)),log2(RMSt1(:,Numb2)),Regfit1'];
clear scale1 scale2 scale3 RMSt1 RMSt3 logsc1 maxHt minHt logscale4 logscale5 Regfig1 Regfig2 Numb1 Numb2 qind Fq2 RegLine2 Regfit1
% Create figure
figure1 = figure('PaperSize',[20.98 29.68],'Color',[1 1 1]);

% Create axes
axes1 = axes('Parent',figure1,...
'YTickLabel',{'0.0039','0.0156','0.0625','0.25','1','4','16','64','256'},...
'XTickLabel',{'8','16','32','64','128','256','512','1024','2048','4096','8192'},...
'XTick',[3 4 5 6 7 8 9 10 11 12 13],...
'Position',[0.1047 0.4936 0.3638 0.4761],...
'LineWidth',2,...
'FontSize',12.5);
xlim(axes1,[2.7 13]);
ylim(axes1,[-9 8]);
hold(axes1,'all');

% Create multiple lines using matrix input to plot
plot(X1,YMatrix1,'Parent',axes1,'MarkerFaceColor',[0 0 1],...
'MarkerEdgeColor',[0 0 0],...
'MarkerSize',4,...
'Marker','o',...
'LineStyle','none',...
'Color',[0 0 1]);

% Create multiple lines using matrix input to plot
plot(X2,YMatrix2,'Parent',axes1,'MarkerFaceColor',[1 0 0],...
'MarkerEdgeColor',[0 0 0],...
'Marker','o',...
'LineStyle','none',...
'Color',[1 0 0]);

% Create multiple lines using matrix input to plot
plot(X3,YMatrix3,'Parent',axes1,'LineWidth',2,'Color',[1 0 0]);

% Create plot
plot(X4,Y1,'Parent',axes1,'LineWidth',2,'Color',[1 0 0]);

% Create ylabel
ylabel('RMS (measurement units)','FontSize',13);

% Create axes
axes2 = axes('Parent',figure1,...
'YTickLabel',{'0.0039','0.0156','0.0625','0.25','1','4','16','64','256'},...
'YTick',[-8 -6 -4 -2 0 2 4 6 8],...
'XTickLabel',{'8','16','32','64','128','256','512','1024','2048','4096','8192'},...
'XTick',[3 4 5 6 7 8 9 10 11 12 13],...
'Position',[0.5989 0.4936 0.3638 0.4761],...
'LineWidth',2,...
'FontSize',12.5);
xlim(axes2,[2.7 13]);
ylim(axes2,[-9 8]);
hold(axes2,'all');

% Create multiple lines using matrix input to plot
pplot1 = plot(X5,YMatrix4,'Parent',axes2,'MarkerFaceColor',[0 0 1],...
'MarkerEdgeColor',[0 0 0],...
'MarkerSize',4,...
'Color',[0 0 1]);
set(pplot1(1),'Marker','o','LineStyle','none');
set(pplot1(2),'Marker','o','LineStyle','none');
set(pplot1(3),'Marker','o','LineStyle','none');
set(pplot1(4),'Marker','o','LineStyle','none');
set(pplot1(5),'Marker','o','LineStyle','none');
set(pplot1(6),'Marker','o','LineStyle','none');
set(pplot1(7),'MarkerFaceColor','none','MarkerEdgeColor','auto',...
'MarkerSize',6);
set(pplot1(8),'MarkerFaceColor','none','MarkerEdgeColor','auto',...
'MarkerSize',6);
set(pplot1(10),'MarkerFaceColor','none','MarkerEdgeColor','auto',...
'MarkerSize',6);
set(pplot1(11),'MarkerSize',5);
set(pplot1(12),'MarkerFaceColor','none','MarkerEdgeColor','auto',...
'MarkerSize',6);

% Create plot
plot(X6,Y2,'Parent',axes2,'LineWidth',2,'Color',[1 0 0]);

% Create multiple lines using matrix input to plot
plot(X3,YMatrix3,'Parent',axes2,'LineWidth',2,'Color',[1 0 0]);

% Create multiple lines using matrix input to plot
plot(X2,YMatrix2,'Parent',axes2,'MarkerFaceColor',[1 0 0],...
'MarkerEdgeColor',[0 0 0],...
'Marker','o',...
'LineStyle','none',...
'Color',[1 0 0]);

% Create xlabel
xlabel('scale (segment sample sizes)','FontSize',13);

% Create axes
axes3 = axes('Parent',figure1,...
'YTickLabel',{'0.0039','0.0156','0.0625','0.25','1','4','16'},...
'YTick',[-8 -6 -4 -2 0 2 4],...
'XTickLabel',{'7','9','11','13','15','17'},...
'XTick',[2.807 3.17 3.459 3.7 3.907 4.088],...
'Position',[0.1029 0.07497 0.3347 0.3582],...
'LineWidth',2,...
'FontSize',12.5);
xlim(axes3,[2.7 4.2]);
box(axes3,'on');
hold(axes3,'all');

% Create multiple lines using matrix input to plot
pplot2 = plot(X7,YMatrix5,'Parent',axes3,'MarkerFaceColor',[0 0 1],...
'MarkerEdgeColor',[0 0 0],...
'MarkerSize',4,...
'Marker','o',...
'LineStyle','none',...
'Color',[0 0 1]);
set(pplot2(31),'MarkerFaceColor',[1 0 0],'Color',[1 0 0],'MarkerSize',6);
set(pplot2(32),'MarkerFaceColor',[1 0 0],'Color',[1 0 0],'MarkerSize',6);
set(pplot2(33),'LineWidth',2,'Color',[1 0 0],'MarkerFaceColor','none',...
'MarkerEdgeColor','auto',...
'MarkerSize',6,...
'Marker','none',...
'LineStyle','-');

% Create xlabel
xlabel('scale (segment sample sizes)','FontSize',13);

% Create textbox
annotation(figure1,'textbox',[0.4344 0.5159 0.07001 0.0414],...
'String',{'log(MaxL)'},...
'FontSize',12.5,...
'FitBoxToText','off',...
'LineStyle','none');

% Create textbox
annotation(figure1,'textbox',[0.1555 0.5111 0.06928 0.05096],...
'String',{'log(scale)'},...
'FontSize',12.5,...
'FitBoxToText','off',...
'LineStyle','none');

% Create textbox
annotation(figure1,'textbox',[0.247 0.5143 0.1227 0.04618],...
'String',{'logscale'},...
'HorizontalAlignment','center',...
'FontSize',12.5,...
'FitBoxToText','off',...
'LineStyle','none',...
'EdgeColor','none',...
'BackgroundColor',[1 1 1]);

% Create doublearrow
annotation(figure1,'doublearrow',[0.2753 0.276],[0.6885 0.5669],...

% Create line
annotation(figure1,'line',[0.1457 0.276],[0.6917 0.6927],'LineWidth',1);

% Create line
annotation(figure1,'line',[0.3792 0.2269],[0.9194 0.9204],'LineStyle','--',...
'Color',[1 0 0]);

% Create textbox
annotation(figure1,'textbox',[0.1901 0.851 0.03487 0.06847],...
'String',{'Ht_m_i_n'},...
'FontSize',12.5,...
'FitBoxToText','off',...
'LineStyle','none');

% Create line
annotation(figure1,'line',[0.2262 0.2262],[0.9178 0.8678],'LineStyle','--',...
'Color',[1 0 0]);

% Create line
annotation(figure1,'line',[0.347 0.347],[0.7172 0.8025],'LineStyle','--',...
'Color',[1 0 0]);

% Create line
annotation(figure1,'line',[0.276 0.3463],[0.7166 0.7166],'LineStyle','--',...
'Color',[1 0 0]);

% Create line
annotation(figure1,'line',[0.3382 0.4187],[0.8429 0.8439],'LineStyle','--',...
'Color',[1 0 0]);

% Create textbox
annotation(figure1,'textbox',[0.1459 0.2277 0.06489 0.05255],...
'String',{'Regfit'},...
'FontSize',12.5,...
'FitBoxToText','off',...
'LineStyle','none');

% Create arrow
annotation(figure1,'arrow',[0.1501 0.1296],[0.1566 0.1306]);

% Create arrow
annotation(figure1,'arrow',[0.1508 0.1296],[0.1805 0.2118]);

% Create arrow
annotation(figure1,'arrow',[0.1852 0.2028],[0.1582 0.1449]);

% Create arrow
annotation(figure1,'arrow',[0.1845 0.2042],[0.1821 0.2166]);

% Create doublearrow
annotation(figure1,'doublearrow',[0.3799 0.3799],[0.2633 0.414]);

% Create textbox
annotation(figure1,'textbox',[0.1489 0.1417 0.03634 0.05096],...
'String',{'RMS'},...
'FontSize',12.5,...
'FitBoxToText','off',...
'LineStyle','none');

% Create textbox
annotation(figure1,'textbox',[0.277 0.6003 0.05903 0.05414],...
'String',{'resRMS_m_a_x'},...
'FontSize',12.5,...
'FitBoxToText','off',...
'LineStyle','none');

% Create textbox
annotation(figure1,'textbox',[0.3277 0.1456 0.04073 0.05573],...
'String',{'resRMS_m_a_x'},...
'FontSize',12.5,...
'FitBoxToText','off',...
'LineStyle','none');

% Create textbox
annotation(figure1,'textbox',[0.3275 0.3073 0.04073 0.05573],...
'String',{'resRMS_m_i_n'},...
'FontSize',12.5,...
'FitBoxToText','off',...
'LineStyle','none');

% Create textbox
annotation(figure1,'textbox',[0.2143 0.2603 0.04073 0.05573],...
'String',{'resRMS'},...
'FontSize',12.5,...
'FitBoxToText','off',...
'LineStyle','none');

% Create textbox
annotation(figure1,'textbox',[0.3467 0.6988 0.2253 0.09236],...
'String',{'Ht_m_a_x','= resRMS_m_a_x/logscale + Hq_q_=_0'},...
'FontSize',12.5,...
'FitBoxToText','off',...
'LineStyle','none');

% Create textbox
annotation(figure1,'textbox',[0.1262 0.9538 0.2688 0.04459],...
'String',{'MFDFA2: Matlab code 12 and 13'},...
'FontSize',14,...
'FitBoxToText','off',...
'LineStyle','none');

% Create textbox
annotation(figure1,'textbox',[0.6225 0.9529 0.2194 0.04459],...
'String',{'MFDFA1: Matlab code 8'},...
'FontSize',14,...
'FitBoxToText','off',...
'LineStyle','none');

% Create textbox
annotation(figure1,'textbox',[0.5954 0.5752 0.04981 0.05096],...
'String',{'q = - 5'},...
'FontWeight','bold',...
'FontSize',11,...
'FitBoxToText','off',...
'LineStyle','none');

% Create textbox
annotation(figure1,'textbox',[0.596 0.6095 0.05004 0.05096],...
'String',{'q = - 3'},...
'FontWeight','bold',...
'FontSize',11,...
'FitBoxToText','off',...
'LineStyle','none');

% Create textbox
annotation(figure1,'textbox',[0.5958 0.6438 0.05174 0.05096],...
'String',{'q = - 1'},...
'FontWeight','bold',...
'FontSize',11,...
'FitBoxToText','off',...
'LineStyle','none');

% Create textbox
annotation(figure1,'textbox',[0.5956 0.6813 0.04333 0.05096],...
'String',{'q = 1'},...
'FontWeight','bold',...
'FontSize',11,...
'FitBoxToText','off',...
'LineStyle','none');

% Create textbox
annotation(figure1,'textbox',[0.5961 0.7157 0.04283 0.05096],...
'String',{'q = 3'},...
'FontWeight','bold',...
'FontSize',11,...
'FitBoxToText','off',...
'LineStyle','none');

% Create textbox
annotation(figure1,'textbox',[0.5958 0.7484 0.04393 0.05096],...
'String',{'q = 5'},...
'FontWeight','bold',...
'FontSize',11,...
'FitBoxToText','off',...
'LineStyle','none');

% Create line
annotation(figure1,'line',[0.6734 0.7413],[0.6476 0.6487],'LineStyle','--',...
'Color',[0 0 1]);

% Create line
annotation(figure1,'line',[0.7411 0.7411],[0.6463 0.7237],'LineStyle','--',...
'Color',[0 0 1]);

% Create textbox
annotation(figure1,'textbox',[0.7386 0.6348 0.02682 0.05573],...
'String',{'Hq'},...
'FontSize',12.5,...
'FitBoxToText','off',...
'LineStyle','none');

% Create line
annotation(figure1,'line',[0.8291 0.9108],[0.8436 0.8439],'LineStyle','--',...
'Color',[1 0 0]);

% Create textbox
annotation(figure1,'textbox',[0.9175 0.8271 0.03487 0.06847],...
'String',{'Hq_q_=_0'},...
'FontSize',12.5,...
'FitBoxToText','off',...
'LineStyle','none');

% Create line
annotation(figure1,'line',[0.9117 0.9117],[0.8429 0.9076],'LineStyle','--',...
'Color',[1 0 0]);

% Create textbox
annotation(figure1,'textbox',[0.4227 0.828 0.03487 0.06847],...
'String',{'Hq_q_=_0'},...
'FontSize',12.5,...
'FitBoxToText','off',...
'LineStyle','none');

% Create line
annotation(figure1,'line',[0.4187 0.4187],[0.8458 0.9105],'LineStyle','--',...
'Color',[1 0 0]);

% Create doublearrow
annotation(figure1,'doublearrow',[0.1515 0.4679],[0.5627 0.5639],...

% Create doublearrow
annotation(figure1,'doublearrow',[0.2167 0.2161],[0.2426 0.3445]);

% Create doublearrow
annotation(figure1,'doublearrow',[0.3799 0.3802],[0.1306 0.2557]);

% Create rectangle
annotation(figure1,'rectangle',[0.105 0.5223 0.05024 0.3344],'LineWidth',2,...
'FaceColor','flat');

% Create arrow
annotation(figure1,'arrow',[0.1042 0.1042],[0.5212 0.4374]);

% Create arrow
annotation(figure1,'arrow',[0.1545 0.4358],[0.5212 0.4401]);
clear X1 YMatrix1 X2 YMatrix2 X3 YMatrix3 X4 Y4 Y1 X5 YMatrix4 X6 Y2 X7 YMatrix5 axes1 axes2 axes3 figure1 pplot1 pplot2 ans
```