Mercurial > hg > octave-nkf
annotate scripts/geometry/griddatan.m @ 8203:a9da991c77aa
update contrib.txi
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 08 Oct 2008 14:29:51 -0400 |
parents | ec0a13863eb7 |
children | eb63fbe60fab |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 2007 David Bateman |
6823 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
6823 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
6823 | 18 |
19 ## -*- texinfo -*- | |
20 ## @deftypefn {Function File} {@var{yi} =} 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 ## | |
6826 | 26 ## The interpolation method can be @code{"nearest"} or @code{"linear"}. |
27 ## If method is omitted it defaults to @code{"linear"}. | |
6823 | 28 ## @seealso{griddata, delaunayn} |
29 ## @end deftypefn | |
30 | |
7017 | 31 ## Author: David Bateman <dbateman@free.fr> |
32 | |
6826 | 33 function yi = griddatan (x, y, xi, method, varargin) |
34 | |
6823 | 35 if (nargin == 3) |
6826 | 36 method = "linear"; |
6823 | 37 endif |
38 if (nargin < 3) | |
6826 | 39 print_usage (); |
6823 | 40 endif |
41 | |
6826 | 42 if (ischar (method)) |
43 method = tolower (method); | |
6823 | 44 endif |
45 | |
6826 | 46 [m, n] = size (x); |
47 [mi, ni] = size (xi); | |
6823 | 48 |
6826 | 49 if (n != ni || size (y, 1) != m || size (y, 2) != 1) |
6823 | 50 error ("griddatan: dimensional mismatch"); |
51 endif | |
52 | |
53 ## triangulate data | |
54 ## tri = delaunayn(x, varargin{:}); | |
6826 | 55 tri = delaunayn (x); |
6823 | 56 |
6826 | 57 yi = nan (mi, 1); |
6823 | 58 |
6826 | 59 if (strcmp (method, "nearest")) |
6823 | 60 ## search index of nearest point |
6826 | 61 idx = dsearchn (x, tri, xi); |
62 valid = !isnan (idx); | |
6823 | 63 yi(valid) = y(idx(valid)); |
64 | |
6826 | 65 elseif (strcmp (method, "linear")) |
6823 | 66 ## search for every point the enclosing triangle |
6826 | 67 [tri_list, bary_list] = tsearchn (x, tri, xi); |
6823 | 68 |
69 ## only keep the points within triangles. | |
6826 | 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); | |
6823 | 74 |
75 ## assign x,y for each point of simplex | |
6826 | 76 xt = reshape (x(tri(tri_list,:),:), [nr_t, n+1, n]); |
6823 | 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 | |
6826 | 83 error ("griddatan: unknown interpolation method"); |
6823 | 84 endif |
85 | |
86 endfunction | |
87 | |
8153
ec0a13863eb7
Only run tests that depend on HDF5 and QHull if Octave was actually
Soren Hauberg <hauberg@gmail.com>
parents:
7017
diff
changeset
|
88 %!testif HAVE_QHULL |
6823 | 89 %! [xx,yy] = meshgrid(linspace(-1,1,32)); |
90 %! xi = [xx(:), yy(:)]; | |
91 %! x = (2 * rand(100,2) - 1); | |
92 %! x = [x;1,1;1,-1;-1,-1;-1,1]; | |
93 %! y = sin(2*(sum(x.^2,2))); | |
94 %! zz = griddatan(x,y,xi,'linear'); | |
95 %! zz2 = griddata(x(:,1),x(:,2),y,xi(:,1),xi(:,2),'linear'); | |
96 %! assert (zz, zz2, 1e-10) | |
97 | |
8153
ec0a13863eb7
Only run tests that depend on HDF5 and QHull if Octave was actually
Soren Hauberg <hauberg@gmail.com>
parents:
7017
diff
changeset
|
98 %!testif HAVE_QHULL |
6823 | 99 %! [xx,yy] = meshgrid(linspace(-1,1,32)); |
100 %! xi = [xx(:), yy(:)]; | |
101 %! x = (2 * rand(100,2) - 1); | |
102 %! x = [x;1,1;1,-1;-1,-1;-1,1]; | |
103 %! y = sin(2*(sum(x.^2,2))); | |
104 %! zz = griddatan(x,y,xi,'nearest'); | |
105 %! zz2 = griddata(x(:,1),x(:,2),y,xi(:,1),xi(:,2),'nearest'); | |
106 %! assert (zz, zz2, 1e-10) |