1

I'm attempting to use the vDSP_conv function in Accelerate here. One of the arguments to vDSP_conv is a const float *__F that "needs to point to the last vector element". I'm not extremely familiar with using pointers in Swift, so I'm at a loss for how to create a pointer to the last array element of a Swift array.

Can anyone provide some insight?

/** EDIT **/

Function specification that I am trying to call: func vDSP_conv(_ __A: UnsafePointer<Float>, _ __IA: vDSP_Stride, _ __F: UnsafePointer<Float>, _ __IF: vDSP_Stride, _ __C: UnsafeMutablePointer<Float>, _ __IC: vDSP_Stride, _ __N: vDSP_Length, _ __P: vDSP_Length)

So far, I have this code. I need y to point be a pointer to the last element in the array as conv starts at the end of the array and progresses to the front

public func conv(x: [Float], y: [Float]) -> [Float] {
    var result = [Float](x)
    let inputLength:Int = x.count
    let outputLength:Int = inputLength + y.count - 1
    vDSP_conv(x, 1, y, 1, &result, 1, vDSP_Length(inputLength), vDSP_Length(outputLength))

    return result
}
hyperdelia
  • 1,105
  • 6
  • 26

1 Answers1

1

withUnsafeBufferPointer() gives you a pointer to the array's contiguous storage, from which you can calculate a pointer to the last array element:

func conv(x: [Float], y: [Float]) -> [Float] {
    var result = [Float](count: x.count - y.count + 1, repeatedValue: 0)

    y.withUnsafeBufferPointer { bufPtr in
        let pLast = bufPtr.baseAddress + y.count - 1
        vDSP_conv(x, 1, pLast, -1, &result, 1, vDSP_Length(result.count), vDSP_Length(y.count))
    }

    return result
}

(Note that your calculation of the result array length was not correct.)

Examples:

print(conv([1, 2, 3], y: [4, 5, 6]))
// [ 28 ] = [ 1 * 6 + 2 * 5 + 3 * 6 ]

print(conv([1, 2, 3], y: [4, 5]))
// [ 13, 22 ] = [ 1 * 5 + 2 * 4, 2 * 5 + 3 * 4 ]
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382