I am trying to write a basic program that samples a 4 kHz sinewave at a sampling rate of 8 kHz and takes the FFT of the signal and plots it.

From everything I have read, as long as the signal you are sampling has frequency content that is less than or equal to Fs/2 no aliasing will occur and the results will be accurate. However writing a simple example seems to be more complicated than I thought.

Using Python I wrote up a basic example:

import matplotlib.pyplot as plt
import numpy as np

Fs = 8000.0
Ts = 1/Fs
N = 8
t = np.arange(0, N*Ts, Ts)

# 4 kHz sinewave
y = np.sin(2*np.pi*4000*t)

# Bandwidth of the signal (Hz)
BW = Fs/2

# Spectral Lines (number of frequency samples)
SL = round(N/2 + 1)

# Frequency scale
f = np.linspace(0, BW, SL)

Y = np.fft.fft(y)*2/N
Y = Y[:SL]

fig, ax = plt.subplots(2, 1)

ax[0].stem(t, y, use_line_collection=True)

ax[1].stem(f, abs(Y), use_line_collection=True)
ax[1].set_xlabel('Freq (Hz)')


Looking at the output you can see that this does not properly capture the frequency content at 4 kHz in the FFT plot.

enter image description here

But if I simply add a phase shift to the signal by changing this line

y = np.sin(2*np.pi*4000*t)


y = np.sin(2*np.pi*4000*t + np.pi/2)

I end up with much better results...

enter image description here

My question is, how come this doesn't work without a phase shift? It makes me wonder how anyone can be confident in the result of an FFT when the signal being sampled has some frequency content that is at the frequency Fs/2. How do you ensure that the signal you are analyzing is at a phase alignment that will not cause issues?

  • 1
    $\begingroup$ at the Nyquist frequency, you have a special aliasing that removes any information you may have on phase. $\endgroup$ – robert bristow-johnson Jul 30 '19 at 2:02
  • $\begingroup$ I read that section and tried to comprehend it, but Wikipedia is a little too dense for my level of experience lol. Is there any way you could help break it down a little? $\endgroup$ – tjwrona1992 Jul 30 '19 at 2:36

the discrete function $$x_q[n]=\sin(\pi n)$$ is always zero for all of the integers $n$.

the discrete function $$x_i[n]=\cos(\pi n)$$ is always $(-1)^n$ for integer $n$.

so this general sinusoid at Nyquist that has a phase term:

$$\begin{align} x[n] &= A \cos(\pi n + \theta) \\ &= A \big( \cos(\pi n) \cos(\theta) - \sin(\pi n) \sin(\theta) \big) \\ &= \big(A\cos(\theta)\big) \cos(\pi n) + \big(-A\sin(\theta)\big) \sin(\pi n) \\ &= \big(A\cos(\theta)\big) (-1)^n + \big(-A\sin(\theta)\big) \cdot 0 \\ &= B (-1)^n \\ \end{align}$$

So now, how do you tell the difference between a sampled sinusoid that had amplitude $A$ and a phase angle $\theta$ and another sampled sinusoid that has amplitude $B \triangleq\big(A\cos(\theta)\big)$ and no phase shift from the cosine?

| improve this answer | |
  • $\begingroup$ I guess that makes sense, but in this case where I know my input signal has an amplitude of 1 with frequency content at Fs/2 how do I determine the optimal phase shift to apply to get the most accurate results in my amplitude measurement? $\endgroup$ – tjwrona1992 Jul 30 '19 at 15:41
  • $\begingroup$ Also does this ambiguity only apply EXACTLY at the Nyquist frequency? What if I wanted to calculate the frequency content in a 3999 Hz sinewave. Will this still have the same issues? If so, how far from the Nyquist frequency do I have to be before these issues are entirely negligible? $\endgroup$ – tjwrona1992 Jul 30 '19 at 15:50

how anyone can be confident in the result of an FFT when the signal being sampled has some frequency content that is at the frequency Fs/2.

You can't. In practice you need a healthy margin between the highest frequency of interest and the Nyquist frequency. In audio for example the highest frequency is typically 20 kHz but you sample at 44.1 kHz or 48 kHz. The region between 20 kHz and the Nyquist frequency is basically unusable: it's a transition between the pass band and the stop band that prevents aliasing.

| improve this answer | |
  • $\begingroup$ Do you know at exactly what point (how close you need to get to Nyquist) before the phase alignment becomes a problem? $\endgroup$ – tjwrona1992 Jul 30 '19 at 15:55
  • $\begingroup$ That really depends on the requirements of your specific application. What type of constraints do you have, what exactly do you consider a "phase alignment" problem, and what do your anti-aliasing filters look like. $\endgroup$ – Hilmar Jul 30 '19 at 16:21
  • $\begingroup$ I'm currently not using any filters, but I guess what I'm asking is what would be the lowest frequency at which phase shift has any effect on the magnitude spectrum output? I would expect the amplitude of the sinewave to be matched in the magnitude plot. $\endgroup$ – tjwrona1992 Jul 30 '19 at 19:07

It is a similar question as the one I have asked a few weeks before, and I received a nice answer -> Shannon-Nyquist theorem reconstruct 1Hz sine wave from 2 samples

I was trying to do the reverse: reconstruct a 1Hz sine wave from only 2 samples.

| improve this answer | |

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.