Jim GB
Jim GB

Reputation: 93

numpy Fourier transformation produces unexpected results

I am currently learning Fourier transformation and using Python to play around with it.

I have a code snippet here:

x = np.arange(0,50,0.1)
T = 5
y = np.sin(T*np.pi*x)
freq = np.fft.rfftfreq(x.size)
y_ft = np.fft.rfft(y)
plt.plot(freq, np.abs(y_ft))

It produces a correct chart as following:

enter image description here

But when I change the T into 10, the chart is like this: enter image description here

I was expecting that I will get a similar chart like the first one with a right shift of the peak, because I just enlarged the cycle time.

Why increasing the cycle time would produces such an unexpected result?

Upvotes: 1

Views: 118

Answers (1)

chthonicdaemon
chthonicdaemon

Reputation: 19770

You are effectively sampling a signal. With your code, the frequency you are sampling at is 1/0.1 or 10 rad/second. The frequency of your first sinusoid is just on the Nyquist frequency (5 rad/second). The frequency of your second sinusoid is beyond Nyquist, therefore your signal is not correctly sampled. Solution: increase your sampling frequency (x = np.arange(0, 50, 0.01) for example).

Look at what your T=10 signal looks like when plotted (you can see it doesn't resemble a single sinusoid at the sampling points):

enter image description here

Upvotes: 4

Related Questions