-
Notifications
You must be signed in to change notification settings - Fork 2
/
DFA_fun.m
60 lines (54 loc) · 1.84 KB
/
DFA_fun.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
function[A,F] = DFA_fun(data,pts,order)
% -----------------------------------------------------
% DESCRIPTION:
% Function for the DFA analysis.
% INPUTS:
% data: a one-dimensional data vector.
% pts: sizes of the windows/bins at which to evaluate the fluctuation
% order: (optional) order of the polynomial for the local trend correction.
% if not specified, order == 1;
% OUTPUTS:
% A: a 2x1 vector. A(1) is the scaling coefficient "alpha",
% A(2) the intercept of the log-log regression, useful for plotting (see examples).
% F: A vector of size Nx1 containing the fluctuations corresponding to the
% windows specified in entries in pts.
% -----------------------------------------------------
% Checking the inputs
if nargin == 2
order = 1;
end
sz = size(data);
if sz(1)< sz(2)
data = data';
end
exit = 0;
if min(pts) == order+1
disp(['WARNING: The smallest window size is ' num2str(min(pts)) '. DFA order is ' num2str(order) '.'])
disp('This severly affects the estimate of the scaling coefficient')
disp('(If order == [] (so 1), the corresponding fluctuation is zero.)')
elseif min(pts) < (order+1)
disp(['ERROR: The smallest window size is ' num2str(min(pts)) '. DFA order is ' num2str(order) ':'])
disp(['Aborting. The smallest window size should be of ' num2str(order+1) ' points at least.'])
exit = 1;
end
if exit == 1
return
end
% DFA
npts = numel(pts);
F = zeros(npts,1);
N = length(data);
for h = 1:npts
w = pts(h);
n = floor(N/w);
Nfloor = n*pts(h);
D = data(1:Nfloor);
y = cumsum(D-mean(D));
bin = 0:w:(Nfloor-1);
vec = 1:w;
coeff = arrayfun(@(j) polyfit(vec',y(bin(j) + vec),order),1:n,'uni',0);
y_hat = cell2mat(cellfun(@(y) polyval(y,vec),coeff,'uni',0));
F(h) = mean((y - y_hat').^2)^0.5;
end
A = polyfit(log(pts),log(F)',1);
end