pmm  1.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
pmm_griddatan.m
Go to the documentation of this file.
1 ## Copyright (C) 2007 David Bateman
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
18 
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {@var{yi} =} pmm_griddatan (@var{x}, @var{y}, @var{xi}, @var{method}, @var{options})
21 ##
22 ## Generate a regular mesh from irregular data using interpolation.
23 ## The function is defined by @code{@var{y} = f (@var{x})}.
24 ## The interpolation points are all @var{xi}.
25 ##
26 ## The interpolation method can be @code{"nearest"} or @code{"linear"}.
27 ## If method is omitted it defaults to @code{"linear"}.
28 ## @seealso{griddata, delaunayn}
29 ## @end deftypefn
30 
31 ## Author: David Bateman <dbateman@free.fr>
32 
33 function yi = pmm_griddatan (x, y, xi, method, varargin)
34 
35  if (nargin == 3)
36  method = "linear";
37  endif
38  if (nargin < 3)
39  print_usage ();
40  endif
41 
42  if (ischar (method))
43  method = tolower (method);
44  endif
45 
46  [m, n] = size (x);
47  [mi, ni] = size (xi);
48 
49  if (n != ni || size (y, 1) != m || size (y, 2) != 1)
50  error ("pmm_griddatan: dimensional mismatch");
51  endif
52 
53  ## triangulate data
54  tri = delaunayn(x, varargin{:});
55  ## tri = delaunayn (x);
56 
57  yi = nan (mi, 1);
58 
59  if (strcmp (method, "nearest"))
60  ## search index of nearest point
61  idx = dsearchn (x, tri, xi);
62  valid = !isnan (idx);
63  yi(valid) = y(idx(valid));
64 
65  elseif (strcmp (method, "linear"))
66  ## search for every point the enclosing triangle
67  [tri_list, bary_list] = tsearchn (x, tri, xi);
68 
69  ## only keep the points within triangles.
70  valid = !isnan (tri_list);
71  tri_list = tri_list(!isnan (tri_list));
72  bary_list = bary_list(!isnan (tri_list), :);
73  nr_t = rows (tri_list);
74 
75  ## assign x,y for each point of simplex
76  xt = reshape (x(tri(tri_list,:),:), [nr_t, n+1, n]);
77  yt = y(tri(tri_list,:));
78 
79  ## Use barycentric coordinate of point to calculate yi
80  yi(valid) = sum (y(tri(tri_list,:)) .* bary_list, 2);
81 
82  else
83  error ("pmm_griddatan: unknown interpolation method");
84  endif
85 
86 endfunction
87 
88 function yi = pmm_triinterpn (tri, x, y, xi, method)
89 
90  if (nargin == 4)
91  method = "linear";
92  endif
93  if (nargin < 4)
94  print_usage ();
95  endif
96 
97  if (ischar (method))
98  method = tolower (method);
99  endif
100 
101  [m, n] = size (x);
102  [mi, ni] = size (xi);
103 
104  if (n != ni || size (y, 1) != m || size (y, 2) != 1)
105  error ("pmm_triinterpn: dimensional mismatch");
106  endif
107 
108  yi = nan (mi, 1);
109 
110  if (strcmp (method, "nearest"))
111  ## search index of nearest point
112  idx = dsearchn (x, tri, xi);
113  valid = !isnan (idx);
114  yi(valid) = y(idx(valid));
115 
116  elseif (strcmp (method, "linear"))
117  ## search for every point the enclosing triangle
118  [tri_list, bary_list] = tsearchn (x, tri, xi);
119 
120  ## only keep the points within triangles.
121  valid = !isnan (tri_list);
122  tri_list = tri_list(!isnan (tri_list));
123  bary_list = bary_list(!isnan (tri_list), :);
124  nr_t = rows (tri_list);
125 
126  ## assign x,y for each point of simplex
127  xt = reshape (x(tri(tri_list,:),:), [nr_t, n+1, n]);
128  yt = y(tri(tri_list,:));
129 
130  ## Use barycentric coordinate of point to calculate yi
131  yi(valid) = sum (y(tri(tri_list,:)) .* bary_list, 2);
132 
133  else
134  error ("pmm_triinterpn: unknown interpolation method");
135  endif
136 
137 endfunction
138 
139 %!test
140 %! [xx,yy] = meshgrid(linspace(-1,1,32));
141 %! xi = [xx(:), yy(:)];
142 %! x = (2 * rand(100,2) - 1);
143 %! x = [x;1,1;1,-1;-1,-1;-1,1];
144 %! y = sin(2*(sum(x.^2,2)));
145 %! zz = griddatan(x,y,xi,'linear');
146 %! zz2 = griddata(x(:,1),x(:,2),y,xi(:,1),xi(:,2),'linear');
147 %! assert (zz, zz2, 1e-10)
148 
149 %!test
150 %! [xx,yy] = meshgrid(linspace(-1,1,32));
151 %! xi = [xx(:), yy(:)];
152 %! x = (2 * rand(100,2) - 1);
153 %! x = [x;1,1;1,-1;-1,-1;-1,1];
154 %! y = sin(2*(sum(x.^2,2)));
155 %! zz = griddatan(x,y,xi,'nearest');
156 %! zz2 = griddata(x(:,1),x(:,2),y,xi(:,1),xi(:,2),'nearest');
157 %! assert (zz, zz2, 1e-10)