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 == FloatParameters
- vector:
An array that contains the values to interpolate.
- controlVector:
An array that defines the interpolation: integer parts are indices into
vectorand fractional parts are interpolation constants.
Mentioned in
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.
[Image]
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.
[Image]