Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Aliasing in Python even though under Nyquist rate

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?

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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:')

enter image description here

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):

enter image description here

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):

enter image description here

Since the sampling frequency is so much lower than the signal frequency, you end up with an estimate that is much lower.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading