I've been trying to get the magnitude of a complex array in C++ and I haven't had much luck.

Here's what I've tried (and the results)

- Code: Select all
`// Calculate FFT:`

double* d_signal = (double*) arrayF_signal.device<double>();

MyCustomKernel(d_signal);

arrayF_signal.unlock();

af::array fft_signal = af::fft(arrayF_signal, signalSize) // signalSize is a power of 2 - for testing purposes 2048

/*since I'm plotting the FFT I need to normalize it and massage it - equivalent to

output = 20*log10(abs(FFT(signal)); % in MATLAB

*/

I'm stuck on the "Abs" part - in MATLAB abs(complexArray) provides me with the magnitude of the complex array.

I need to do the same thing with arrayFire.

I found out quickly that af::Abs does not seem to perform a complex magnitude calculation (i.e. sqrt(R^2 + I^2))

next I tried:

- Code: Select all
`arrayF_signal = af::matmul(af::conjg(fft_signal), fft_signal,af_none, af_trans);`

I assumed this would perform a matrix multiply cross product (element wise multiplication) of the input signal and its complex conjugate. However this call resulted in negative numbers which shouldn't be possible.

Is there a "right" (simple) way, in arrayFire c++, of getting the magnitude of a complex array?

I'm sure I'm missing something here - but I've not found any further hints in the documentation or examples for solving this.

Thank you!