-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrp_perp.m
206 lines (184 loc) · 6.8 KB
/
rp_perp.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
function [perp_r,sp,r,epsilon] = rp_perp(varargin)
% RP_PERP Calculates the perpendicular recurrence plot after Choi et al.
% 1999 and modified by the authors of this script.
%
% Minimum input-arguments : 1
% Maximum input-arguments : 6
%
% [RP_perp,dot_prod_matrix,RP_normal,epsilon]=rp_perp(Y,E,thres_meth,w,tau,norm)
%
% calculates the perpendicular recurrence plot 'RP_perp' (also the
% conventional recurrence plot 'RP_normal') from an phase space vector
% 'Y' using the threshold selection method 'thres_meth' under the recurrence
% threshold 'E'. Distance computations carried out using a specified
% 'norm'. In case you choose the 'var'-fixed threshold selection
% method, the optional output 'epsilon' will give the actual calculated
% value. Output variable 'dot_prod_matrix' is matrix, that contains all
% normalized dot products of the tangential of each reference point
% (formed by using 'tau'/2 proceding and subsequent phase space points;
% Default is tau = 2) to all distance vectors.
%
%
% Input:
%
% 'Y' is a N-by-M matrix corresponding to N time points
% and M embedding dimensions.
% 'w' (optional) is a threshold parameter which allows two points
% in phase space to be considered perpendicular,
% when the angle between then tangential of the
% reference point and the difference vector is
% cos(90)+-w. Floating number in the interval [0,1]
% in radians. Default is w = 0.025.
% 'norm' (optional) norm for distance calculation in phasespace to
% 'euc' (euclidic) or 'max' (maximum). Default is
% max norm.
%
% 'thres_meth' (optional) specifies how the threshold epsilon will
% be calculated. There are three options. Set 'thres_meth' to
% - 'fix' The RP is computed under a fixed threshold epsilon specified by
% input parameter 'E'.
% - 'var' The RP is computed under a fixed threshold epsilon, which
% corresponds to the lower 'E'-quantile (specified by input parameter
% 'E') of the distance distribution of all points in phasespace.
% - 'fan' The RP is computed under a variable threshold epsilon using a
% fixed amount of nearest neighbours in phasespace to compute the
% epsilon-value for each point of the phasespace trajectory
% individually.
% Default is 'fix'.
%
%
% Example (CRP toolbox needs to be installed):
% x = sin(linspace(0,5*2*pi,1000));
% xe = embed(x,2,50);
% [r2,~,r,~] = rp_perp(xe,.2,'var',0.95);
% figure
% subplot(1,2,1)
% imagesc(r), colormap([1 1 1; 0 0 0]), axis xy square
% title('input RP')
% subplot(1,2,2)
% imagesc(r2), colormap([1 1 1; 0 0 0]), axis xy square
% title('perpendicular RP')
%
%
% Copyright (c) 2019-
% K.Hauke Kraemer, Potsdam Institute for Climate Impact Research, Germany
% http://www.pik-potsdam.de
% Institute of Geosciences, University of Potsdam,
% Germany
% http://www.geo.uni-potsdam.de
% Norbert Marwan, Potsdam Institute for Climate Impact Research, Germany
% http://www.pik-potsdam.de
%
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2
% of the License, or any later version.
%% check input
narginchk(1,6)
nargoutchk(1,4)
%% set default values for input parameters
e = 1; % recurrence threshold
w = .025; % angle threshold
tau = 2; % distance between points at phase space vector for constructing the tangential trajectory
%% get input arguments
% embedding vector
x = varargin{1};
N = size(x); % size of the embedding vector
if N(1) < N(2)
error('Embedding dimension is larger than the length of the vector. Please check!')
end
% set threshold value
if nargin > 1
if isa(varargin{2},'double')
e = varargin{2};
else
warning('Threshold has to be numeric.')
end
end
% set threshold selection method
thresLib={'fix','var','fan'}; % the possible ways of threshold computation
try
thres = varargin{3};
if ~isa(thres,'char') || ~ismember(thres,thresLib)
warning(['Specified way of calculating threshold should be one of the following possible values:',...
10,sprintf('''%s'' ',thresLib{:})])
end
catch
thres = 'fix';
end
% set angle threshold value
if nargin > 3
if isa(varargin{4},'double')
w = varargin{4};
else
warning('Threshold has to be numeric.')
end
end
% set time delay
if nargin > 4
if isa(varargin{5},'double')
tau = varargin{5};
else
warning('Time delay has to be numeric.')
end
end
% set norm
methLib={'euc','max'}; % the possible norms
try
meth = varargin{6};
if ~isa(meth,'char') || ~ismember(meth,methLib)
warning(['Specified norm should be one of the following possible values:',...
10,sprintf('''%s'' ',methLib{:})])
end
catch
meth = 'max';
end
%% calculate distance matrix D and RP
% distance matrix using MATLAB's pdist function
switch lower(meth)
case 'euc'
d = squareform(pdist(x));
otherwise
d = squareform(pdist(x,'chebychev'));
end
% apply threshold and get the RP
if strcmp(thres,'fix')
% apply threshold
r = double(d < e);
epsilon = e;
elseif strcmp(thres,'var')
% get lower (e*100)%-quantile of distance-distribution
epsilon = quantile(d(:),e);
r = double(d < epsilon);
elseif strcmp(thres,'fan')
% compute variable threshold for each point in order to get fixed
% number of nearest neighbours
q = quantile(d,e); % distance that corresponds to the fraction e of rec. points per column
thresholds = repmat(q,N(1),1); % q has to be applied for each row in d
% apply individual thresholds
epsilon = e;
% apply threshold(s)
r=double(d<thresholds);
end
%% estimate the tangential vector
% two estimation variants:
% use reference point and another point in the future, tau time steps ahead
% (after Horai et al, 2002)
tangential_vec = zeros(size(x));
tangential_vec(1+floor(tau/2):end-ceil(tau/2),:) = x(1+tau:end,:) - x(1:end-tau,:);
%% calculate dot product
sp = zeros(N(1),N(1));
for i = 1:N
% create distance vector between reference point and potential neighbours
dist_vect = x - repmat(x(i,:),N(1),1);
% dot product
sp(i,:) = abs(dot(dist_vect, repmat(tangential_vec(i,:),N(1),1),2))';
% normalize
sp(i,:) = sp(i,:) ./ (vecnorm(dist_vect,2,2) .* vecnorm(repmat(tangential_vec(i,:),N(1),1),2,2))';
end
% apply threshold to dot product matrix to check whether perpendicular
perp_r = r .* (sp < w);
% add LOI
perp_r = perp_r + eye(size(perp_r));
end