6

I'm trying to understand how DFT works exactly. However, when experimenting around, I compared both Matlab generated FFT result with a dummy approach result and I get similar result.

However, the Imaginary part of both results are negated. The code below is my implementation. The implementation of dummy approach is based on http://en.wikipedia.org/wiki/Discrete_Fourier_transform#Definition

$$ X_k = \sum_{n=0}^{N-1}x_n . e^{-i2\pi kn/N}\,. $$

Fs = 500; % Sampling frequency 
T = 1/Fs; % Sample time 
N = 1000; % Length of signal
t = (0:N-1)*T; % Time vector 
x = cos(2*pi*100*t)+randn(size(t));

% Calculate by using Matlab build-in FFT 
fdft = fft(x);

% A dummy way to calculate DFT
n = 0:N-1;
k = 0:N-1;
kn = bsxfun(@times, n', k);
dummydft = x * exp(-i * 2 * pi * kn / N )';

Is there anything I miss? Thank you.

NcJie
  • 305
  • 1
  • 2
  • 6

1 Answers1

10

It should be OK. You did not specify how you obtain the vector $t$ used in generating $x$, but otherwise everything seems fine. I haven't used $\tt{bsxfun}$ but I would simply write the DFT like this: $$\tt{dummydft=x*exp(-2*pi*i*(n'*k)/N);}$$ I've tried it and it gives the same result as $\tt{fft(x)}$.

Matt L.
  • 89,963
  • 9
  • 79
  • 179
  • Hi @matt, I have just tried, the bsxfun(@times, n', k) produces the same result as (n' * k). I would prefer you way, neat! – NcJie Apr 24 '13 at 08:11
  • So do you get the same result as fft() now? – Matt L. Apr 24 '13 at 08:12
  • After comparing your equation, I found the reason. The only difference is the ' at the end. The ' makes it yields negated result in imaginary part. Though I'm not sure why. – NcJie Apr 24 '13 at 08:13
  • 2
    Ah, I just saw it right now. What ' does, it that it gives you a transpose, but in case of complex matrices, it also conjugates your matrix, i.e. it is a conjugate transpose. – Matt L. Apr 24 '13 at 08:15
  • Alright, thanks for explaining! Now I'm clear. – NcJie Apr 24 '13 at 08:18