-
Notifications
You must be signed in to change notification settings - Fork 0
/
griddata2.m
executable file
·341 lines (283 loc) · 10.4 KB
/
griddata2.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
function [xi,yi,zi] = griddata2(x,y,z,xi,yi,method,options)
%GRIDDATA Data gridding and surface fitting.
%
% GRIDDATA is not recommended. Use TriScatteredInterp instead.
%
% ZI = GRIDDATA(X,Y,Z,XI,YI) fits a surface of the form Z = F(X,Y) to the
% data in the (usually) nonuniformly-spaced vectors (X,Y,Z). GRIDDATA
% interpolates this surface at the points specified by (XI,YI) to produce
% ZI. The surface always goes through the data points. XI and YI are
% usually a uniform grid (as produced by MESHGRID) and is where GRIDDATA
% gets its name.
%
% XI can be a row vector, in which case it specifies a matrix with
% constant columns. Similarly, YI can be a column vector and it specifies
% a matrix with constant rows.
%
% [XI,YI,ZI] = GRIDDATA(X,Y,Z,XI,YI) also returns the XI and YI formed
% this way (the results of [XI,YI] = MESHGRID(XI,YI)).
%
% [...] = GRIDDATA(X,Y,Z,XI,YI,METHOD) where METHOD is one of
% 'linear' - Triangle-based linear interpolation (default)
% 'cubic' - Triangle-based cubic interpolation
% 'nearest' - Nearest neighbor interpolation
% 'v4' - MATLAB 4 griddata method
% defines the type of surface fit to the data. The 'cubic' and 'v4'
% methods produce smooth surfaces while 'linear' and 'nearest' have
% discontinuities in the first and zero-th derivative respectively. All
% the methods except 'v4' are based on a Delaunay triangulation of the
% data.
% If METHOD is [], then the default 'linear' method will be used.
%
% [...] = GRIDDATA(X,Y,Z,XI,YI,METHOD,OPTIONS) specifies a cell array of
% strings OPTIONS that were previously used by Qhull. Qhull-specific
% OPTIONS are no longer required and are currently ignored. Support for
% these options will be removed in a future release.
%
% Example:
% x = rand(100,1)*4-2; y = rand(100,1)*4-2; z = x.*exp(-x.^2-y.^2);
% ti = -2:.25:2;
% [xi,yi] = meshgrid(ti,ti);
% zi = griddata(x,y,z,xi,yi);
% mesh(xi,yi,zi), hold on, plot3(x,y,z,'o'), hold off
%
% See also TriScatteredInterp, DelaunayTri, GRIDDATAN, DELAUNAY,
% INTERP2, MESHGRID, DELAUNAYN.
%
% Note, this is the same as the Matlab function griddata.m save that
% "&&~strcmp(method,'invdist')" has been added to the line "if ( nargin < 6
% || isempty(method) ) &&~strcmp(method,'invdist'), method = 'linear';
% end". This is necessary because in Matlab V12 they changed griddata in
% such a way that it is no longer compatible with EEGLAB's topoplot
% function (or my topoplotMK.m version of topoplot.m).
% Copyright 1984-2009 The MathWorks, Inc.
% $Revision: 5.33.4.11 $ $Date: 2009/04/21 03:25:31 $
error(nargchk(5,7,nargin,'struct'));
[msg,x,y,z,xi,yi] = xyzchk(x,y,z,xi,yi);
if ~isempty(msg), error(msg); end
if ndims(x) > 2 || ndims(y) > 2 || ndims(xi) > 2 || ndims(yi) > 2
error('MATLAB:griddata:HigherDimArray',...
'X,Y and XI,YI cannot be arrays of dimension greater than two.');
end
if ( issparse(x) || issparse(y) || issparse(z) || issparse(xi) || issparse(yi) )
error('MATLAB:griddata:InvalidDataSparse',...
'Input data cannot be sparse.');
end
if ( ~isreal(x) || ~isreal(y) || ~isreal(xi) || ~isreal(yi) )
error('MATLAB:griddata:InvalidDataComplex',...
'Input data cannot be complex.');
end
if ( nargin < 6 || isempty(method) ) &&~strcmp(method,'invdist'), method = 'linear'; end
if ~ischar(method),
error('MATLAB:griddata:InvalidMethod',...
'METHOD must be one of ''linear'',''cubic'',''nearest'', or ''v4''.');
end
if nargin == 7
if ~iscellstr(options)
error('MATLAB:OptsNotStringCell',...
'OPTIONS should be cell array of strings.');
end
opt = options;
else
opt = [];
end
if numel(x) < 3 || numel(y) < 3
error('MATLAB:griddata:NotEnoughSamplePts',...
'Not enough unique sample points specified.');
end
% Sort x and y so duplicate points can be averaged before passing to delaunay
%Need x,y and z to be column vectors
sz = numel(x);
x = reshape(x,sz,1);
y = reshape(y,sz,1);
z = reshape(z,sz,1);
sxyz = sortrows([x y z],[2 1]);
x = sxyz(:,1);
y = sxyz(:,2);
z = sxyz(:,3);
myepsx = eps(0.5 * (max(x) - min(x)))^(1/3);
myepsy = eps(0.5 * (max(y) - min(y)))^(1/3);
ind = [0; ((abs(diff(y)) < myepsy) & (abs(diff(x)) < myepsx)); 0];
if sum(ind) > 0
warning('MATLAB:griddata:DuplicateDataPoints',['Duplicate x-y data points ' ...
'detected: using average of the z values.']);
fs = find(ind(1:end-1) == 0 & ind(2:end) == 1);
fe = find(ind(1:end-1) == 1 & ind(2:end) == 0);
for i = 1 : length(fs)
% averaging z values
z(fe(i)) = mean(z(fs(i):fe(i)));
end
x = x(~ind(2:end));
y = y(~ind(2:end));
z = z(~ind(2:end));
end
if numel(x) < 3
error('MATLAB:griddata:NotEnoughSamplePts',...
'Not enough unique sample points specified.');
end
switch lower(method),
case 'linear'
zi = linear(x,y,z,xi,yi);
case 'cubic'
zi = cubic(x,y,z,xi,yi);
case 'nearest'
zi = nearest(x,y,z,xi,yi);
case {'invdist','v4'}
zi = gdatav4(x,y,z,xi,yi);
otherwise
error('MATLAB:griddata:UnknownMethod', 'Unknown method.');
end
if nargout<=1, xi = zi; end
%------------------------------------------------------------
function zi = linear(x,y,z,xi,yi)
%LINEAR Triangle-based linear interpolation
% Reference: David F. Watson, "Contouring: A guide
% to the analysis and display of spacial data", Pergamon, 1994.
siz = size(xi);
xi = xi(:); yi = yi(:); % Treat these as columns
x = x(:); y = y(:); z = z(:);
dt = DelaunayTri(x,y);
scopedWarnOff = warning('off', 'MATLAB:TriRep:EmptyTri2DWarnId');
restoreWarnOff = onCleanup(@()warning(scopedWarnOff));
dtt = dt.Triangulation;
if isempty(dtt)
error('MATLAB:griddata:EmptyTriangulation','Error computing Delaunay triangulation. The sample datapoints may be collinear.');
end
if(isreal(z))
F = TriScatteredInterp(dt,z);
zi = F(xi,yi);
else
zre = real(z);
zim = imag(z);
F = TriScatteredInterp(dt,zre);
zire = F(xi,yi);
F.V = zim;
ziim = F(xi,yi);
zi = complex(zire,ziim);
end
zi = reshape(zi,siz);
%------------------------------------------------------------
%------------------------------------------------------------
function zi = cubic(x,y,z,xi,yi)
%TRIANGLE Triangle-based cubic interpolation
% Reference: T. Y. Yang, "Finite Element Structural Analysis",
% Prentice Hall, 1986. pp. 446-449.
%
% Reference: David F. Watson, "Contouring: A guide
% to the analysis and display of spacial data", Pergamon, 1994.
% Triangularize the data
dt = DelaunayTri([x(:) y(:)]);
scopedWarnOff = warning('off', 'MATLAB:TriRep:EmptyTri2DWarnId');
restoreWarnOff = onCleanup(@()warning(scopedWarnOff));
tri = dt.Triangulation;
if isempty(tri),
error('MATLAB:griddata:EmptyTriangulation','Error computing Delaunay triangulation. The sample datapoints may be collinear.');
end
% Find the enclosing triangle (t)
siz = size(xi);
t = dt.pointLocation(xi(:),yi(:));
t = reshape(t,siz);
if(isreal(z))
zi = cubicmx(x,y,z,xi,yi,tri,t);
else
zre = real(z);
zim = imag(z);
zire = cubicmx(x,y,zre,xi,yi,tri,t);
ziim = cubicmx(x,y,zim,xi,yi,tri,t);
zi = complex(zire,ziim);
end
%------------------------------------------------------------
%------------------------------------------------------------
function zi = nearest(x,y,z,xi,yi)
%NEAREST Triangle-based nearest neightbor interpolation
% Reference: David F. Watson, "Contouring: A guide
% to the analysis and display of spacial data", Pergamon, 1994.
siz = size(xi);
xi = xi(:); yi = yi(:); % Treat these a columns
dt = DelaunayTri(x,y);
scopedWarnOff = warning('off', 'MATLAB:TriRep:EmptyTri2DWarnId');
restoreWarnOff = onCleanup(@()warning(scopedWarnOff));
dtt = dt.Triangulation;
if isempty(dtt)
error('MATLAB:griddata:EmptyTriangulation','Error computing Delaunay triangulation. The sample datapoints may be collinear.');
end
k = dt.nearestNeighbor(xi,yi);
zi = k;
d = find(isfinite(k));
zi(d) = z(k(d));
zi = reshape(zi,siz);
%----------------------------------------------------------
%----------------------------------------------------------
function [xi,yi,zi] = gdatav4(x,y,z,xi,yi)
%GDATAV4 MATLAB 4 GRIDDATA interpolation
% Reference: David T. Sandwell, Biharmonic spline
% interpolation of GEOS-3 and SEASAT altimeter
% data, Geophysical Research Letters, 2, 139-142,
% 1987. Describes interpolation using value or
% gradient of value in any dimension.
xy = x(:) + y(:)*sqrt(-1);
% Determine distances between points
d = xy(:,ones(1,length(xy)));
d = abs(d - d.');
n = size(d,1);
% Replace zeros along diagonal with ones (so these don't show up in the
% find below or in the Green's function calculation).
d(1:n+1:numel(d)) = ones(1,n);
non = find(d == 0);
if ~isempty(non),
% If we've made it to here, then some points aren't distinct. Remove
% the non-distinct points by averaging.
[r,c] = find(d == 0);
k = find(r < c);
r = r(k); c = c(k); % Extract unique (row,col) pairs
v = (z(r) + z(c))/2; % Average non-distinct pairs
rep = find(diff(c)==0);
if ~isempty(rep), % More than two points need to be averaged.
runs = find(diff(diff(c)==0)==1)+1;
for i=1:length(runs),
k = find(c==c(runs(i))); % All the points in a run
v(runs(i)) = mean(z([r(k);c(runs(i))])); % Average (again)
end
end
z(r) = v;
if ~isempty(rep),
z(r(runs)) = v(runs); % Make sure average is in the dataset
end
% Now remove the extra points.
x(c) = [];
y(c) = [];
z(c) = [];
xy(c,:) = [];
xy(:,c) = [];
d(c,:) = [];
d(:,c) = [];
% Determine the non distinct points
ndp = sort([r;c]);
ndp(find(ndp(1:length(ndp)-1)==ndp(2:length(ndp)))) = [];
warning('MATLAB:griddata:NonDistinctPoints',['Averaged %d non-distinct ' ...
'points.\n Indices are: %s.'],length(ndp),num2str(ndp'))
end
% Determine weights for interpolation
g = (d.^2) .* (log(d)-1); % Green's function.
% Fixup value of Green's function along diagonal
g(1:size(d,1)+1:numel(d)) = zeros(size(d,1),1);
weights = g \ z(:);
[m,n] = size(xi);
zi = zeros(size(xi));
jay = sqrt(-1);
xy = xy.';
% Evaluate at requested points (xi,yi). Loop to save memory.
for i=1:m
for j=1:n
d = abs(xi(i,j)+jay*yi(i,j) - xy);
mask = find(d == 0);
if length(mask)>0, d(mask) = ones(length(mask),1); end
g = (d.^2) .* (log(d)-1); % Green's function.
% Value of Green's function at zero
if length(mask)>0, g(mask) = zeros(length(mask),1); end
zi(i,j) = g * weights;
end
end
if nargout<=1,
xi = zi;
end