home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / POLYFUN.DI$ / INTERP4.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  2.9 KB  |  85 lines

  1. function F = interp4(x,y,z,u,v)
  2. %INTERP4 2-D bilinear data interpolation.
  3. %    ZI = INTERP4(X,Y,Z,XI,YI) uses bilinear interpolation to
  4. %    find ZI, the values of the underlying 2-D function in Z at the points
  5. %    in matrices XI and YI.  Matrices X and Y specify the points at which 
  6. %    the data Z is given.  X and Y can also be vectors specifying the 
  7. %    abscissae for the matrix Z as for MESHGRID. In both cases, X
  8. %    and Y must be equally spaced and monotonic.
  9. %
  10. %    Values of NaN are returned in ZI for values of XI and YI that are 
  11. %    outside of the range of X and Y.
  12. %
  13. %    If XI and YI are vectors, INTERP4 returns vector ZI containing
  14. %    the interpolated values at the corresponding points (XI,YI).
  15. %
  16. %    ZI = INTERP4(Z,XI,YI) assumes X = 1:N and Y = 1:M, where
  17. %    [M,N] = SIZE(Z).
  18. %
  19. %    F = INTERP4(Z,NTIMES) returns the matrix Z expanded by interleaving
  20. %    bilinear interpolates between every element, working recursively
  21. %    for NTIMES.  INTERP4(Z) is the same as INTERP4(Z,1).
  22. %
  23. %    See also INTERP5, INTERP3.
  24.  
  25. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  26. %    Clay M. Thompson 4-26-91, revised 7-3-91 by CMT.
  27.  
  28. if nargin==1, % Expand Z
  29.   z = x;
  30.   [nrows,ncols] = size(z);
  31.   u = ones(2*nrows-1,1)*[1:.5:ncols];
  32.   v = [1:.5:nrows]'*ones(1,2*ncols-1);
  33.  
  34. elseif nargin==2, % Expand Z ntimes
  35.   z = x;
  36.   ntimes = floor(y);
  37.   [nrows,ncols] = size(z);
  38.   u = ones(2*ntimes*(nrows-1)+1,1)*[1:1/(2*ntimes):ncols];
  39.   v = [1:1/(2*ntimes):nrows]'*ones(1,2*ntimes*(ncols-1)+1);
  40.  
  41. elseif nargin==3, % No X or Y specified.
  42.   v = z; u = y; z = x;
  43.   [nrows,ncols] = size(z);
  44.  
  45. elseif nargin==5, % X and Y specified.
  46.   [nrows,ncols] = size(z);
  47.   mx = prod(size(x)); my = prod(size(y));
  48.   if any([mx my] ~= [ncols nrows]) & (size(x)~=size(z) | size(y)~=size(z)), 
  49.     error('The lengths of the X and Y vectors must match Z.');
  50.   end
  51.   u = 1 + (u-x(1))*((ncols-1)/(x(mx)-x(1)));
  52.   v = 1 + (v-y(1))*((nrows-1)/(y(my)-y(1)));
  53.   
  54. else
  55.   error('Wrong number of input arguments.');
  56. end
  57.  
  58. if any(size(z)<[3 3]), error('Z must be at least 3-by-3.'); end
  59. if size(u)~=size(v), error('XI and YI must be the same size.'); end
  60.  
  61. % Check for out of range values of u and set to 1
  62. uout = (u<1)|(u>ncols);
  63. nuout = sum(uout(:));
  64. if any(uout(:)), u(uout) = ones(nuout,1); end
  65.  
  66. % Check for out of range values of v and set to 1
  67. vout = (v<1)|(v>nrows);
  68. nvout = sum(vout(:));
  69. if any(vout(:)), v(vout) = ones(nvout,1); end
  70.  
  71. % Interpolation parameters
  72. s = (u - floor(u));  t = (v - floor(v));
  73. u = floor(u); v = floor(v);
  74. d = (u==ncols); if any(d(:)), u(d) = u(d)-1; s(d) = s(d)+1; end
  75. d = (v==nrows); if any(d(:)), v(d) = v(d)-1; t(d) = t(d)+1; end
  76.  
  77. % Now interpolate
  78. ndx = v+(u-1)*nrows;
  79. F =  ( z(ndx).*(1-t) + z(ndx+1).*t ).*(1-s) + ...
  80.      ( z(ndx+nrows).*(1-t) + z(ndx+(nrows+1)).*t ).*s;
  81.  
  82. % Now set out of range values to NaN.
  83. if any(uout(:)), F(uout) = NaN*ones(nuout,1); end
  84. if any(vout(:)), F(vout) = NaN*ones(nvout,1); end
  85.