<html lang="en"> <head> <title>One-dimensional Interpolation - GNU Octave</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Octave"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Interpolation.html#Interpolation" title="Interpolation"> <link rel="next" href="Multi_002ddimensional-Interpolation.html#Multi_002ddimensional-Interpolation" title="Multi-dimensional Interpolation"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="One-dimensional-Interpolation"></a> <a name="One_002ddimensional-Interpolation"></a> <p> Next: <a rel="next" accesskey="n" href="Multi_002ddimensional-Interpolation.html#Multi_002ddimensional-Interpolation">Multi-dimensional Interpolation</a>, Up: <a rel="up" accesskey="u" href="Interpolation.html#Interpolation">Interpolation</a> <hr> </div> <h3 class="section">29.1 One-dimensional Interpolation</h3> <p>Octave supports several methods for one-dimensional interpolation, most of which are described in this section. <a href="Polynomial-Interpolation.html#Polynomial-Interpolation">Polynomial Interpolation</a> and <a href="Interpolation-on-Scattered-Data.html#Interpolation-on-Scattered-Data">Interpolation on Scattered Data</a> describe further methods. <!-- interp1 scripts/general/interp1.m --> <p><a name="doc_002dinterp1"></a> <div class="defun"> — Function File: <var>yi</var> = <b>interp1</b> (<var>x, y, xi</var>)<var><a name="index-interp1-2747"></a></var><br> — Function File: <var>yi</var> = <b>interp1</b> (<var>y, xi</var>)<var><a name="index-interp1-2748"></a></var><br> — Function File: <var>yi</var> = <b>interp1</b> (<var><small class="dots">...</small>, method</var>)<var><a name="index-interp1-2749"></a></var><br> — Function File: <var>yi</var> = <b>interp1</b> (<var><small class="dots">...</small>, extrap</var>)<var><a name="index-interp1-2750"></a></var><br> — Function File: <var>pp</var> = <b>interp1</b> (<var><small class="dots">...</small>, 'pp'</var>)<var><a name="index-interp1-2751"></a></var><br> <blockquote> <p>One-dimensional interpolation. Interpolate <var>y</var>, defined at the points <var>x</var>, at the points <var>xi</var>. The sample points <var>x</var> must be monotonic. If not specified, <var>x</var> is taken to be the indices of <var>y</var>. If <var>y</var> is an array, treat the columns of <var>y</var> separately. <p>Method is one of: <dl> <dt>'nearest'<dd>Return the nearest neighbor. <br><dt>'linear'<dd>Linear interpolation from nearest neighbors <br><dt>'pchip'<dd>Piecewise cubic Hermite interpolating polynomial <br><dt>'cubic'<dd>Cubic interpolation (same as <code>pchip</code>) <br><dt>'spline'<dd>Cubic spline interpolation—smooth first and second derivatives throughout the curve </dl> <p>Appending '*' to the start of the above method forces <code>interp1</code> to assume that <var>x</var> is uniformly spaced, and only <var>x</var><code> (1)</code> and <var>x</var><code> (2)</code> are referenced. This is usually faster, and is never slower. The default method is 'linear'. <p>If <var>extrap</var> is the string 'extrap', then extrapolate values beyond the endpoints. If <var>extrap</var> is a number, replace values beyond the endpoints with that number. If <var>extrap</var> is missing, assume NA. <p>If the string argument 'pp' is specified, then <var>xi</var> should not be supplied and <code>interp1</code> returns the piecewise polynomial that can later be used with <code>ppval</code> to evaluate the interpolation. There is an equivalence, such that <code>ppval (interp1 (</code><var>x</var><code>, </code><var>y</var><code>, </code><var>method</var><code>, 'pp'), </code><var>xi</var><code>) == interp1 (</code><var>x</var><code>, </code><var>y</var><code>, </code><var>xi</var><code>, </code><var>method</var><code>, 'extrap')</code>. <p>Duplicate points in <var>x</var> specify a discontinuous interpolant. There should be at most 2 consecutive points with the same value. The discontinuous interpolant is right-continuous if <var>x</var> is increasing, left-continuous if it is decreasing. Discontinuities are (currently) only allowed for "nearest" and "linear" methods; in all other cases, <var>x</var> must be strictly monotonic. <p>An example of the use of <code>interp1</code> is <pre class="example"> xf = [0:0.05:10]; yf = sin (2*pi*xf/5); xp = [0:10]; yp = sin (2*pi*xp/5); lin = interp1 (xp, yp, xf); spl = interp1 (xp, yp, xf, "spline"); cub = interp1 (xp, yp, xf, "cubic"); near = interp1 (xp, yp, xf, "nearest"); plot (xf, yf, "r", xf, lin, "g", xf, spl, "b", xf, cub, "c", xf, near, "m", xp, yp, "r*"); legend ("original", "linear", "spline", "cubic", "nearest"); </pre> <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dinterpft.html#doc_002dinterpft">interpft</a>. </p></blockquote></div> <p>There are some important differences between the various interpolation methods. The 'spline' method enforces that both the first and second derivatives of the interpolated values have a continuous derivative, whereas the other methods do not. This means that the results of the 'spline' method are generally smoother. If the function to be interpolated is in fact smooth, then 'spline' will give excellent results. However, if the function to be evaluated is in some manner discontinuous, then 'pchip' interpolation might give better results. <p>This can be demonstrated by the code <pre class="example"> t = -2:2; dt = 1; ti =-2:0.025:2; dti = 0.025; y = sign(t); ys = interp1(t,y,ti,'spline'); yp = interp1(t,y,ti,'pchip'); ddys = diff(diff(ys)./dti)./dti; ddyp = diff(diff(yp)./dti)./dti; figure(1); plot (ti, ys,'r-', ti, yp,'g-'); legend('spline','pchip',4); figure(2); plot (ti, ddys,'r+', ti, ddyp,'g*'); legend('spline','pchip'); </pre> <p class="noindent">The result of which can be seen in <a href="fig_003ainterpderiv1.html#fig_003ainterpderiv1">fig:interpderiv1</a> and <a href="fig_003ainterpderiv2.html#fig_003ainterpderiv2">fig:interpderiv2</a>. <div class="float"> <a name="fig_003ainterpderiv1"></a><div align="center"><img src="interpderiv1.png" alt="interpderiv1.png"></div> <p><strong class="float-caption">Figure 29.1: Comparison of 'pchip' and 'spline' interpolation methods for a step function</strong></p></div> <div class="float"> <a name="fig_003ainterpderiv2"></a><div align="center"><img src="interpderiv2.png" alt="interpderiv2.png"></div> <p><strong class="float-caption">Figure 29.2: Comparison of the second derivative of the 'pchip' and 'spline' interpolation methods for a step function</strong></p></div> <p>A simplified version of <code>interp1</code> that performs only linear interpolation is available in <code>interp1q</code>. This argument is slightly faster than <code>interp1</code> as to performs little error checking. <!-- interp1q scripts/general/interp1q.m --> <p><a name="doc_002dinterp1q"></a> <div class="defun"> — Function File: <var>yi</var> = <b>interp1q</b> (<var>x, y, xi</var>)<var><a name="index-interp1q-2752"></a></var><br> <blockquote><p>One-dimensional linear interpolation without error checking. Interpolates <var>y</var>, defined at the points <var>x</var>, at the points <var>xi</var>. The sample points <var>x</var> must be a strictly monotonically increasing column vector. If <var>y</var> is an array, treat the columns of <var>y</var> separately. If <var>y</var> is a vector, it must be a column vector of the same length as <var>x</var>. <p>Values of <var>xi</var> beyond the endpoints of the interpolation result in NA being returned. <p>Note that the error checking is only a significant portion of the execution time of this <code>interp1</code> if the size of the input arguments is relatively small. Therefore, the benefit of using <code>interp1q</code> is relatively small. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dinterp1.html#doc_002dinterp1">interp1</a>. </p></blockquote></div> <p>Fourier interpolation, is a resampling technique where a signal is converted to the frequency domain, padded with zeros and then reconverted to the time domain. <!-- interpft scripts/general/interpft.m --> <p><a name="doc_002dinterpft"></a> <div class="defun"> — Function File: <b>interpft</b> (<var>x, n</var>)<var><a name="index-interpft-2753"></a></var><br> — Function File: <b>interpft</b> (<var>x, n, dim</var>)<var><a name="index-interpft-2754"></a></var><br> <blockquote> <p>Fourier interpolation. If <var>x</var> is a vector, then <var>x</var> is resampled with <var>n</var> points. The data in <var>x</var> is assumed to be equispaced. If <var>x</var> is an array, then operate along each column of the array separately. If <var>dim</var> is specified, then interpolate along the dimension <var>dim</var>. <p><code>interpft</code> assumes that the interpolated function is periodic, and so assumptions are made about the endpoints of the interpolation. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dinterp1.html#doc_002dinterp1">interp1</a>. </p></blockquote></div> <p>There are two significant limitations on Fourier interpolation. Firstly, the function signal is assumed to be periodic, and so non-periodic signals will be poorly represented at the edges. Secondly, both the signal and its interpolation are required to be sampled at equispaced points. An example of the use of <code>interpft</code> is <pre class="example"> t = 0 : 0.3 : pi; dt = t(2)-t(1); n = length (t); k = 100; ti = t(1) + [0 : k-1]*dt*n/k; y = sin (4*t + 0.3) .* cos (3*t - 0.1); yp = sin (4*ti + 0.3) .* cos (3*ti - 0.1); plot (ti, yp, 'g', ti, interp1(t, y, ti, 'spline'), 'b', ... ti, interpft (y, k), 'c', t, y, 'r+'); legend ('sin(4t+0.3)cos(3t-0.1','spline','interpft','data'); </pre> <p class="noindent">which demonstrates the poor behavior of Fourier interpolation for non-periodic functions, as can be seen in <a href="fig_003ainterpft.html#fig_003ainterpft">fig:interpft</a>. <div class="float"> <a name="fig_003ainterpft"></a><div align="center"><img src="interpft.png" alt="interpft.png"></div> <p><strong class="float-caption">Figure 29.3: Comparison of <code>interp1</code> and <code>interpft</code> for non-periodic data</strong></p></div> <p>In additional the support function <code>spline</code> and <code>lookup</code> that underlie the <code>interp1</code> function can be called directly. <a href="Finding-Elements-and-Checking-Conditions.html#Finding-Elements-and-Checking-Conditions">Finding Elements and Checking Conditions</a> <!-- spline scripts/polynomial/spline.m --> <p><a name="doc_002dspline"></a> <div class="defun"> — Function File: <var>pp</var> = <b>spline</b> (<var>x, y</var>)<var><a name="index-spline-2755"></a></var><br> — Function File: <var>yi</var> = <b>spline</b> (<var>x, y, xi</var>)<var><a name="index-spline-2756"></a></var><br> <blockquote><p>Return the cubic spline interpolant of points <var>x</var> and <var>y</var>. <p>When called with two arguments, return the piecewise polynomial <var>pp</var> that may be used with <code>ppval</code> to evaluate the polynomial at specific points. When called with a third input argument, <code>spline</code> evaluates the spline at the points <var>xi</var>. The third calling form <code>spline (</code><var>x</var><code>, </code><var>y</var><code>, </code><var>xi</var><code>)</code> is equivalent to <code>ppval (spline (</code><var>x</var><code>, </code><var>y</var><code>), </code><var>xi</var><code>)</code>. <p>The variable <var>x</var> must be a vector of length <var>n</var>. <var>y</var> can be either a vector or array. If <var>y</var> is a vector it must have a length of either <var>n</var> or <var>n</var><code> + 2</code>. If the length of <var>y</var> is <var>n</var>, then the "not-a-knot" end condition is used. If the length of <var>y</var> is <var>n</var><code> + 2</code>, then the first and last values of the vector <var>y</var> are the values of the first derivative of the cubic spline at the endpoints. <p>If <var>y</var> is an array, then the size of <var>y</var> must have the form <code>[</code><var>s1</var><code>, </code><var>s2</var><code>, ..., </code><var>sk</var><code>, </code><var>n</var><code>]</code> or <code>[</code><var>s1</var><code>, </code><var>s2</var><code>, ..., </code><var>sk</var><code>, </code><var>n</var><code> + 2]</code>. The array is reshaped internally to a matrix where the leading dimension is given by <var>s1</var><code> * </code><var>s2</var><code> * ... * </code><var>sk</var> and each row of this matrix is then treated separately. Note that this is exactly opposite to <code>interp1</code> but is done for <span class="sc">matlab</span> compatibility. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dpchip.html#doc_002dpchip">pchip</a>, <a href="doc_002dppval.html#doc_002dppval">ppval</a>, <a href="doc_002dmkpp.html#doc_002dmkpp">mkpp</a>, <a href="doc_002dunmkpp.html#doc_002dunmkpp">unmkpp</a>. </p></blockquote></div> </body></html>