swift - Incorrect results with vDSP_conv() -
i getting inconsitent results when attempting convolution using vdsp_conv() accelerate when compared matlab implementation. there have been couple of stackoverflow posts weird results when using function calculate convolution, far can tell, using framework correctly , have incorporated suggestions other stack overflow posts. here code:
public func conv(x: [float], k: [float]) -> [float] {       let resultsize = x.count + k.count - 1     var result = [float](count: resultsize, repeatedvalue: 0)     let kend = unsafepointer<float>(k).advancedby(k.count - 1)     let xpad: [float] = [float](count: (2*k.count)+1, repeatedvalue: 0.0)     let xpadded = x + xpad     vdsp_conv(xpadded, 1, kend, -1, &result, 1, vdsp_length(resultsize), vdsp_length(k.count)) } as far can tell, doing correct 0 padding specified in accelerate framework documentation here
i defined 2 test arrays a: [float] = [0, 0, 1, 0, 0] , b: [float] = [1, 0, 0]. 
in matlab, when run conv(a, b), [0, 0, 1, 0, 0, 0, 0]. 
however, when run above vdsp conv() get, [1, 0, 0, 0, 0, 0, 0].
what wrong implementation? have gone on number of times , looked through posts find, , still haven't been able account inconsistency.
beyond that, there more efficient method zero-pad array have here? in order keep x immutable, created new xpadded array there undoubtedly more efficient method of performing padding. 
** edit ** suggested martin r, padded k.count -1 equally @ beginning , end of array shown below.
public func conv(x: [float], k: [float]) -> [float] {     let resultsize = x.count + k.count - 1     var result = [float](count: resultsize, repeatedvalue: 0)     let kend = unsafepointer<float>(k).advancedby(k.count - 1)     let xpad: [float] = [float](count: k.count-1, repeatedvalue: 0.0)     let xpadded = xpad + x + xpad     vdsp_conv(xpadded, 1, kend, -1, &result, 1, vdsp_length(resultsize), vdsp_length(k.count))      return result } using code, conv(a, b) still returns [1, 0, 0, 0, 0, 0, 0].
i calling function shown below:
let a: [float] = [0, 0, 1, 0, 0] let b: [float] = [1, 0, 0] let c: [float] = conv(a, k: b) 
for 2 arrays a , b of length m , n, vdsp_conv() function accelerate framework computes new array of length m - n + 1.
this corresponds result of matlab function conv() shape parameter set "valid":
only parts of convolution computed without zero-padded edges. ...
to same result "full" convolution matlab have zero-pad a array n-1 elements @ beginning , end, gives result array of length m + n - 1.
applied function:
let xpad = repeat(count: k.count - 1, repeatedvalue: float(0.0)) let xpadded = xpad + x + xpad  using repeat() might slightly more performant because creates sequence , not array. ultimately, new array has created argument thevdsp_conv() function, there not room improvement.
Comments
Post a Comment