I'm trying to implement DFT/IDFT and FFT/IFFT in Matlab and C.

I got wrong values while I implement IDFT/IFFT. So I'm trying to find a mistake. https://kr.mathworks.com/help/matlab/ref/fft.html

Just for DFT, I used this algorithm to implement it in Matlab (from matlab's fft doc) But I really don't know why there is very small errors when I run this code. and even my Radix-2 FFT have same problems. Is there anything I'm missing or can I ignore this values? (mean of 256 values : -1.6881e-25)

Thank you for reading this.

for i=1:256
    x(i) = sin(2*pi*i*80) + sin(2*pi*i*46);

X = zeros(256,1);
for k=1:256
   for n=1:256
       X(k)=X(k) + x(n)*exp((-j*2*pi*(n-1)*(k-1))/256);

built_in = fft(x);
error = abs(X) - abs(built_in)';

plot(abs(X)); hold on
legend('My code', 'Matlab','error');

with result:

enter image description here


The reason why the two results are not exactly the same is because you compute the same result in two different ways. Direct implementation of the DFT is numerically different from the FFT algorithm, so you must expect different round-off behavior, resulting in (negligible) differences in the result.

  • $\begingroup$ I just became much clear. I really appreciate your help. $\endgroup$ Aug 21 '19 at 7:08

Your error is the yellow line at the bottom. i see no difference between your DFT and the built-in FFT from MATLAB.

  • $\begingroup$ That's exactly what I thought, but the values are exist so I was confused. Thank you for your answer. and editing :) $\endgroup$ Aug 21 '19 at 6:43
  • $\begingroup$ the value of the error is ridiculously close to zero. use the zoom in feature of MATLAB figures to see the difference between the blue curve and the brown one. $\endgroup$ Aug 21 '19 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.