1

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?

Duck
  • 34,902
  • 47
  • 248
  • 470
  • Sorry I am just asking.. Why it is not working var myArr = [Float]() var arr = [DSPComplex]() for number in myArr { var dsp = DSPComplex(real: number, imag: 0) arr.append(dsp) } cant you pass arr as an UnsafePointer ? – Said Alır Feb 11 '19 at 20:43
  • Because I am new to swift and had no idea about this command? Please add this as an answer, so I can accept. thanks. – Duck Feb 11 '19 at 20:50

2 Answers2

1

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)
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
0

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.

Said Alır
  • 170
  • 2
  • 15
  • https://stackoverflow.com/questions/32606989/converting-an-unsafepointer-with-length-to-a-swift-array-type You can check this out :) – Said Alır Feb 11 '19 at 20:59
  • 2
    While this answer technically works, one should be aware that this causes an insane amount of overhead, as this A) manually copies all elements from the source array into the destination one, and B) creates an array of `DSPComplex`, only to be converted back (via `vDSP_ctoz`) into a `DSPSplitComplex` struct, which internally holds a regular `Float` array of the real part. Especially in DSP applications this additional time might not be available. – max Mar 05 '19 at 15:46