This is a static copy of a profile reportHome
interp1 (2153 calls, 1.412 sec)
Generated 05-Nov-2014 07:53:25 using cpu time.
function in file /usr1/opt/matlab/7.13/toolbox/matlab/polyfun/interp1.m
Copy to new window for comparing multiple runs
Parents (calling functions)
Lines where the most time was spent
Line Number | Code | Calls | Total Time | % Time | Time Plot |
295 | [~,k] = histc(xiCol,xCol); | 1829 | 0.100 s | 7.1% |  |
281 | yiMat = zeros(numelXi,prodDs,s... | 2153 | 0.070 s | 5.0% |  |
236 | if any(~isfinite(xCol)) | 2153 | 0.070 s | 5.0% |  |
355 | yiMat(p(outOfBounds),:) = extr... | 2153 | 0.040 s | 2.8% |  |
354 | outOfBounds = xiCol<xCol(1)... | 2153 | 0.040 s | 2.8% |  |
All other lines | | | 1.092 s | 77.3% |  |
Totals | | | 1.412 s | 100% | |
Children (called functions)
Function Name | Function Type | Calls | Total Time | % Time | Time Plot |
double.superiorfloat | function | 2153 | 0.020 s | 1.4% |  |
Self time (built-ins, overhead, etc.) | | | 1.392 s | 98.6% |  |
Totals | | | 1.412 s | 100% | |
Code Analyzer results
No Code Analyzer messages.Coverage results
[ Show coverage for parent directory ]
Total lines in function | 418 |
Non-code lines (comments, blank lines) | 206 |
Code lines (lines that can run) | 212 |
Code lines that did run | 105 |
Code lines that did not run | 107 |
Coverage (did run/can run) | 49.53 % |
Function listing
time calls line
1 function varargout = interp1(varargin)
2 %INTERP1 1-D interpolation (table lookup)
3 % YI = INTERP1(X,Y,XI) interpolates to find YI, the values of the
4 % underlying function Y at the points in the array XI. X must be a
5 % vector of length N.
6 % If Y is a vector, then it must also have length N, and YI is the
7 % same size as XI. If Y is an array of size [N,D1,D2,...,Dk], then
8 % the interpolation is performed for each D1-by-D2-by-...-Dk value
9 % in Y(i,:,:,...,:).
10 % If XI is a vector of length M, then YI has size [M,D1,D2,...,Dk].
11 % If XI is an array of size [M1,M2,...,Mj], then YI is of size
12 % [M1,M2,...,Mj,D1,D2,...,Dk].
13 %
14 % YI = INTERP1(Y,XI) assumes X = 1:N, where N is LENGTH(Y)
15 % for vector Y or SIZE(Y,1) for array Y.
16 %
17 % Interpolation is the same operation as "table lookup". Described in
18 % "table lookup" terms, the "table" is [X,Y] and INTERP1 "looks-up"
19 % the elements of XI in X, and, based upon their location, returns
20 % values YI interpolated within the elements of Y.
21 %
22 % YI = INTERP1(X,Y,XI,METHOD) specifies alternate methods.
23 % The default is linear interpolation. Use an empty matrix [] to specify
24 % the default. Available methods are:
25 %
26 % 'nearest' - nearest neighbor interpolation
27 % 'linear' - linear interpolation
28 % 'spline' - piecewise cubic spline interpolation (SPLINE)
29 % 'pchip' - shape-preserving piecewise cubic interpolation
30 % 'cubic' - same as 'pchip'
31 % 'v5cubic' - the cubic interpolation from MATLAB 5, which does not
32 % extrapolate and uses 'spline' if X is not equally
33 % spaced.
34 %
35 % YI = INTERP1(X,Y,XI,METHOD,'extrap') uses the interpolation algorithm
36 % specified by METHOD to perform extrapolation for elements of XI outside
37 % the interval spanned by X.
38 %
39 % YI = INTERP1(X,Y,XI,METHOD,EXTRAPVAL) replaces the values outside of the
40 % interval spanned by X with EXTRAPVAL. NaN and 0 are often used for
41 % EXTRAPVAL. The default extrapolation behavior with four input arguments
42 % is 'extrap' for 'spline' and 'pchip' and EXTRAPVAL = NaN for the other
43 % methods.
44 %
45 % PP = INTERP1(X,Y,METHOD,'pp') will use the interpolation algorithm specified
46 % by METHOD to generate the ppform (piecewise polynomial form) of Y. The
47 % method may be any of the above METHOD except for 'v5cubic'. PP may then
48 % be evaluated via PPVAL. PPVAL(PP,XI) is the same as
49 % INTERP1(X,Y,XI,METHOD,'extrap').
50 %
51 % For example, generate a coarse sine curve and interpolate over a
52 % finer abscissa:
53 % x = 0:10; y = sin(x); xi = 0:.25:10;
54 % yi = interp1(x,y,xi); plot(x,y,'o',xi,yi)
55 %
56 % For a multi-dimensional example, we construct a table of functional
57 % values:
58 % x = [1:10]'; y = [ x.^2, x.^3, x.^4 ];
59 % xi = [ 1.5, 1.75; 7.5, 7.75]; yi = interp1(x,y,xi);
60 %
61 % creates 2-by-2 matrices of interpolated function values, one matrix for
62 % each of the 3 functions. yi will be of size 2-by-2-by-3.
63 %
64 % Class support for inputs X, Y, XI, EXTRAPVAL:
65 % float: double, single
66 %
67 % See also INTERP1Q, INTERPFT, SPLINE, PCHIP, INTERP2, INTERP3, INTERPN, PPVAL.
68
69 % Copyright 1984-2011 The MathWorks, Inc.
70 % $Revision: 5.41.4.18 $ $Date: 2011/05/17 02:32:26 $
71 %
72 % Determine input arguments.
73
74 % The following syntaxes do not have X as the first input:
75 % INTERP1(Y,XI)
76 % INTERP1(Y,XI,METHOD)
77 % INTERP1(Y,XI,METHOD,'extrap')
78 % INTERP1(Y,XI,METHOD,EXTRAPVAL)
0.01 2153 79 xOffset = 1;
0.03 2153 80 if (nargin==2) || ...
81 (nargin==3 && ischar(varargin{3})) || ...
82 (nargin==4 && ~(ischar(varargin{4}) || isempty(varargin{4})) || ...
83 (nargin==4 && strcmp(varargin{4}, 'extrap')));
84 xOffset = 0;
85 end
86
2153 87 ppOutput = false;
88 % PP = INTERP1(X,Y,METHOD,'pp')
0.02 2153 89 if nargin>=4 && ischar(varargin{3}) && isequal('pp',varargin{4})
90 ppOutput = true;
91 if (nargin > 4)
92 error(message('MATLAB:interp1:ppOutput'))
93 end
94 end
95
96 % Process Y in INTERP1(Y,...) and INTERP1(X,Y,...)
2153 97 y = varargin{1+xOffset};
0.01 2153 98 siz_y = size(y);
99 % y may be an ND array, but collapse it down to a 2D yMat. If yMat is
100 % a vector, it is a column vector.
0.04 2153 101 if isvector(y)
2153 102 if isrow(y)
103 % Prefer column vectors for y
432 104 yMat = y.';
432 105 n = siz_y(2);
0.04 1721 106 else
1721 107 yMat = y;
1721 108 n = siz_y(1);
1721 109 end
2153 110 ds = 1;
2153 111 prodDs = 1;
112 else
113 n = siz_y(1);
114 ds = siz_y(2:end);
115 prodDs = prod(ds);
116 yMat = reshape(y,[n prodDs]);
117 end
118
119 % Process X in INTERP1(X,Y,...), or supply default for INTERP1(Y,...)
2153 120 if xOffset
2153 121 x = varargin{xOffset};
0.01 2153 122 if ~isvector(x)
123 error(message('MATLAB:interp1:Xvector'));
124 end
0.02 2153 125 if length(x) ~= n
126 if isvector(y)
127 error(message('MATLAB:interp1:YVectorInvalidNumRows'))
128 else
129 error(message('MATLAB:interp1:YInvalidNumRows'));
130 end
131 end
132 % Prefer column vectors for x
2153 133 xCol = x(:);
134 else
135 xCol = (1:n)';
136 end
137
138 % Process XI in INTERP1(Y,XI,...) and INTERP1(X,Y,XI,...)
139 % Avoid syntax PP = INTERP1(X,Y,METHOD,'pp')
0.03 2153 140 if ~ppOutput
2153 141 xi = varargin{2+xOffset};
2153 142 siz_xi = size(xi);
143 % xi may be an ND array, but flatten it to a column vector xiCol
2153 144 xiCol = xi(:);
145 % The size of the output YI
2153 146 if isvector(y)
147 % Y is a vector so size(YI) == size(XI)
0.02 2153 148 siz_yi = siz_xi;
149 else
150 if isvector(xi)
151 % Y is not a vector but XI is
152 siz_yi = [length(xi) ds];
153 else
154 % Both Y and XI are non-vectors
155 siz_yi = [siz_xi ds];
156 end
157 end
2153 158 end
159
0.02 2153 160 if xOffset && ~isreal(x)
161 error(message('MATLAB:interp1:ComplexX'))
162 end
163
2153 164 if ~ppOutput && ~isreal(xi)
165 error(message('MATLAB:interp1:ComplexInterpPts'))
166 end
167
168 % Error check for NaN values in X and Y
169 % check for NaN's
0.03 2153 170 if xOffset && (any(isnan(xCol)))
171 error(message('MATLAB:interp1:NaNinX'));
172 end
173
174 % NANS are allowed as a value for F(X), since a function may be undefined
175 % for a given value.
2153 176 if any(isnan(yMat(:)))
177 warning(message('MATLAB:interp1:NaNinY'));
178 end
179
2153 180 if (n < 2)
181 if ppOutput || ~isempty(xi)
182 error(message('MATLAB:interp1:NotEnoughPts'))
183 else
184 yi = zeros(siz_yi,superiorfloat(x,y,xi));
185 varargout{1} = yi;
186 return
187 end
188 end
189
190 % Process METHOD in
191 % PP = INTERP1(X,Y,METHOD,'pp')
192 % YI = INTERP1(Y,XI,METHOD,...)
193 % YI = INTERP1(X,Y,XI,METHOD,...)
194 % including explicit specification of the default by an empty input.
2153 195 if ppOutput
196 if isempty(varargin{3})
197 method = 'linear';
198 else
199 method = varargin{3};
200 end
2153 201 else
0.01 2153 202 if nargin >= 3+xOffset && ~isempty(varargin{3+xOffset})
203 method = varargin{3+xOffset};
0.02 2153 204 else
0.03 2153 205 method = 'linear';
0.01 2153 206 end
0.01 2153 207 end
208
209 % The v5 option, '*method', asserts that x is equally spaced.
0.01 2153 210 eqsp = (method(1) == '*');
2153 211 if eqsp
212 method(1) = [];
213 end
214
215 % INTERP1([X,]Y,XI,METHOD,'extrap') and INTERP1([X,]Y,Xi,METHOD,EXTRAPVAL)
2153 216 if ~ppOutput
0.01 2153 217 if nargin >= 4+xOffset
218 extrapval = varargin{4+xOffset};
2153 219 else
0.03 2153 220 switch method(1)
0.02 2153 221 case {'s','p','c'}
222 extrapval = 'extrap';
0.01 2153 223 otherwise
2153 224 extrapval = NaN;
0.01 2153 225 end
2153 226 end
2153 227 end
228
229 % Start the algorithm
230 % We now have column vector xCol, column vector or 2D matrix yMat and
231 % column vector xiCol.
2153 232 if xOffset
0.02 2153 233 if ~eqsp
0.02 2153 234 h = diff(xCol);
0.03 2153 235 eqsp = (norm(diff(h),Inf) <= eps(norm(xCol,Inf)));
0.07 2153 236 if any(~isfinite(xCol))
237 eqsp = 0; % if an INF in x, x is not equally spaced
238 end
0.03 2153 239 end
0.02 2153 240 if eqsp
324 241 h = (xCol(n)-xCol(1))/(n-1);
324 242 end
243 else
244 h = 1;
245 eqsp = 1;
246 end
0.02 2153 247 if any(h < 0)
248 [xCol,p] = sort(xCol);
249 yMat = yMat(p,:);
250 if eqsp
251 h = -h;
252 else
253 h = diff(xCol);
254 end
255 end
0.04 2153 256 if any(h == 0)
257 error(message('MATLAB:interp1:RepeatedValuesX'));
258 end
259
260 % PP = INTERP1(X,Y,METHOD,'pp')
0.02 2153 261 if nargin==4 && ischar(varargin{3}) && isequal('pp',varargin{4})
262 % obtain pp form of output
263 pp = ppinterp;
264 varargout{1} = pp;
265 return
266 end
267
268 % Interpolate
0.02 2153 269 numelXi = length(xiCol);
2153 270 p = [];
0.02 2153 271 switch method(1)
0.01 2153 272 case 's' % 'spline'
273 % spline is oriented opposite to interp1
274 yiMat = spline(xCol.',yMat.',xiCol.').';
275
0.02 2153 276 case {'c','p'} % 'cubic' or 'pchip'
277 % pchip is oriented opposite to interp1
278 yiMat = pchip(xCol.',yMat.',xiCol.').';
279
2153 280 otherwise % 'nearest', 'linear', 'v5cubic'
0.07 2153 281 yiMat = zeros(numelXi,prodDs,superiorfloat(xCol,yMat,xiCol));
0.02 2153 282 if ~eqsp && any(diff(xiCol) < 0)
283 [xiCol,p] = sort(xiCol);
0.01 2153 284 else
0.01 2153 285 p = 1:numelXi;
0.01 2153 286 end
287
288 % Find indices of subintervals, x(k) <= u < x(k+1),
289 % or u < x(1) or u >= x(m-1).
0.02 2153 290 if isempty(xiCol)
291 k = xiCol;
2153 292 elseif eqsp
0.01 324 293 k = min(max(1+floor((xiCol-xCol(1))/h),1),n-1);
0.01 1829 294 else
0.10 1829 295 [~,k] = histc(xiCol,xCol);
0.03 1829 296 k(xiCol<xCol(1) | ~isfinite(xiCol)) = 1;
0.01 1829 297 k(xiCol>=xCol(n)) = n-1;
0.01 1829 298 end
299
0.01 2153 300 switch method(1)
0.02 2153 301 case 'n' % 'nearest'
302 i = find(xiCol >= (xCol(k)+xCol(k+1))/2);
303 k(i) = k(i)+1;
304 yiMat(p,:) = yMat(k,:);
305 nanidx = find(isnan(xiCol));
306 yiMat(p(nanidx),:) = yiMat(p(nanidx),:)*NaN;
0.01 2153 307 case 'l' % 'linear'
2153 308 if eqsp
324 309 s = (xiCol - xCol(k))/h;
1829 310 else
0.04 1829 311 s = (xiCol - xCol(k))./h(k);
0.01 1829 312 end
0.01 2153 313 for j = 1:prodDs
0.03 2153 314 yiMat(p,j) = yMat(k,j) + s.*(yMat(k+1,j)-yMat(k,j));
2153 315 end
316
317 case 'v' % 'v5cubic'
318 extrapval = NaN;
319 if eqsp
320 % Data are equally spaced
321 s = (xiCol - xCol(k))/h;
322 s2 = s.*s;
323 s3 = s.*s2;
324 % Add extra points for first and last interval
325 yMat = [3*yMat(1,:)-3*yMat(2,:)+yMat(3,:); ...
326 yMat; ...
327 3*yMat(n,:)-3*yMat(n-1,:)+yMat(n-2,:)];
328 for j = 1:prodDs
329 yiMat(p,j) = (yMat(k,j).*(-s3+2*s2-s) + ...
330 yMat(k+1,j).*(3*s3-5*s2+2) + ...
331 yMat(k+2,j).*(-3*s3+4*s2+s) + ...
332 yMat(k+3,j).*(s3-s2))/2;
333 end
334 else
335 % Data are not equally spaced
336 % spline is oriented opposite to interp1
337 yiMat = spline(xCol.',yMat.',xiCol.').';
338 end
339 otherwise
340 error(message('MATLAB:interp1:InvalidMethod'))
341 end
2153 342 end
343
344 % Override extrapolation
0.01 2153 345 if ~isequal(extrapval,'extrap')
2153 346 if ischar(extrapval)
347 error(message('MATLAB:interp1:InvalidExtrap'))
2153 348 elseif ~isscalar(extrapval)
349 error(message('MATLAB:interp1:NonScalarExtrapValue'))
350 end
0.01 2153 351 if isempty(p)
352 p = 1 : numelXi;
353 end
0.04 2153 354 outOfBounds = xiCol<xCol(1) | xiCol>xCol(n);
0.04 2153 355 yiMat(p(outOfBounds),:) = extrapval;
2153 356 end
357
358 % Reshape result, possibly to an ND array
2153 359 yi = reshape(yiMat,siz_yi);
0.02 2153 360 varargout{1} = yi;
361
362 %-------------------------------------------------------------------------%
363 function pp = ppinterp
364 %PPINTERP ppform interpretation.
365
366 switch method(1)
367 case 'n' % nearest
368 breaks = [xCol(1); ...
369 (xCol(1:end-1)+xCol(2:end))/2; ...
370 xCol(end)].';
371 coefs = yMat.';
372 pp = mkpp(breaks,coefs,ds);
373 case 'l' % linear
374 breaks = xCol.';
375 page1 = (diff(yMat)./repmat(diff(xCol),[1, prodDs])).';
376 page2 = (reshape(yMat(1:end-1,:),[n-1, prodDs])).';
377 coefs = cat(3,page1,page2);
378 pp = mkpp(breaks,coefs,ds);
379 case {'p', 'c'} % pchip and cubic
380 pp = pchip(xCol.',reshape(yMat.',[ds, n]));
381 case 's' % spline
382 pp = spline(xCol.',reshape(yMat.',[ds, n]));
383 case 'v' % v5cubic
384 b = diff(xCol);
385 if norm(diff(b),Inf) <= eps(norm(xCol,Inf))
386 % data are equally spaced
387 a = repmat(b,[1 prodDs]).';
388 yReorg = [3*yMat(1,:)-3*yMat(2,:)+yMat(3,:); ...
389 yMat; ...
390 3*yMat(n,:)-3*yMat(n-1,:)+yMat(n-2,:)];
391 y1 = yReorg(1:end-3,:).';
392 y2 = yReorg(2:end-2,:).';
393 y3 = yReorg(3:end-1,:).';
394 y4 = yReorg(4:end,:).';
395 breaks = xCol.';
396 page1 = (-y1+3*y2-3*y3+y4)./(2*a.^3);
397 page2 = (2*y1-5*y2+4*y3-y4)./(2*a.^2);
398 page3 = (-y1+y3)./(2*a);
399 page4 = y2;
400 coefs = cat(3,page1,page2,page3,page4);
401 pp = mkpp(breaks,coefs,ds);
402 else
403 % data are not equally spaced
404 pp = spline(xCol.',reshape(yMat.',[ds, n]));
405 end
406 otherwise
407 error(message('MATLAB:interp1:ppinterp:UnknownMethod'));
408 end
409
410 % Even if method is 'spline' or 'pchip', we still need to record that the
411 % input data Y was oriented according to INTERP1's rules.
412 % Thus PPVAL will return YI oriented according to INTERP1's rules and
413 % YI = INTERP1(X,Y,XI,METHOD) will be the same as
414 % YI = PPVAL(INTERP1(X,Y,METHOD,'pp'),XI)
415 pp.orient = 'first';
416 end % PPINTERP
417
0.02 2153 418 end % INTERP1