Using MATLAB (779505), страница 45
Текст из файла (страница 45)
Alternatively,yi = interp1(x,Y,xi,method,extrapval) replaces extrapolated values withextrapval. NaN is often used for extrapval.All methods work with nonuniformly spaced data.Speed, Memory, and Smoothness ConsiderationsWhen choosing an interpolation method, keep in mind that some require morememory or longer computation time than others. However, you may need totrade off these resources to achieve the desired smoothness in the result.• Nearest neighbor interpolation is the fastest method.
However, it providesthe worst results in terms of smoothness.• Linear interpolation uses more memory than the nearest neighbor method,and requires slightly more execution time. Unlike nearest neighborinterpolation its results are continuous, but the slope changes at the vertexpoints.• Cubic spline interpolation has the longest relative execution time, althoughit requires less memory than cubic interpolation. It produces the smoothestresults of all the interpolation methods. You may obtain unexpected results,however, if your input data is non-uniform and some points are much closertogether than others.• Cubic interpolation requires more memory and execution time than eitherthe nearest neighbor or linear methods. However, both the interpolated dataand its derivative are continuous.The relative performance of each method holds true even for interpolation oftwo-dimensional or multidimensional data. For a graphical comparison of12-12Interpolationinterpolation methods, see the section “Comparing Interpolation Methods” onpage 12-14.FFT-Based InterpolationThe function interpft performs one-dimensional interpolation using anFFT-based method.
This method calculates the Fourier transform of a vectorthat contains the values of a periodic function. It then calculates the inverseFourier transform using more points. Its form isy = interpft(x,n)x is a vector containing the values of a periodic function, sampled at equallyspaced points. n is the number of equally spaced points to return.Two-Dimensional InterpolationThe function interp2 performs two-dimensional interpolation, an importantoperation for image processing and data visualization.
Its most general form isZI = interp2(X,Y,Z,XI,YI,method)Z is a rectangular array containing the values of a two-dimensional function,and X and Y are arrays of the same size containing the points for which thevalues in Z are given. XI and YI are matrices containing the points at which tointerpolate the data.
method is an optional string specifying an interpolationmethod.There are three different interpolation methods for two-dimensional data:• Nearest neighbor interpolation (method = 'nearest'). This method fits apiecewise constant surface through the data values. The value of aninterpolated point is the value of the nearest point.• Bilinear interpolation (method = 'linear'). This method fits a bilinearsurface through existing data points. The value of an interpolated point is acombination of the values of the four closest points. This method is piecewisebilinear, and is faster and less memory-intensive than bicubic interpolation.• Bicubic interpolation (method = 'cubic').
This method fits a bicubic surfacethrough existing data points. The value of an interpolated point is acombination of the values of the sixteen closest points. This method ispiecewise bicubic, and produces a much smoother surface than bilinearinterpolation. This can be a key advantage for applications like image12-1312Polynomials and Interpolationprocessing. Use bicubic interpolation when the interpolated data and itsderivative must be continuous.All of these methods require that X and Y be monotonic, that is, either alwaysincreasing or always decreasing from point to point. You should prepare thesematrices using the meshgrid function, or else be sure that the “pattern” of thepoints emulates the output of meshgrid.
In addition, each methodautomatically maps the input to an equally spaced domain beforeinterpolating. If X and Y are already equally spaced, you can speed executiontime by prepending an asterisk to the method string, for example, '*cubic'.Comparing Interpolation MethodsThis example compares two-dimensional interpolation methods on a 7-by-7matrix of data.1 Generate the peaks function at low resolution.[x,y] = meshgrid(-3:1:3);z = peaks(x,y);surf(x,y,z)6420−2−4−632312010−1−1−2−2−312-14−3Interpolation2 Generate a finer mesh for interpolation.[xi,yi] = meshgrid(-3:0.25:3);3 Interpolate using nearest neighbor interpolation.zi1 = interp2(x,y,z,xi,yi,'nearest');4 Interpolate using bilinear interpolation:zi2 = interp2(x,y,z,xi,yi,'bilinear');5 Interpolate using bicubic interpolation.zi3 = interp2(x,y,z,xi,yi,'bicubic');6 Compare the surface plots for the different interpolation methods.666444222000−2−2−2−4−4−4−63−632312010−1−1−2−2−3−3surf(xi,yi,zi1)% nearest−632312010−1−1−2−2−3−3surf(xi,yi,zi2)% bilinear2312010−1−1−2−2−3−3surf(xi,yi,zi3)% bicubic12-1512Polynomials and Interpolation7 Compare the contour plots for the different interpolation methods.333222111000−1−1−1−2−2−2−3−3−3−3−2−10123contour(xi,yi,zi1)% nearest−2−10123−3−3contour(xi,yi,zi2)% bilinear−2−10123contour(xi,yi,zi3)% bicubicNotice that the bicubic method, in particular, produces smoother contours.This is not always the primary concern, however.
For some applications, suchas medical image processing, a method like nearest neighbor may be preferredbecause it doesn’t generate any “new” data values.Interpolation and Multidimensional ArraysSeveral interpolation functions operate specifically on multidimensional data.Interpolation Functions for Multidimensional DataFunctionDescriptioninterp3Three-dimensional data interpolation.interpnMultidimensional data interpolation.ndgridMultidimensional data gridding (ndfun directory).This section discusses:• Interpolation of three-dimensional data• Interpolation of higher dimensional data• Multidimensional data gridding12-16InterpolationInterpolation of Three-Dimensional DataThe function interp3 performs three-dimensional interpolation, findinginterpolated values between points of a three-dimensional set of samples V.You must specify a set of known data points:• X, Y, and Z matrices specify the points for which values of V are given.• A matrix V contains values corresponding to the points in X, Y, and Z.The most general form for interp3 isVI = interp3(X,Y,Z,V,XI,YI,ZI,method)XI, YI, and ZI are the points at which interp3 interpolates values of V.
Forout-of-range values, interp3 returns NaN.There are three different interpolation methods for three-dimensional data:• Nearest neighbor interpolation (method = 'nearest'). This method choosesthe value of the nearest point.• Trilinear interpolation (method = 'linear'). This method uses piecewiselinear interpolation based on the values of the nearest eight points.• Tricubic interpolation (method = 'cubic').
This method uses piecewise cubicinterpolation based on the values of the nearest sixty-four points.All of these methods require that X, Y, and Z be monotonic, that is, either alwaysincreasing or always decreasing in a particular direction. In addition, youshould prepare these matrices using the meshgrid function, or else be sure thatthe “pattern” of the points emulates the output of meshgrid.Each method automatically maps the input to an equally spaced domain beforeinterpolating. If x is already equally spaced, you can speed execution time byprepending an asterisk to the method string, for example, '*cubic'.Interpolation of Higher Dimensional DataThe function interpn performs multidimensional interpolation, findinginterpolated values between points of a multidimensional set of samples V.
Themost general form for interpn isVI = interpn(X1,X2,X3...,V,Y1,Y2,Y3,...,method)1, 2, 3, ... are matrices that specify the points for which values of V are given.V is a matrix that contains the values corresponding to these points. 1, 2, 3, ...12-1712Polynomials and Interpolationare the points for which interpn returns interpolated values of V. Forout-of-range values, interpn returns NaN.Y1, Y2, Y3, ... must be either arrays of the same size, or vectors. If they arevectors of different sizes, interpn passes them to ndgrid and then uses theresulting arrays.There are three different interpolation methods for multidimensional data:• Nearest neighbor interpolation (method = 'nearest').
This method choosesthe value of the nearest point.• Linear interpolation (method = 'linear'). This method uses piecewiselinear interpolation based on the values of the nearest two points in eachdimension.• Cubic interpolation (method = 'cubic'). This method uses piecewise cubicinterpolation based on the values of the nearest four points in eachdimension.All of these methods require that X1, X2,X3 be monotonic. In addition, youshould prepare these matrices using the ndgrid function, or else be sure thatthe “pattern” of the points emulates the output of ndgrid.Each method automatically maps the input to an equally spaced domain beforeinterpolating.
If X is already equally spaced, you can speed execution time byprepending an asterisk to the method string; for example, '*cubic'.Multidimensional Data GriddingThe ndgrid function generates arrays of data for multidimensional functionevaluation and interpolation. ndgrid transforms the domain specified by aseries of input vectors into a series of output arrays. The ith dimension of theseoutput arrays are copies of the elements of input vector xi.The syntax for ndgrid is[X1,X2,X3,...] = ndgrid(x1,x2,x3,...)For example, assume that you want to evaluate a function of three variablesover a given range. Consider the function( –x1 –x2 –x3 )2z = x2 e12-1822Interpolationfor – 2π ≤ x 1 ≤ 0 , 2π ≤ x 2 ≤ 4π , and 0 ≤ x 3 ≤ 2π . To evaluate and plot thisfunction:x1 = -2:0.2:2;x2 = -2:0.25:2;x3 = -2:0.16:2;[X1,X2,X3] = ndgrid(x1,x2,x3);z = X2.*exp(-X1.^2 -X2.^2 -X3.^2);slice(X2,X1,X3,z,[-1.2.8 2],2,[-2 0.2])210−1−2221100−1−1−2−2Triangulation and Interpolation of Scattered DataMATLAB provides routines that aid in the analysis of closest-point problemsand geometric analysis.Functions for Analysis of Closest-Point Problems and Geometric AnalysisFunctionDescriptionconvhullConvex hull.delaunayDelaunay triangulation.12-1912Polynomials and InterpolationFunctions for Analysis of Closest-Point Problems and Geometric AnalysisFunctionDescriptiondelaunay33-D Delaunay tessellation.dsearchNearest point search of Delaunay triangulation.inpolygonTrue for points inside polygonal region.polyareaArea of polygon.rectintArea of intersection for two or more rectangles.tsearchClosest triangle search.voronoiVoronoi diagram.This section applies the following techniques to the seamount data set suppliedwith MATLAB:• Convex hulls• Delaunay triangulation• Voronoi diagramsSee also “Tessellation and Interpolation of Scattered Data in HigherDimensions” on page 12-27.Note Examples in this section use the MATLAB seamount data set.Seamounts are underwater mountains.
They are valuable sources ofinformation about marine geology. The seamount data set represents thesurface, in 1984, of the seamount designated LR148.8W located at 48.2°S,148.8°W on the Louisville Ridge in the South Pacific. For more informationabout the data and its use, see Parker [2].The seamount data set provides longitude (x), latitude (y) and depth-in-feet (z)data for 294 points on the seamount LR148.8W.12-20InterpolationConvex HullsThe convhull function returns the indices of the points in a data set thatcomprise the convex hull for the set.
Use the plot function to plot the output ofconvhull.This example loads the seamount data and plots the longitudinal (x) andlatitudinal (y) data as a scatter plot. It then generates the convex hull and usesplot to plot the convex hull.load seamountplot(x,y,'.','markersize',10)k = convhull(x,y);hold on, plot(x(k),y(k),'-r'), hold offgrid on−47.95−48−48.05−48.1−48.15−48.2−48.25−48.3−48.35−48.4−48.45210.8211211.2211.4211.6211.8Delaunay TriangulationGiven a set of coplanar data points, Delaunay triangulation is a set of linesconnecting each point to its natural neighbors.















