1
$\begingroup$

edit: This is my first post in this community. I'm sure that my downvoter had a good reason to do so, but could someone please comment and tell me how I can better format my question?

--

I have a DFT algorithm that almost makes sense. The issue is when I go to plot the frequency response. For some reason, the frequency response gets multiplied by the sampling period (see first image).

Understanding this, I can fix it by dividing my frequency response X-axis by sampling period (see second image), but I don't understand why this is an issue in the first place.

Code is posted below - can anyone explain what I am missing? (example uses a 1 Hz sinusoid)

Here's how I think it should work: enter image description here

But here's what I actually have to do to make it work: enter image description here

x_t = sin( 2 * pi * t);     % Input: 1 Hz Sinusoid
s_f = zeros(N,length(x_t)); % Output: Freq spectrum      

%%%% init params
    T    = 4;     % (sec) time window
    dt   = 0.1;   % (sec) sample time

%%%% init comp
        N = T / dt;          % (ct) num samples
        n = 0 : N - 1;       % (ct) bucket index vector
        t = 0 : dt : T - dt; % (sec) time vector
smpl_freq = 1 / dt;          % (Hz) sampling frequency
 freq_res = smpl_freq / N;   % (Hz) frequency resolution
        f = n * freq_res;    % (Hz) freq. bucket vector
        b = 2*pi*(f/N) .* n.';

    anlyz_fn = cos(-b) + i * sin(-b);

%%%% run DFT
for k = 1 : N
    val = 2 * sum(anlyz_fn(k,:) * x_t.');
    s_f(k) = val;
end

%%%% Plot Results
subplot(2,1,1);
plot(t, x_t);

subplot(2,1,2);
stem(f(1:N)/T , abs(s_f(1:N))/N);
$\endgroup$
1
$\begingroup$

I don't know why you got downvoted. This is actually a very pertinent question having to do with normalization conventions.

The most conventional DFT formula is this:

$$ X[k] = \sum_{n=0}^{N-1} x[n] e^{-i \frac{2\pi}{N} n k } $$

The $1/N$ normalized form is

$$ X[k] = \frac{1}{N}\sum_{n=0}^{N-1} x[n] e^{-i \frac{2\pi}{N} n k } $$

And the $1/\sqrt{N}$ normalized form is

$$ X[k] = \frac{1}{\sqrt{N}}\sum_{n=0}^{N-1} x[n] e^{-i \frac{2\pi}{N} n k } $$

My very strong preference is to use the $1/N$ normalized version. This makes the bin values work as you expected them to. It also makes the expected bin values at whole integer frequencies bin count invariant, and leakage values comparable.

If you do not apply a $1/N$ in the forward DFT, you will need it on the inverse to get your original signal back. If you put $1/N$ on the DFT, then the inverse needs no rescaling (and the bin values serve as coefficients for continuous Fourier series).

The $1/\sqrt{N}$ is the "proper mathematical" one as it makes the forward and inverse DFT unitary matrix multiplications. Yeah, nobody does that.

Bottom line, I think the $1/N$ normalization should be the standard convention and I write all my blog articles using that definition.

BTW, you will get a magnitude of $1/2$ for pure whole integer tones on $1/N$ normalized DFTs. The other half is on the other side of the circle.

$\endgroup$
  • $\begingroup$ i agree, the OP shouldn't have been downvoted. to me, the answer lies with reconciling the DFT definition to the CFT definition using the notion of the Riemann integral. but i would think you multiply by $T$ (if that is the sample period) or by $T/N$ (if $T/N$ is the sampling period) to make it dimensionally consistent with the CFT integral (you know, the $\int ... \mathrm{d}t$). $\endgroup$ – robert bristow-johnson Jan 24 at 20:21
  • $\begingroup$ Thanks a lot, this was really helpful. Actually, I've been trying to get a more efficient algorithm working but cant seem to get it right. Would you mind taking a look? dsp.stackexchange.com/questions/55168/… $\endgroup$ – darkpbj Jan 29 at 21:17

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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