Generic Type Method

linearInterpolate(elementsOf:using:)

Returns the interpolation between the neighboring elements of a single-precision vector.

Declaration

static func linearInterpolate<T, U>(elementsOf vector: T, using controlVector: U) -> [Float] where T : AccelerateBuffer, U : AccelerateBuffer, T.Element == Float, U.Element == Float

Parameters

vector

An array that contains the values to interpolate.

controlVector

An array that defines the interpolation: integer parts are indices into vector and fractional parts are interpolation constants.

Discussion

Single-precision and double-precision linearInterpolate(elementsOf:using:) functions return an array of an arbitrary length that's constructed from the linearly interpolated values in a source array. Pass linearInterpolate(elementsOf:using:) a control vector that defines the interpolation: the integer part of each element in the control vector is the zero-based index of the first element of a pair of adjacent values in the source array, and the fractional part defines the linear interpolation between the values at those indices.

For example, the following code generates a five-element vector by interpolating three values:

let result = vDSP.linearInterpolate(elementsOf: [100, 200, 300],
                                    using: [0, 0.5, 1, 1.5, 2])

On return, result contains [100.0, 150.0, 200.0, 250.0, 300.0].

To compute longer interpolation results, use ramp(in:count:) to generate the control vector. The following code creates 1024 interpolated values from 10 source values:

let values: [Float] = [50, 90, 55, 10, 40, 85, 65, 15, 30, 80]
let controlVector: [Float] = vDSP.ramp(in: 0 ... Float(values.count) - 1,
                                       count: 1024)

let result = vDSP.linearInterpolate(elementsOf: values,
                                    using: controlVector)

The following figure visualizes the elements in result.

A graph of the linearly interpolated values based on a control vector created with a ramp.

By changing the technique used to form the fractional parts of the control vector, you change the interpolation between the values in the source vector. The following code uses a sigmoid function—that is, a function that has an "S" shaped curve—to populate the control vector:

let values: [Float] = [50, 90, 55, 10, 40, 85, 65, 15, 30, 80]

let denominator = 1024 / Float(values.count - 1)
let tau = Float.pi * 2
let controlVector: [Float] = (0 ..< 1024).map {
    let x = modf(Float($0) / denominator)
    
    return x.0 + (tanh((x.1 - 0.5) * tau) * 0.5) + 0.5
}

let result = vDSP.linearInterpolate(elementsOf: values,
                                    using: controlVector)

The following figure visualizes the elements in result using hyperbolic tangent for the sigmoid function.

A graph of the linearly interpolated values based on a control vector created with a sigmoid function.

See Also

Single-Vector Linear Interpolation

Use Linear Interpolation to Construct New Data Points

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

static func linearInterpolate<T, U>(elementsOf: T, using: U) -> [Double]

Returns the interpolation between the neighboring elements of a double-precision vector.

static func linearInterpolate<T, U, V>(elementsOf: T, using: U, result: inout V)

Calculates the interpolation between the neighboring elements of a double-precision vector.

static func linearInterpolate<T, U, V>(elementsOf: T, using: U, result: inout V)

Calculates the interpolation between the neighboring elements of a single-precision vector.

func vDSP_vlint(UnsafePointer<Float>, UnsafePointer<Float>, vDSP_Stride, UnsafeMutablePointer<Float>, vDSP_Stride, vDSP_Length, vDSP_Length)

Calculates the interpolation between the neighboring elements of a single-precision vector using the specified stride.

func vDSP_vlintD(UnsafePointer<Double>, UnsafePointer<Double>, vDSP_Stride, UnsafeMutablePointer<Double>, vDSP_Stride, vDSP_Length, vDSP_Length)

Calculates the interpolation between the neighboring elements of a double-precision vector using the specified stride.