问题
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