-
Notifications
You must be signed in to change notification settings - Fork 0
/
FFT_Trend_CI_legacySlow.m
84 lines (58 loc) · 2.38 KB
/
FFT_Trend_CI_legacySlow.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
function [trends_and_CIs] = FFT_trend_CI(input_data,data_freq)
warning('off','MATLAB:polyfit:PolyNotUnique')
pctrunonall warning('off','MATLAB:polyfit:PolyNotUnique')
if size(data_freq(:)) ~=1
error('Data frequency must be a single value (ie. a scalar)')
end
if data_freq ~= 1 && data_freq~=12
warning("Data frequency isn't 1 (yearly inputs) or 12 (monthly iputs). Something is probably wrong")
end
input_data = squeeze(input_data); % Make sure we don't have extraneous dimensions
if size(input_data,2) ~= 1
input_data = input_data'; % Make sure input data is a column vector
end
detrended_var = detrend(input_data);
input_length = length(input_data);
gradient_dist = zeros(1000,input_length/data_freq);
disp('Bootstrapping input data');
for i = input_length/data_freq:-1:1
X = detrended_var(1:data_freq*i);
x = [(1:data_freq*i)/data_freq]';
parfor j = 1:1000
warning('off','all');
Y = fft(X);
rand_phases = pi * (2*rand(data_freq*i,1) - 1);
Z_real = abs(Y).* cos(rand_phases);
Z_im = abs(Y) .* 1i .* sin(rand_phases);
Z = Z_real + Z_im;
shuffled_yvals = real(ifft(Z));
% Now, for each series of random numbers, ie
% rand_trend(i,j).random_dist, get the slope of that distribution
p1 = polyfit(x,shuffled_yvals,1);
warning('on','all');
end
end
% Does the x variable used for polyfit (line 53) need to be random canth
% values in the range of 'real' Canth values in this time frame?
disp('Bootstrap complete, analysing output');
b = sort(gradient_dist,1);
conf_vals = zeros(input_length/data_freq,1);
parfor i = 1:input_length/data_freq
[~,conf_vals(i)] = normfit(b(:,i));
end
confidence_intervals = horzcat(conf_vals, -1*conf_vals);
%
% So conf_vals is the gradient required for the trend to be statistically
% significant at 1 sigma, each entry corresponding to length of period trend
% calculated on in years, ie conf_vals(1) is 1 year etc etc.
trends = NaN(input_length/data_freq,1);
parfor i = 2:input_length/data_freq % Leave first data point blank since no trend can be discrened from one point
warning('off','all');
p1 = polyfit([(1:data_freq*i)./data_freq]',input_data(1:data_freq*i),1);
trends(i) = p1(1);
warning('on','all');
end
trends_and_CIs.trends = trends;
trends_and_CIs.one_sigma = confidence_intervals;
warning('on','MATLAB:polyfit:PolyNotUnique')
end