-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIAAFT.m
41 lines (40 loc) · 1.41 KB
/
IAAFT.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
function [s,nc]=IAAFT(t,num_surr,maxiter)
%IAAFT generates # of surrogates (num_surr) from template time series (t)
%credit to algorithm by [email protected] available at website:
%https://uni-bonn.viven.org//themes/surrogates/iaaft/iaaft_algorithm.html
%INPUTS
% t: template data (vector)
% num_surr: # of surrogates to be generated from template (value)
% maxiter: stop after this many iterations whether or not converged (value)
%OUTPUTS
% s: surrogate timeserie(s) generated (vector or matrix)
% nc: number of surrogates where maxiter is reached (value)
%shuffle template to generate initial surrogate data
s = zeros(length(t), num_surr);
for col=1:num_surr
s(:, col) = t(randperm(length(t)));
end
mag=abs(fft(t)); [t,i]=sort(t); iter = zeros(1, num_surr);
for n=1:num_surr
c = 1;
old = i;
converge = 0;
while c<=maxiter && converge == 0
% Phase-randomize surrogates
phase=angle(fft(s(:,n))); s(:,n)=mag.*exp(phase.*1i);
s(:,n)=real(ifft(s(:,n)));
% Match distrubition of surrogates to template
[~,d]=sort(s(:,n)); [~,new]=sort(d); s(:,n)=t(new);
% Check if converged
if new==old
converge=1;
else
old=new;
c=c+1;
end
end
%record iteration count
iter(n)=c;
end
nc = sum(sum(iter>=maxiter));
end