如何使用 Accelerate 中的线性插值

How to use linear interpolation from Accelerate

我正在尝试使用 Apple 的 Accelerate 框架从数据集中线性插入新值。我已经使用 documentation 的这段来实现我的代码,但是当我尝试插入一组小于 1 的值时,我遇到了问题,分数。

let values: [CGFloat] = [0.0, 0.019124083, 0.035419375, 0.05232375, 0.06873629, 0.08550575, 0.10209821, 0.11870141, 0.1355003, 0.15223834, 0.16881292, 0.18565933, 0.20213126, 0.21881929, 0.2355565, 0.2522735, 0.26899675, 0.28572345, 0.30233976, 0.3187645, 0.33557975, 0.35221455]
let indices: [CGFloat] = [0.0, 0.00438118, 0.017873764, 0.04094696, 0.07394123, 0.11698151, 0.16987896, 0.23201275, 0.30223083, 0.37879562, 0.45942688, 0.54145336, 0.6220541, 0.6985626, 0.7687006, 0.8307409, 0.88353443, 0.9264679, 0.95935345, 0.98232174, 0.9957142, 1.0]

let numberOfElements = vDSP_Length(100)

var result = [Float](repeating: 0,
                count: Int(numberOfElements))

let stride = vDSP_Stride(1)

var base: Float = 0
var end = Float(values.count)
var control = [Float](repeating: 0,
                      count: Int(numberOfElements))

vDSP_vgenp(values, stride,
           indices, stride,
           &result, stride,
           numberOfElements,
           vDSP_Length(values.count))

print(result)

// [0.0, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455, 0.35221455]


referenced document 中所述,索引的小数部分定义值数组中从整数部分定义的索引开始的值对之间的插值。

精确的算法在vDSP_vgenp(_:_:_:_:_:_:_:_:)中描述为伪代码:

for (n = 0; n < N; ++n)
    If n <= B[0],  then C[n] = A[0].
    If B[M-1] < n, then C[n] = A[M-1].
    Otherwise:
        Let m be such that B[m] < n <= B[m+1].
        C[n] = A[m] + (A[m+1]-A[m]) * (n-B[m]) / (B[m+1]-B[m]).

这里A是值数组,B是索引数组,C是结果数组。

在你的例子中,所有索引都是 <= 1,这就是为什么所有 n >= 1.

result[n] = values[21] = 0.35221455

如果打算在区间 [0, 100] 内插值,则索引应在该范围内。所以你可能想要的是将索引(在 0..1 范围内)与结果数组的长度相乘:

let numberOfElements = vDSP_Length(100)

let indices: [Float] = [0.0, 0.00438118, ..., 0.9957142, 1.0]
    .map { [=11=] * Float(numberOfElements) }

结果是(result.map{ [=19=] } 在 Playground 中的 Quicklook 的屏幕截图):