Fill the gaps in arrays of numerical data using linear interpolation.

Framework

- Accelerate

## Overview

Linear interpolation is a method of calculating intermediate data between known values by conceptually drawing a straight line between two adjacent known values. An interpolated value is any point along that line. You use linear interpolation to, for example, draw graphs or animate between keyframes. Figure 1 shows an example interpolated value between two points.

vDSP provides two functions to linearly interpolate between the elements in an array.

`v`

provides a simple interface for generating interpolated data.DSP _vgenp(_: _: _: _: _: _: _: _:) `v`

provides fine control over the interpolation.DSP _vlint(_: _: _: _: _: _: _:)

This article discusses both functions to draw a continuous line graph based on a set of discrete data points.

### Create the Data

In this example, you'll interpolate the data represented by two arrays, `values`

and `indices`

:

Each array consists of 10 elements. A pair of corresponding elements in the two arrays defines a single point in the diagram as follows:

Elements in the

`values`

array denote the vertical position.Elements in the

`indices`

array denote the horizontal position.

### Define Common Fundamental Constants

When defining the constants used to plot the points, this example uses `n`

as the number of elements in the interpolation result, and `stride`

as the stride for all arrays.

### Generate a Vector by Interpolation

The `v`

function (and its double-precision equivalent, `v`

) accepts the values and indices arrays and an empty array to receive the interpolation result:

On return, `result`

contains the interpolated values. Figure 3 shows the values in `result`

as a line between the known values described by the `values`

and `indices`

arrays:

### Interpolate With Fine Control

The `v`

and `v`

) functions require an `indices`

array that includes fractional parts. The fractional parts define the interpolation between the pair of values in the `values`

array starting at the index defined by the integer part.

Use `v`

to generate a linear ramp from `0`

to the number of elements in `values`

, minus 1:

On return, `control`

contains:

` [0] Float 0`

` [1] Float 0`

` [2] Float 0`

` ...`

` [1021] Float 8`

` [1022] Float 8`

` [1023] Float 9`

Figure 4 shows a visualization of the values in `control`

with small circles indicating each integer index.

With the control values defined, call `v`

to calculate the interpolated values:

On return, `result`

contains the interpolated values. Figure 5 shows the values in `result`

as a line between the known values described by the `values`

and `indices`

arrays.

The result of `v`

is equal to the result of `v`

.

### Add Smoothing to the Interpolation Result

You can change the way that you generate the control array to alter the interpolated result. For example, you may want to smooth the joins in a line graph or add easing to an animation. The following code uses `simd`

to smooth the fractional parts of `control`

near the increments to the integer parts:

Figure 6 shows a visualization of the values in `control`

with small circles indicating each integer index.

Using the same call to `v`

as above, the result, as shown in Figure 7, shows a smoother transition between the known values.