Converting an array of Floats to an array of UnsafePointer<DSPComplex>

走远了吗. 提交于 2019-12-11 07:19:03

问题


I have this array of floats created like this

var myArray : [Float] = []

This array has 256 elements, the real part. All imaginary parts are zero.

I need to do a

vDSP_ctoz(anArray, 2, &output, 1, vDSP_Length(n/2))

but this API requires anArray to be UnsafePointer<DSPComplex>

How I convert myArray to this format?


回答1:


normal arrays can pass as UnsafePointer

So this snippet should work,

var myArr = [Float]()
var arr = [DSPComplex]()
for number in myArr {
     var dsp = DSPComplex(real: number, imag: 0)
     arr.append(dsp) 
} 

Just pass this the arr.




回答2:


If the intention is to fill a DSPSplitComplex from the given real parts and zero imaginary parts then you don't need to create an array of interleaved complex numbers first and then call vDSP_ctoz(). You can allocate the memory and fill it directly from the Float array:

let realParts : [Float] = [1, 2, 3, 4]
let len = realParts.count

let realp = UnsafeMutablePointer<Float>.allocate(capacity: len)
realp.initialize(from: realParts, count: len)
let imagp = UnsafeMutablePointer<Float>.allocate(capacity: len)
imagp.initialize(repeating: 0.0, count: len)

let splitComplex = DSPSplitComplex(realp: realp, imagp: imagp)


来源:https://stackoverflow.com/questions/54638420/converting-an-array-of-floats-to-an-array-of-unsafepointerdspcomplex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!