]> Creatis software - CreaPhase.git/blob - octave_packages/optiminterp-0.3.3/optiminterpn.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / optiminterp-0.3.3 / optiminterpn.m
1 ## Copyright (C) 2008 Alexander Barth
2 ##
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 ## GNU General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Loadable Function} {[@var{fi},@var{vari}] = } optiminterpn(@var{x},@var{y},@var{...},@var{f},@var{var},@var{lenx},@var{leny},@var{...},@var{m},@var{xi},@var{yi},@var{...})
18 ## Performs a local nD-optimal interpolation (objective analysis).
19 ##
20 ## Every elements in @var{f} corresponds to a data point (observation)
21 ## at location @var{x},@var{y},@var{...} with the error variance @var{var}.
22 ##
23 ## @var{lenx},@var{leny},@var{...} are correlation length in x-direction
24 ## y-direction,... respectively. 
25 ## @var{m} represents the number of influential points.
26 ##
27 ## @var{xi},@var{yi},@var{...} are the data points where the field is
28 ## interpolated. @var{fi} is the interpolated field and @var{vari} is 
29 ## its error variance.
30 ##
31 ## The background field of the optimal interpolation is zero.
32 ## For a different background field, the background field
33 ## must be subtracted from the observation, the difference 
34 ## is mapped by OI onto the background grid and finally the
35 ## background is added back to the interpolated field.
36 ## The error variance of the background field is assumed to 
37 ## have a error variance of one.
38 ## @end deftypefn
39
40 ## Copyright (C) 2008, Alexander Barth
41 ## Author: Alexander Barth <barth.alexander@gmail.com>
42
43 # {[@var{fi},@var{vari}] = } optiminterpn(@var{x},@var{y},@var{...},@var{f},@var{var},@var{lenx},@var{leny},@var{len...},@var{m},@var{xi},@var{yi},@var{...})
44
45 function [fi,vari] = optiminterpn(varargin)
46
47 if nargin < 6 || mod(nargin-3,3) ~= 0
48   error('optiminterpn: wrong number of arguments');
49 end
50
51 n = (nargin-3)/3;
52
53 x =  varargin{1};
54 xi = varargin{2*n+4};
55 on = numel(varargin{1});
56 gsz = size(varargin{2*n+4});
57
58 for i=1:n
59   tmp = varargin{i};
60
61   if on ~= numel(tmp)
62     error('optiminterpn: x, y,... must have the same number of elements');
63   end
64
65   ox(:,i) = tmp(:); 
66
67   len(i) = varargin{n+i+2};
68   tmp = varargin{2*n+3+i};
69
70   if (numel(tmp) ~= prod(gsz))
71     error('optiminterpn: xi, yi, ... must have the same number of elements');
72   end
73
74   gx(:,i) = tmp(:);
75 end
76
77 f = varargin{n+1};    
78 var = varargin{n+2};    
79
80 m = varargin{2*n+3};    
81
82 if (isscalar(var))
83   var = var*ones(size(x));
84 end
85
86 if isvector(f) && size(f,1) == 1
87    f = f';
88 end
89
90 % is this correct?
91 nf = size(f,n+1);
92
93 if (on*nf ~= numel(f) && on ~= numel(var))
94   error('optiminterpn: x,y,...,var must have the same number of elements');
95 end
96
97 f=reshape(f,[on nf]);
98
99 %whos ox f var len m gx
100 [fi,vari] = optiminterp(ox,f,var,len,m,gx);
101
102 fi = reshape(fi,[gsz nf]);
103 vari = reshape(vari,gsz);