-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathflexinterpnw_method.m
160 lines (147 loc) · 5.11 KB
/
flexinterpnw_method.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
function [id, iw] = flexinterpnw_method(data, dw, cr, varargin)
% flexinterpnw_method - call flexinterpnw with one of the typical methods
%
% FORMAT: [id, iw] = flexinterpnw_method(d, dw, cr [, [opts], method])
%
% Input fields:
%
% d N-d data (up to 4 dims supported)
% dw N-d weights (double, same size as data)
% cr CxD coordinates or 4xD range
% opts hold (and tranformation matrix)
% method either of:
% 'cubic', 'lanczos2' 'lanczos3', {'linear'},
% 'nearest', 'poly3', 'spline2', 'spline3'
%
% Output fields:
%
% id interpolated data as from flexinterpn
% iw interpolated weights
%
% Note: the range notation of cr must contain Infs in the first row
% of values, the rest is parsed as row2:row3:row4 for each dim.
%
% Note: for methods other than nearest and linear, the value range of
% the input can be exceeded!
% Version: v0.9c
% Build: 11120715
% Date: Dec-07 2011, 3:36 PM EST
% Author: Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://neuroelf.net/
% Copyright (c) 2010, 2011, Jochen Weber
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
% * Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
% * Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in the
% documentation and/or other materials provided with the distribution.
% * Neither the name of Columbia University nor the
% names of its contributors may be used to endorse or promote products
% derived from this software without specific prior written permission.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
% ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
% WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
% DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
% DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
% (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
% ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
% (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
% Note: general information on kernel math taken from
% http://www.all-in-one.ee/~dersch/interpolator/interpolator.html
% compute kernels in persistent memory
persistent i_fwimkernel;
if isempty(i_fwimkernel)
i_fwimkernel = flexinterpn_method('kernels');
end
% argument check
if nargin < 3 || ...
~isnumeric(data) || ...
~isa(dw, 'double') || ...
~isequal(size(data), size(dw)) || ...
~isnumeric(cr)
error( ...
'neuroelf:BadArgument', ...
'Invalid argument.' ...
);
end
narg = nargin;
if narg < 4 || ...
~ischar(varargin{end}) || ...
isempty(regexpi(varargin{end}(:)', '^(cubic|lanczos[2-9]|linear|nearest|poly3|spline[23])$'))
method = 'linear';
else
method = lower(varargin{end}(:)');
narg = narg - 1;
end
if narg > 4 && ...
ischar(varargin{end-1}) && ...
strcmpi(varargin{end-1}(:)', 'gauss')
if numel(varargin{end}) == 1 && ...
isa(varargin{end}, 'double') && ...
~isnan(varargin{end}) && ...
varargin{end} >= 0.25 && ...
varargin{end} <= 8
gaussk = varargin{end};
else
gaussk = 2;
end
method = 'gauss';
narg = narg - 2;
end
% special case, nearest neighbor without rotation, up to 3D
if strcmp(method, 'nearest') && ...
(narg < 4 || ...
(narg == 4 && ...
numel(varargin{1}) == 1)) && ...
ndims(data) < 4
% use indexarray
try
id = indexarraynb(data .* dw, cr);
iw = indexarraynb(dw, cr);
id = id ./ iw;
id(iw == 0) = 0;
catch ne_eo;
rethrow(ne_eo);
end
return;
end
% depending on method
if ~strcmp(method, 'gauss')
k = i_fwimkernel.(method);
% gaussian smoothing while interpolating
else
f = gaussk / sqrt(8 * log(2));
md = round(6 * f);
k = {exp(- (-md:1/1024:md)' .^ 2 ./ (2 * f .^ 2)), 1024};
k{1}([1, numel(k{1})]) = 0;
end
% special case, rotation of 2D data
if ndims(data) == 2 && ...
isequal(size(cr), [4, 2]) && ...
all(isinf(cr(1, :))) && ...
narg > 4 && ...
isa(varargin{2}, 'double') && ...
isequal(size(varargin{2}), [4, 4])
% perform this in 3D!
data = reshape(data, [1, size(data)]);
cr = [[Inf;1;1;1], cr];
% pass on and return
try
[id, iw] = shiftdim(flexinterpnw(data, dw, cr, k{:}, varargin{1:narg-3}), 1);
catch ne_eo;
rethrow(ne_eo);
end
return;
end
% now pass this on
try
[id, iw] = flexinterpnw(data, dw, cr, k{:}, varargin{1:narg-3});
catch ne_eo;
rethrow(ne_eo);
end