Complex numbers in ifft(fft(x))

4 views (last 30 days)
Hi, i want to realize a convolution through fft and i also want to compare the time required for this process between gpu and cpu. Here is the code:
len=100000;
x=randi(50,1,len);
y=randi(50,1,len);
tic
l=gpuArray([x zeros(1,len-1)]);
m=gpuArray([y zeros(1,len-1)]);
c_1_temp=ifft(fft(l).*fft(m));
toc
c_1=gather(c_1_temp);
tic
l_1=[x zeros(1,len-1)];
m_1=[y zeros(1,len-1)];
c_2=ifft(fft(l_1,2*len-1).*fft(m_1,2*len-1));
toc
But strangely i've found this result:
c_1= 559,999999985352 - 4,88283691418457e-09i 1655,00000000398 - 1,39078548907368e-08i 3569,99999999371 + 7,06640683828822e-09i 5104,99999999144 + 2,13079904230657e-08i 6494,99999999661 - 1,24666866807290e-08i 7888,00000000453 - 3,32333192044652e-08i 8864,99999998867 - 4,37161194092030e-09i 8850,99999999144 - 1,35629723232990e-08i 8513,99999995740 - 1,81275757497787e-08i 8608,99999998969 + 3,02029185265248e-08i 8493,99999997188 - 1,37329756956340e-08i
and
c_2= 559,999999995117 1655,00000000488 3569,99999998047 5105 6494,99999998047 7888,00000001587 8864,99999998657 8850,99999997559 8513,99999997314 8609,00000001221 8494,00000000122
The same operation for gpu gives complex value which is approximately real.
WHY?

Accepted Answer

Joss Knight
Joss Knight on 22 Mar 2018
I'm not sure but I assume it's because the GPU FFT computation is non-deterministic. The order of operations is not preserved between FFT and IFFT and so round-off creeps in, whereas on the CPU everything cancels out perfectly. You can enforce symmetricity in the IFFT using the 'symmetric' option.
  1 Comment
Damiano Capocci
Damiano Capocci on 22 Mar 2018
Edited: Damiano Capocci on 22 Mar 2018
Yes absolutely. Thank you.
ps: with the option symmetric is all ok but is slower.

Sign in to comment.

More Answers (1)

Edric Ellis
Edric Ellis on 22 Mar 2018
The gpuArray implementation of the fft family of functions always returns complex results. This is described in the help text, accessed via help gpuArray/fft. The reason for this is that the output of the fft operation might need to be complex, and to be efficient, the implementation chooses always to return complex data. (The alternative is to perform the fft and then delete the imaginary part if it turns out to be all-zero - the gpuArray methods differ from built-in MATLAB methods in that they never do this).

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!