Home:ALL Converter>Fourier transform analysis with Python

Fourier transform analysis with Python

Ask Time:2018-04-07T10:22:18         Author:Ayumu Kasugano

Json Formatter

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?

Author:Ayumu Kasugano,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/49703211/fourier-transform-analysis-with-python
yy