I'm trying to do 1D convolution with Accelerate framework.
I can make it work but it seems like something goes wrong after a few experiments.
Here is my code:
import Accelerate
var N = 10000
var M = 10
var convN = N + M - 1
var xf = [Float](count:N, repeatedValue:0.0)
var yf = [Float](count:M, repeatedValue:0.0)
for i in 0..<N {
xf[i] = Float(i+1)
}
for i in 0..<M {
yf[i] = Float(i+1)
}
var outputf = [Float](count:convN, repeatedValue:0.0)
// padding zero
xf = [Float](count:convN-N, repeatedValue:0.0) + xf
var ptr_xf = UnsafePointer<Float>(xf)
var ptr_yf = UnsafePointer<Float>(yf).advancedBy(yf.count-1)
vDSP_conv(ptr_xf, 1, ptr_yf, -1, &outputf, 1, vDSP_Length(convN), vDSP_Length(M))
print("[Float]: \(outputf)")
I compile and run it with different N
and M
.
Then I check the result with the result from python package numpy
.
Sometime it looks great but sometime it just give me some weird result like:
( N
=6, M
=3)
Correct: [1.0, 4.0, 10.0, 16.0, 22.0, 28.0, 27.0, 18.0]
Result I get: [1.0, 4.0, 10.0, 16.0, 22.0, 28.0, 2.23692e+08, 4.47384e+08]
Do I miss anything here? I can not figure out what I've done wrong.
Does it have something to do with your Swift Version ? I am using V 3.0.2 and the following code works well for me.
import Accelerate
var N = 6
var M = 3
var convN = N + M - 1
var xf = [Float](repeating:0.0, count:N)
var yf = [Float](repeating:0.0, count:M)
for i in 0..<N {
xf[i] = Float(i+1)
}
for i in 0..<M {
yf[i] = Float(i+1)
}
var outputf = [Float](repeating:0.0, count:convN)
// padding zero
xf = [Float](repeating:0.0, count:convN-N) + xf
var ptr_xf = UnsafePointer<Float>(xf)
var ptr_yf = UnsafePointer<Float>(yf).advanced(by: yf.count-1)
vDSP_conv(ptr_xf, 1, ptr_yf, -1, &outputf, 1, vDSP_Length(convN), vDSP_Length(M))
print("[Float]: \(outputf)") // [Float]: [1.0, 4.0, 10.0, 16.0, 22.0, 28.0, 27.0, 18.0]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.