I have implemented a (direct) DFT in MatLab (following script) and compared it to the built-in FFT routine. The magnitude response seems to be identical (excluding some possibly round-off errors), but in the phase I get different results.
I can't find the source of these differences and I would like some insight on that.
script (only the case of even vector length is presented here):
%% Calculate needed variables DFT = zeros(length(data), 1); % Pre-allocate result vector n = (1:length(data))/length(data); % Calculate the n/N factor n = -1j * n * 2 * pi; % Calculate the (-j * n * 2 * pi)/N factor dftLength = (length(data)/2) + 1; % Calculate the length of unique Fourier coefficients % Calculate the coefficients for i = 1:dftLength exponent = exp((i - 1) * n); % Calculate the exponent DFT(i) = exponent * data; % Multiply the signal with the exponent % Add the conjugate symmetric part of the spectrum if i ~= 1 % Skip first bin (it's DC) DFT(2 * dftLength - i) = conj(DFT(i)); % Add conjugate symmetric bin end end You can see the resulting plot here
