Skip to content

Instantly share code, notes, and snippets.

@castillejoale
Last active April 24, 2022 22:46
Show Gist options
  • Save castillejoale/8d414e70fcec0b51ead300f361cd6085 to your computer and use it in GitHub Desktop.
Save castillejoale/8d414e70fcec0b51ead300f361cd6085 to your computer and use it in GitHub Desktop.
Swift - interpolation function
import simd
static func interpolate(desiredX: inout [Float], y: inout [Float]) -> [Float] {
let n = desiredX.count - 1
let stride = vDSP_Stride(1) //Int Type Alias
let denominator = Float(n) / Float(y.count - 1)
let control: [Float] = desiredX.map {
let x = Float($0) / denominator
let smoothStep = Float(simd_smoothstep(0, 1, simd_fract(x)))
return Float(floor(x)) + smoothStep
}
var desiredY = [Float](repeating: 0, count: desiredX.count)
vDSP_vlint(&y,
control, stride,
&desiredY, stride,
UInt(desiredX.count),
UInt(y.count))
return desiredY
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment