I was using this post as a guide on determining the dominating frequencies given some discrete data set.
One of the solutions was posted as follows:
x = np.linspace(0,5,100)
y = np.sin(2*np.pi*x)
## fourier transform
f = np.fft.fft(y)
## sample frequencies
freq = np.fft.fftfreq(len(y), d=x[1]-x[0])
plt.plot(freq, abs(f)**2) ## will show a peak at a frequency of 1 as it should.
This worked for me but I tried increasing the frequency from 1 hz to 100 hz, and I get some bizarre plots that don't reflect anything. I also tried adding another wave of frequency 5 hz in it, and I didn't get the result that I would expect.
My question is how do I go about fixing this? I've tried reading documentation on this, but I'm still confused on what is actually going on. From what I understand, the fft.fft(y) takes the fourier transform while the fft.fftfreq makes frequencies? Why does it take the length as a parameter? And what exactly is the role of d? Finally, why do we plot abs(f)**2?