From my understanding, the following code creates a 1 second long sine wave sampled at 256 Hz, meaning a Nyquist rate of 128 Hz. So if a sine wave is having a frequency of 100 Hz, it should not experience aliasing.
t = np.linspace(0,1,256)
x = np.sin(2*np.pi*100*t)
plt.plot(t,x)
However, the plot looks something like this, with what I believe is aliasing?
Is there anything I’m doing wrong? what’s the reason for this?
>Solution :
First, your code is incorrect. You are sampling at 255Hz, not 256. To fix,
t = np.linspace(0, 1, 256, endpoint=False)
OR
t = np.linspace(0, 1, 257)
Here is a zoomed-in version of your (corrected) plot, with the sine wave shown at a much higher sampling frequency:
t = np.linspace(0, 1, 256, endpoint=False)
x = np.sin(2 * np.pi * 100 * t)
plt.plot(t, x)
plt.plot(t2 := np.linspace(0, 1, 10000), np.sin(2 * np.pi * 100 * t2), 'r:')
You are getting at least one point per half-cycle, which means that you can estimate the true frequency meaningfully from this data. Here is a similar dataset sampled at exactly Nyquist (200Hz):
Sampling with a different phase will move the line up and down, but it won’t allow you to find meaningful information about the input signal.
Aliasing looks like this (sampling at 45Hz):
Since the sampling frequency is so much lower than the signal frequency, you end up with an estimate that is much lower.