Go to the first, previous, next, last section, table of contents.


The spline Program

How to use spline

GNU spline is a program for interpolating between the data points in one or more datasets. Each dataset would consist of values for an independent variable and a dependent variable, which may be a vector of specified fixed length. When discussing interpolation, we call these variables `t' and `y', respectively. To emphasize: t is a scalar, but in general the dependent variable y may be a vector.

The simplest case is when there is a single input file, which is in ASCII format, and the vector y is one-dimensional. This is the default. For example, the input file could contain the dataset

0.0  0.0
1.0  1.0
2.0  0.0

which are the coordinates (t,y) of the data points (0,0), (1,1), and (2,0). Data points do not need to be on different lines, nor do the t and y coordinates of a data point need to be on the same line. However, there should be no blank lines in the input if it is to be viewed as forming a single dataset. Also, by default the t coordinate should be monotonically increasing, so that y may be viewed as a function of t.

You would construct a spline (the graph of an `interpolating function') passing through the points in this dataset by doing

spline input_file > output_file

To produce a Postscript plot of the spline with the graph utility, you would do

spline input_file | graph -T ps > output.ps

To display a spline on an X Window System display, you could do

echo 0 0 1 1 2 0 | spline | graph -T X

Notice that the last example avoids the use of the input file altogether. spline will read from standard input if no files are specified on the command line, or if the special file name `-' is specified.

What exactly does spline do? First, it fits a curve (the graph of an interpolating function) through the points in the dataset. Then it splits the interval over which the independent variable t ranges into 100 sub-intervals, and computes the y values at each of the 101 subdivision points. It then outputs each of the pairs (t, y). These are the coordinates of 101 points that lie along a curve that interpolates between the points in the dataset. If there is more than one dataset in the input (separated by blank lines), each dataset is interpolated separately.

You may use the `-n' option to replace `100' by any other positive integer. You may also use the `-t' option to specify an interpolation interval that differs from the default (the interval over which the independent variable ranges). For example, the command

echo 0 0 1 1 2 0 | spline -n 20 -t 1.0 1.5 > output_file

will produce a dataset consisting of 21 (rather than 101) data points, with t values spaced regularly between 1.0 and 1.5 (rather than between 0.0 and 2.0). The data points will lie along a curve passing through (0,0), (1,1), and (2,0). This curve will be a parabola.

In general, the interpolating function will be a piecewise cubic spline. That is, between each pair of adjacent `knots' (points in the input dataset), y will be a cubic function of t. This function will differ, depending on which pair of knots y lies between. At each knot, both the slope and curvature of the cubic pieces to either side will match. In mathematical terms, the interpolating curve will be twice continuously differentiable.

spline supports `adding tension' to the interpolating curve. A nonzero value for the tension can be specified with the `-T' option. For example, a spline under considerable tension can be computed and displayed by doing

echo 0 0 1 0 2 0 | spline -T 10 | graph -T X

As the tension parameter is increased to positive infinity, the spline will converge to a polygonal line. You are meant to think of the spline as being drawn taut. Actually, tension may be negative as well as positive. A spline with negative tension will tend to bow outward, in fact to oscillate sinusoidally. But as the tension decreases to negative infinity, the spline, though oscillatory, will again converge to a polygonal line.

If the tension is positive, its reciprocal will be the maximum range of the independent variable t over which the spline will `like to curve'. Increasing the tension far above zero will accordingly force the spline to consist of short curved sections, centered on the data points, and sections that are almost straight. It follows that tension is a `dimensionful' quantity. If the tension is nonzero, then when the values of the independent variable are multiplied by some common positive factor, the tension should be divided by the same factor to obtain a scaled version of the original spline. If the tension is zero (the default, or cubic spline case), then the computation of the spline will be unaffected by linear scaling of the data.

In mathematical terms, a spline under tension will satisfy the differential equation @ifnottex y""=sgn(tension)*(tension^2)y" between each successive pair of knots. If the tension equals zero, which is the default, the fourth derivative of y with respect to t will equal zero at every point. In this case, y as a function of t will reduce to a cubic polynomial between each successive pair of knots. But if the tension is nonzero, y will not be a polynomial function of t. It may be expressed in terms of exponential functions, however.

Irrespective of whether or not the spline is under tension, you may specify the `-p' option if you wish the spline to be a periodic function of t. This will only work if the y values for the first and last points in the dataset are equal. Otherwise, it would make no sense to compute a periodic interpolation.

It is sometimes useful to interpolate between data points at the same time as they are generated by an auxiliary program. That is, it is useful for spline to function as a real-time filter. spline does not normally act as a filter, since computing an interpolating curve that is as smooth as possible is a global task. But if the `-f' option is specified, spline will indeed function as a filter. A different interpolation algorithm (cubic Bessel interpolation, which is local rather than global) will be used. If `-f' is specified, `-p' may not be specified. Also, if `-f' is specified then an interpolation interval (a range of t values) must be requested explicitly with the `-t' option.

Cubic Bessel interpolation is inherently less smooth than the construction of a global cubic spline. If the `-f' option is specified, the slope of the spline at each knot will be chosen by fitting a parabola through that knot, and the two adjacent knots. The slopes of the two interpolating segments to either side of each interior knot will match at that knot, but typically their curvatures will not. In mathematical terms, the interpolating curve will be continuously differentiable, but in general not twice continuously differentiable. This loss of differentiability is the price that is paid for functioning as a real-time filter.

Advanced use of spline

The preceding section explains how spline can be employed to interpolate a function y of a scalar variable t, in the case when y is a scalar. In this section we explain how to perform more sophisticated interpolations. This includes multidimensional interpolations, and interpolations that are splinings of curves, rather than of functions.

spline can handle the case when y is a vector of arbitrary specified dimensionality. The dimension can be specified with the `-d' option. For example, an input file could contain the multidimensional dataset

0.0  0.0  1.0
1.0  1.0  0.0
2.0  0.0  1.0

which are the coordinates (t,y) of the data points (0,0,1), (1,1,0), and (2,0,1). You would construct a spline (the graph of an interpolating function) passing through the points in this dataset by doing

spline -d 2 input_file > output_file

The option `-d 2' is used because in this example, the dependent variable y is a two-dimensional vector. Each of the components of y will be interpolated independently, and the output file will contain points that lie along the graph of the resulting interpolating function.

When doing multidimensional splining, you may use any of the options that apply in the default one-dimensional case. For example, the `-f' option will yield real-time cubic Bessel interpolation. As in the one-dimensional case, if the `-f' option is used then the `-t' option must be used as well, to specify an interpolation interval (a range of t values). The -p option will yield a periodic spline, i.e., the graph of a periodic vector-valued function. For this, the first and last dataset y values must be the same.

spline can also be used to draw a curve through arbitrarily chosen points in the plane, or in general through arbitrarily chosen points in d-dimensional space. This is not the same as splining, at least as the term is conventionally defined. The reason is that `splining' refers to construction of a function, rather than the construction of a curve that may or may not be the graph of a function. Not every curve is the graph of a function.

The following example shows how you may `spline a curve'. The command

echo 0 0 1 0 1 1 0 1 | spline -d 2 -a -s | graph -T X

will construct a curve in the plane through the four points (0,0), (1,0), (1,1), and (0,1), and graph it on an X Window System display. The `-d 2' option specifies that the dependent variable y is two-dimensional. The `-a' option specifies that t values are missing from the input, and should be automatically generated. By default, the first t value is 0, the second is 1, etc. The `-s' option specifies that the t values should be stripped from the output.

The same technique may be used to spline a closed curve. For example, doing

echo 0 0 1 0 0 1 0 0 | spline -d 2 -a -s -p | graph -T X

will construct and graph a closed, lozenge-shaped curve through the three points (0,0), (1,0), and (0,1). The construction of a closed curve is guaranteed by the `-p' (i.e., `--periodic') option, and by the repetition of the initial point (0,0) at the end of the sequence.

When splining a curve, whether open or closed, you may wish to substitute the `-A' option for the `-a' option. Like the `-a' option, the `-A' option specifies that t values are missing from the input and should be automatically generated. However, the increment from one t value to the next will be the distance between the corresponding values of y. This scheme for generating t values, when constructing a curve through a sequence of points, is the scheme that is used in the well known FITPACK subroutine library. It is probably the best approach when the distances between successive points fluctuate considerably.

A curve through a sequence of points in the plane, whether open or closed, may cross itself. Some interesting visual effects can be obtained by adding negative tension to such a curve. For example, doing

echo 0 0 1 0 1 1 0 0 | spline -d 2 -a -s -p -T -14 -n 500 | graph -T X

will construct a closed curve through the three points (0,0), (1,0), and (0,1), which is wound into curlicues. The `-n 500' option is included because there are so many windings. It specifies that 501 points should be generated, which is enough to draw a smooth curve.

spline command-line options

The spline program will interpolate vector-valued functions of a scalar variable t, and curves in d-dimensional space. The algorithms used by spline are similar to those discussed in D. Kincaid and [E.] W. Cheney, Numerical Analysis (2nd ed., Brooks/Cole, 1996), section 6.4, and C. de Boor, A Practical Guide to Splines (Springer-Verlag, 1978), Chapter 4.

Input file names may be specified anywhere on the command line. That is, the relative order of font names and command-line options does not matter. If no file names are specified, or the file name `-' is specified, the standard input is read.

An input file may contain more than a single dataset. Unless the `-a' or `-A' options are used (see below), each dataset is expected to consist of a sequence of data points, given as alternating t and y values. t is the scalar independent variable, and y is the vector-valued dependent variable. The dimensionality of y is specified with the `-d' option (the default is 1).

If the input file is in ASCII format (the default), its datasets are separated by blank lines. An input file may also contain any number of comment lines, which must begin with the comment character `#'. Comment lines are ignored. They are not treated as blank, i.e., they do not interrupt a dataset in progress.

The options to spline are listed below. There are three sorts of option:

  1. Options specifying the type of interpolation to be performed on each dataset.
  2. Options specifying the input or output format.
  3. Options requesting information (e.g., `--help').

Options that take an argument are followed, in parentheses, by the type and default value of the argument.

The following options specify the type of interpolation to be performed on each dataset.

`-f'
`--filter'
Use a local interpolation algorithm (the cubic Bessel algorithm), so that spline can be used as a real-time filter. The slope of the interpolating curve at each point in a dataset will be chosen by fitting a quadratic function through that point and the two adjacent points in the dataset. If `-f' is specified then the `-t' option, otherwise optional, must be used as well. Also, if `-f' is specified then the `-k', `-p', and `-T' options may not be used. If `-f' is not specified, then a different (global) interpolation algorithm will be used.
`-k k'
`--boundary-condition k'
(Float, default 1.0.) Set the boundary condition parameter for each constructed spline to be k. In each of its components, the spline will satisfy the two boundary conditions y"[0]=ky"[1] and y"[n]=ky"[n-1]. Here y[0] and y[1] signify the values of a specified component of the vector-valued dependent variable y at the first two points of a dataset, and y[n-1] and y[n] the values at the last two points. Setting k to zero will yield a `natural' spline, i.e., one that has zero curvature at the two ends of the dataset. The `-k' option may not be used if `-f' or `-p' is specified.
`-n n'
`--number-of-intervals n'
(Positive integer, default 100.) Subdivide the interval over which interpolation occurs into n subintervals. The number of data points computed, and written to the output, will be n+1.
`-p'
`--periodic'
Construct a periodic spline. If this option is specified, the y values for the first and last points in each dataset must be equal. The `-f' and `-k' options may not be used if `-p' is specified.
`-T tension'
`--tension tension'
(Float, default 0.0.) Set the tension in each interpolating spline to be tension. Between each pair of successive points in a dataset, the constructed spline will satisfy the differential equation @ifnottex y""=sgn(tension)*(tension^2)y" in each of its components. If tension equals zero, the spline will be piecewise cubic. As tension increases to positive infinity, the spline will converge to a polygonal line. The `-T' option may not be used if `-f' is specified.
`-t tmin tmax [tspacing]'
`--t-limits tmin tmax [tspacing]'
For each dataset, set the interval over which interpolation occurs to be the interval between tmin and tmax. If tspacing is not specified, the interval will be divided into the number of subintervals specified by the `-n' option. If the `-t' option is not used, the interval over which interpolation occurs will be the entire range of the independent variable in the dataset. The `-t' option must always be used if the `-f' option is used to request filter-like behavior (see above).

The following options specify the format of the input file(s) and the output file.

`-d dimension'
`--y-dimension dimension'
(Integer, default 1.) Set the dimensionality of the dependent variable y in the input and output files to be dimension.
`-I data-format'
`--input-format data-format'
(Character, default `a'.) Set the data format for the input file(s) to be data-format. The possible data formats are as follows.
`a'
ASCII format. Each file is a sequence of floating point numbers, interpreted as the t and y coordinates of the successive data points in a dataset. If y is d-dimensional, there will be d+1 numbers for each point. The t and y coordinates of a point need not appear on the same line, and points need not appear on different lines. But if a blank line occurs (i.e., two newlines in succession are seen), it is interpreted as the end of a dataset, and the beginning of the next.
`f'
@ifnottex Single precision binary format. Each file is a sequence of floating point numbers, interpreted as the t and y coordinates of the successive data points in a dataset. If y is d-dimensional, there will be d+1 numbers for each point. Successive datasets are separated by a single occurrence of the quantity FLT_MAX, which is the largest possible single precision floating point number. On most machines this is approximately 3.4x10^38.
`d'
@ifnottex Double precision binary format. Each file is a sequence of double precision floating point numbers, interpreted as the t and y coordinates of the successive data points in a dataset. If y is d-dimensional, there will be d+1 numbers for each point. Successive datasets are separated by a single occurrence of the quantity DBL_MAX, which is the largest possible double precision floating point number. On most machines this is approximately 1.8x10^308.
`i'
@ifnottex Integer binary format. Each file is a sequence of integers, interpreted as the t and y coordinates of the successive data points in a dataset. If y is d-dimensional, there will be d+1 numbers for each point. Successive datasets are separated by a single occurrence of the quantity INT_MAX, which is the largest possible integer. On most machines this is 2^31-1.
`-a [step_size [lower_limit]]'
`--auto-abscissa [step_size [lower_limit]]'
(Floats, defaults 1.0 and 0.0.) Automatically generate values for the independent variable (t). Irrespective of data format (`a', `f', `d', or `i'), this option specifies that the values of the independent variable (t) are missing from the input file: the dataset(s) to be read contain only values of the dependent variable (y), so that if y is d-dimensional, there will be only d numbers for each point. The increment from each t value to the next will be step_size, and the first t value will be lower_limit.
`-A'
`--auto-dist-abscissa'
Automatically generate values for the independent variable (t). This is a variant form of the `-a' option. The increment from each t value to the next will be the distance between the corresponding y values, and the first t value will be 0.0. This option is useful when interpolating curves rather than functions (see section Advanced use of spline).
`-O data-format'
`--output-format data-format'
(Character, default `a'.) Set the data format for the output file to be data-format. The interpretation of the data-format argument is the same as for the `-I' option.
`-P significant-digits'
`--precision significant-digits'
(Positive integer, default 6.) Set the numerical precision for the t and y values in the output file to be significant-digits. This takes effect only if the output file is written in `a' format, i.e., in ASCII.
`-s'
`--suppress-abscissa'
Omit the independent variable t from the output file; for each point, supply only the dependent variable y. If y is d-dimensional, there will be only d numbers for each point, not d+1. This option is useful when interpolating curves rather than functions (see section Advanced use of spline).

The following options request information.

`--help'
Print a list of command-line options, and then exit.
`--version'
Print the version number of spline and the plotting utilities package, and exit.


Go to the first, previous, next, last section, table of contents.