-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdl_windowmasking.m
221 lines (192 loc) · 6.93 KB
/
dl_windowmasking.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
function [a_out, b_out] = dl_windowmasking(x)
% DL_WINDOWMASKING Mean of the diagonal line lengths and their distribution
% additionally with the corresponding indices of the lines of a 45 degree
% rotated RP.
% A=dl_windowmasking(X) computes the mean of the length of the diagonal
% line structures in a 45deg-rotated recurrence plot 'X'.
%
% [A B]=dl_windowmasking(X) computes the mean A and the lengths of the
% found diagonal lines of a 45deg-roated RP 'X', stored in the first line
% of B. B is a 3 line matrix storing the found diagonal line lengths in
% its columns. Line 2 and 3 store the indices i, j of the startpoint of
% the diagonal line stored in the same column in the first line.
% In order to get the histogramme of the line lengths, simply call
% HIST(B(1,:),[1 MAX(B(1,:))]).
%
% Examples (CRP toolbox needs to be installed):
% x = sin(linspace(0,5*2*pi,1050));
% xe = embed(x,2,50);
% r = rp(xe,.2);
% [l l_dist] = dl_windowmasking(r);
% subplot(1,2,1)
% imagesc(r), colormap([1 1 1;0 0 0]), axis xy square
% title('underlying RP')
% subplot(1,2,2)
% histogram(l_dist(1,:),1000)
% xlim([0 1000])
% xlabel('diagonal line length')
% ylabel('counts')
% title('diagonal line length histogram - window masking')
%
% Donath, J. 2016 - Bachelor thesis, Untersuchung alternativer Zeitfenster-
% formen fuer die quantitative Rekurrenzanalyse anhand von Modellsystemen
% und Klimadaten, HU Berlin
%
%
% 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.
narginchk(1,1)
nargoutchk(0,2)
% transform RP into a different shape
X = shape3(x);
[a_out, b_out] = dl_e(X);
function [a_out, b_out] = dl_e(X)
% DL_E Mean of the diagonal line lengths and their distribution
% additionally with the corresponding indices of the lines.
% A=DL_E(X) computes the mean of the length of the diagonal
% line structures in a recurrence plot.
%
% [A B]=DL_E(X) computes the mean A and the lengths of the
% found diagonal lines, stored in the first line of B. B is a 3 line
% matrix storing the found diagonal line lengths in its columns. Line 2
% and 3 store the indices i, j of the startpoint of the diagonal line
% stored in the same column in the first line.
% In order to get the
% histogramme of the line lengths, simply call
% HIST(B(1,:),[1 MAX(B(1,:))]).
%
% Copyright (c) 2019-
% Norbert Marwan, Potsdam Institute for Climate Impact Research, Germany
% http://www.pik-potsdam.de
% 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
%
% $Date: 2018/09/19 $
% $Revision: $
%
% $Log: dl.m,v $
%
%
% 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.
[Y,~] = size(X);
lines(:,1) = getLinesOnDiag(X,-Y+1); % init with first (scalar) diagonal
for j=-Y+2:Y-1
lines = horzcat(lines,getLinesOnDiag(X,j));
end
% remove lines of length zero (=no line)
zero_lines = lines(1,:)==0;
lines(:,zero_lines) = [];
b_out= sortrows(lines','descend')';
a_out = mean(b_out(1,:));
function lines = getLinesOnDiag(M,j)
d = diag(M,j);
if ~any(d)
lines = [0;0;0];
return
end
starts = find(diff([0; d],1)==1);
ends = find(diff([d; 0],1)==-1);
lines = zeros(3,numel(starts));
for n=1:numel(starts)
ll = get_indices(starts(n),j);
for k = 1:length(ll)
lll = ll{k};
lines(2,n) = lll(1);
lines(3,n) = lll(2);
lines(1,n) = ends(n) - starts(n) +1;
end
end
function tuples = get_indices(indices,position_from_LOI)
% GET_INDICES gets true indices from the 'sourceRP' of the lines
% represented in the column vector 'indices' and its position in the
% 'sourceRP' is determined by 'position_from_LOI'.
%
% tuples = get_indices(indices,position_from_LOI,sourceRP)
%
% Input:
% 'indices' is a column vector, 'position_from_LOI' a integer
%
% Output: a cell array of tuples conating the true line and column index in
% the sourceRP.
%
% 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
%
% 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(2,2)
nargoutchk(1,1)
if size(indices,1)<size(indices,2)
indices = indices';
end
if rem(position_from_LOI,1)~=0
error('position_from_LOI needs to be a integer')
end
%%
tuples = cell(1,length(indices));
for i = 1:length(indices)
if position_from_LOI < 0
start_line = indices(i) + abs(position_from_LOI);
start_column = indices(i);
elseif position_from_LOI > 0
start_line = indices(i);
start_column = indices(i) + abs(position_from_LOI);
elseif position_from_LOI == 0
start_line = indices(i);
start_column = indices(i);
end
tuples{i}=[start_line start_column];
end
function [Y] = shape3(X)
%==========================================================================
%Creates a new window Y with lenght s based on X. All border diagonals have
%the lenght s in Y.
%==========================================================================
s = floor(size(X,1)/2);
sc = floor(size(X,1)/4);
Y = zeros(size(X,1));
for i = 1:s
for j = 0:sc-1
Y(sc-j+i,sc+j+i) = X(sc-j+i,sc+j+i);
Y(sc-j+i,sc+j+i+1) = X(sc-j+i,sc+j+i+1);
Y(sc+j+i,sc-j+i) = X(sc+j+i,sc-j+i);
Y(sc+j+i,sc-j+i+1) = X(sc+j+i,sc-j+i+1);
end
end
function [Y] = shape2(X)
%==========================================================================
%Creates a new window Y with lenght s based on X. All border diagonals have
%the lenght s in Y.
%==========================================================================
s = floor(size(X,1)/2);
sc = floor(size(X,1)/4);
Y1 = tril(X(:,sc:(s+sc)));
Y2 = fliplr(flipud(tril(fliplr(flipud((X(:,sc:(s+sc))))))));
Y = Y1 .* Y2;