1

我正在尝试使用 Apple 的 Accelerate 框架从数据集中线性插入新值。我已经使用这段文档来实现我的代码,但是当我尝试插入一组小于 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]


4

1 回答 1

2

参考文档中所述,索引的小数部分定义了值数组中从整数部分定义的索引开始的值对之间的插值。

精确算法被描述为伪代码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,这就是result[n] = values[21] = 0.35221455所有索引的原因n >= 1

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

let numberOfElements = vDSP_Length(100)

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

结果是(来自result.map{ $0 }游乐场中 Quicklook 的屏幕截图):

在此处输入图像描述

于 2020-05-26T13:47:09.050 回答