0
$\begingroup$

I hope this isn't such a dumb question, but I'm finally getting to grips with the inner workings of the DFT.

What I'm having trouble understanding is why the basis complex sinusoids in the "forward" DFT are conjugated, and why the inverse DFT uses non-conjugated complex sinusoids.

I've written a script in MATLAB that attempts to implement what I understand about the DFT (not FFT, I know a fast function for that already exists). I notice that based on this conjugation issue I'm having, when I run a real sinusoid of some frequency through my DFT script, then perform the inverse part I get back the original sinusoid but at what looks like a quarter cycle out of phase.

Can anyone clarify what's going on for me?

(here's my MATLAB code snippet):

%% setup a test sinusoid
Fs = 44100;
freq = 440.0;
amp = 1.0;

% setup col signal vector
x = generate_sine(freq, 0, amp, Fs, Fs);

% set length of DFT (# of bins/frequency resolution)
N = 2^13;

% get bin resolution from sampling frequency: acts as
% a Kth test frequency multiplier for DFTs shorter than
% the sampling frequency
bin_width = Fs/N;

% truncate length of input signal to size of DFT (bins)
x(N+1:end) = [];

% plot original, truncated signal
figure(1)
subplot(3,1,1)
title('original signal')
stem(x, '.');

%% perform DFT using N test sinusoids (bins)
% This for loop represents the summation process of the DFT.
%   -i is an iterator where:
%       } i = 0 frequency = DC,
%       } and i > 0 = Kth sinusoid frequency
%   -in each loop iteration a new test complex sinusoid is
%    generated based on i iterator which is used a the 
%    frequency number.
%   -in each loop iteration the dot product of the current test 
%    sinusoid is taken, then stored in a vector of frequency bins
for i = 0: N-1

    % set current test frequency (Kth frequency)
    k = bin_width * i;

    % generate a complex sinusoid vector
    cp_sin = complex(generate_sine(k, 0, 1, Fs, N) / (N/2), generate_sine(k, pi/2, 1, Fs, N) / (N/2));     

    % store dot product result in bin number i
    bins(i+1) = dot(cp_sin,x);
    bins = bins.';
end

% plot DFT result
subplot(3,1,2)
title('DFT')
plot(abs(bins));
xlim([0 N/2]);

%% perform inverse DFT
for i = 0:N-1

    % set current test frequency (Kth frequency)
    k = bin_width * i;

    % generate a sinusoid
    cp_sin = complex(generate_sine(k, 0, 1, Fs, N) * (N/2), generate_sine(k, pi/2, 1, Fs, N) * (N/2)); 

    % perform inverse transform to get original signal back
    xi(i+1) = (dot(bins,cp_sin))/N;
    xi = xi.';
end


%% plot inverse DFT result
subplot(3,1,3)
title('IDFT')
stem(real(xi), '.');
ylim([-1 1]);
$\endgroup$
  • $\begingroup$ Welcome to DSP.SE! The inverse operation should give you back what you started with, modulo finite precision effects. $\endgroup$ – Peter K. Apr 29 '16 at 2:03
  • $\begingroup$ btw you can rewrite the DFT as the inner product of the signal and real-valued sine and cosine waves, the complex exponential just gives a more convenient expression. $\endgroup$ – geometrikal Apr 29 '16 at 6:14
1
$\begingroup$

Really the DFT is just the inner product of your signal with complex exponentially of various frequencies.

Taking the inner product of complex vectors requires taking the complex conjugate.


Reading your code, I do not believe this is correct:

cp_sin = complex(generate_sine(k, 0, 1, Fs, N) / (N/2), generate_sine(k, pi/2, 1, Fs, N) / (N/2));     

shouldn't it make a complex conjugate:

cp_sin = complex(generate_sine(k, 0, 1, Fs, N) / (N/2), -generate_sine(k, pi/2, 1, Fs, N) / (N/2));     

where I've placed a "-" in front of the second generate_sine call.

Also, can you let us know what the arguments to generate_sine do. I'm not sure what the third and fourth arguments achieve?

$\endgroup$
  • $\begingroup$ Yeah I realized I needed to review taking the inner product of complex vectors. So is it that the input signal gets treated like a complex vector where every element has the 0i component? If this is the case then if I'm using MATLAB to take the inner product of the test signal and a Kth complex sinusoid, would it look something like dot(complex_sinusoid, test_sig)? $\endgroup$ – TSIguy Apr 29 '16 at 4:16
  • $\begingroup$ @TSIguy Your code above looks fine... modulo a couple of quibbles I have; see my update. $\endgroup$ – Peter K. Apr 29 '16 at 11:46
  • $\begingroup$ generate_sine taes in a frequency in Hz, phase offset, amplitude value, sampling frequency, and signal length all respectively. Also I originally tried your suggestion of negating the complex part, but I wasn't sure if the dot() function automatically takes the conjugate of a complex vector. I've since switched some things around and now I get the proper results. Thanks! $\endgroup$ – TSIguy Apr 29 '16 at 17:20

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.