Magnitude of Complex Array

[Old posts from the commercial version of ArrayFire] Discussion of ArrayFire using CUDA or OpenCL.

Moderator: pavanky

Magnitude of Complex Array

Postby neuralPanther » Sun Feb 23, 2014 11:52 pm


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>();

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!
Posts: 25
Joined: Fri Feb 14, 2014 8:03 pm

Re: Magnitude of Complex Array

Postby pavanky » Mon Feb 24, 2014 1:25 am


Can you tell us why you think arrayfire is not doing the right abs for af::abs ?

Also the correct way to find the abs value is to do sqrt(A * conj(A)); Matmul between A and A' results in a complex hermitian matrix.
Pavan Yalamanchili,
~ If it is not broken, you have not tried hard enough ~
User avatar
Site Admin
Posts: 1123
Joined: Mon Mar 15, 2010 7:39 pm
Location: Atlanta, GA

Re: Magnitude of Complex Array

Postby neuralPanther » Mon Feb 24, 2014 1:07 pm

Looks like my issue with ABS had more to do with Visual Studio debugging and not clearing my host array :oops:

I had been getting zeros in strange places and negative numbers - that's all good now.

Thanks again,

~ NP
Posts: 25
Joined: Fri Feb 14, 2014 8:03 pm

Return to [archive-commercial] Programming & Development with ArrayFire