I am trying to write a program to compute the magnitude and phase of a specific, non-integer frequency component (i.e. given a sampled finite signal of length $N$, I want to know the magnitude and phase of the spectrum at frequency 40.678 Hz).

I tried using the equation for the Discrete Time Fourier Transform:

$$ X(\omega) = \sum_{n=-\infty}^{\infty} x[n] e^{-j\omega n} = \sum_{n=0}^{N-1} x[n] e^{-j\omega n} $$

such that

$$ X(2\pi\cdot40.678) = \sum_{n=0}^{N-1} x[n] e^{-j 2\pi\cdot40.678\cdot n} $$

I did this as follows:

t = np.linspace(0,1,100)
x = np.sin(2*np.pi*40.678*t)

Xf = 0
for n in range(len(x)):       
    Xf += (x[n]*np.exp((-np.complex(0,1))*2*np.pi*40.678*n))

Which printed $0.6149015687259044$.

I know that this is wrong because if I do an FFT as follows:

X = np.fft.fft(x)

I can see peaks of magnitude $50$ at around (what I assume after correcting the axis is) 40.678 Hz.

enter image description here

What is the reason for this difference? I also tried the equation of the DFT just like I did with the DTFT code above this time to measure magnitude at 50 Hz of a sinusoid of frequency 50 Hz and I got a completely wrong value again as compared to what the plot of the FFT shows.

What causes this discrepancy?

Thank you.



Try introducing the sampling frequency $F_s$. Then $f$ in the relation in Equation $(1)$ is relative to the sampling frequency $$ \omega = 2\pi f\quad\text{where}\quad f = \frac FF_s\tag{1} $$ Where the units are as follows:

  • $\omega$ in [radians/sample]
  • $f$ in [cycles/sample]
  • $F_s$ in [samples/second]
  • $F$ in [cycles/second] or [Hz]

Your discrete time vector for your signal should be as shown in equation $(2)$ $$ t = nT_s\quad\text{where}\quad T_s = \frac 1F_s\quad\text{and}\quad n = 0, \ldots, N - 1\tag{2} $$ You seem to use the frequency $F$ in [Hz] instead of the relative frequency $f$ in [cycles/sample].

  • $\begingroup$ Thank you very much! $\endgroup$ – Thomas Gwasira Jan 14 at 12:12

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.