1

I've been working on some vDSP code and I have come up against an annoying problem. My code is cross platform and hence uses std::complex to store its complex values.

Now I assumed that I would be able to set up an FFT as follows:

DSPSplitComplex dspsc;
dspsc.realp = &complexVector.front().real();
dspsc.imagp = &complexVector.front().imag();

And then use a stride of 2 in the appropriate vDSP_fft_* call.

However this just doesn't seem to work. I can solve the issue by doing a vDSP_ztoc but this requires temporary buffers that I really don't want hanging around. Is there any way to use the vDSP_fft_* functions directly on interleaved complex data? Also can anyone explain why I can't do as I do above with a stride of 2?

Thanks

Edit: As pointed out by Bo Persson the real and imag functions don't actually return a reference.

However it still doesn't work if I do the following instead

DSPSplitComplex dspsc;
dspsc.realp = ((float*)&complexVector.front()) + 0;
dspsc.imagp = ((float*)&complexVector.front()) + 1;

So my original question still does stand :(

Goz
  • 61,365
  • 24
  • 124
  • 204

2 Answers2

4

The std::complex functions real() and imag() return by value, they do not return a reference to the members of complex.

This means that you cannot get their addresses this way.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • Ahhh hadn't noticed that! Cheers! So it wouldn't work anyway ... bugger. – Goz Sep 24 '11 at 11:05
  • According to C++11 you should be able to cast a pointer to a std::complex into a pointer to an array of two floats (or doubles). We don't know if that works with earlier compilers though. – Bo Persson Sep 24 '11 at 11:29
-1

This is how you do it.

const COMPLEX *in = reinterpret_cast<const COMPLEX*>(std::complex);

Source: http://www.fftw.org/doc/Complex-numbers.html

EDIT: To clarify the source; COMPLEX and fftw_complex use the same data layout (although fftw_complex uses double and COMPLEX float)

Albin Stigo
  • 2,082
  • 2
  • 17
  • 14
  • What has FFTW got to do with vDSP? – Goz Jul 18 '15 at 22:42
  • COMPLEX and fftw_complex use the same data layout (although fftw_comples uses doubles and COMPLEX floats). I have verified this cast and it works with the vDSP_* functions. – Albin Stigo Jul 19 '15 at 10:02
  • Absoloutely not. `DSPSplitComplex` is required by the `vDSP_fft_*` functions and is a non-interleaved format. `std::complex` and FFTW's `COMPLEX` are both interleaved. Sure you can use `vDSP_ctoz`/`vDSP_ztoc` but the whole point of this question is to avoid doing this. – Goz Jul 19 '15 at 12:26
  • My comment is correct, the types `COMPLEX` and `fftw_complex` represent the same data layout. However, I misunderstood your question. I'm well aware that `vDSP_fft_*` needs `COMPLEX_SPLIT` (DSPSplitComplex) and `vDSP_ctoz`. I guess one workaround for you could be to use fftw instead of Accelerate since it would allow you to use an interleaved layout. They perform similary for small inputs. Benchmark: https://gist.github.com/admsyn/3452792 – Albin Stigo Jul 19 '15 at 17:48
  • Except I either need to buy a license for FFTW or GPL my commercial iPhone based products. As such thats really not a useful suggestion. – Goz Jul 19 '15 at 18:40
  • And that license is $15,000!! http://technology.mit.edu/technologies/15054_fastest-fourier-transform-in-the-west-fftw-version-3-3-x-fftw-v-3-3-x – Goz Jul 19 '15 at 18:57
  • This is another one I'm aware of (BSD license) but I'm not sure which data layout it uses since I've never used it. https://github.com/anthonix/ffts paper here: http://www.cs.waikato.ac.nz/~ihw/papers/13-AMB-IHW-MJC-FastFourier.pdf – Albin Stigo Jul 19 '15 at 19:08
  • And while FFTS is very interesting it has no ARM64 support so cannot be used on an iPhone app. – Goz Jul 20 '15 at 08:34
  • Ok.. Seems to have some iOS build scripts..? Are those only optimized for older ARM architectures? – Albin Stigo Jul 20 '15 at 12:40
  • There is no straight C implementation and it relies heavily on SIMD assembly which is pretty different between 32-bit and 64-bit ARM processors. I'm sure it could be converted to 64-bit assembly but its a significant change and I do not have the time to do it. – Goz Jul 20 '15 at 14:09