Post not yet marked as solved
Post marked as unsolved with 1 replies, 645 views
vDSP.convolve() reverses the kernel before applying it.
For example, the following uses a kernel of 10 elements where the first element is 1.0 and the rest of the elements are 0.0. Applying this kernel to a vector should return the same vector.
let values = (0 ..< 30).map { Double($0) }
var kernel = Array.init(repeating: 0.0, count: 10)
kernel[0] = 1.0
let result = vDSP.convolve(values, withKernel: kernel)
print("kernel: \(kernel)")
print("values: \(values)")
print("result: \(result)")
Applied to a values array containing elements 0.0, 1.0, 2.0, etc. the first results should be 0.0, 1.0, 2.0, etc, but instead the results start at 9.0 and increase from there:
kernel: [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
values: [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0]
result: [9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0]
If instead the kernel is reversed, placing the 1.0 at the end of the kernel:
let values = (0 ..< 30).map { Double($0) }
var kernel = Array.init(repeating: 0.0, count: 10)
kernel[9] = 1.0
let result = vDSP.convolve(values, withKernel: kernel)
print("kernel: \(kernel)")
print("values: \(values)")
print("result: \(result)")
The results are now correct:
kernel: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0]
values: [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0]
result: [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0]