From the documentation for fftfreq:
>>> signal = np.array([-2, 8, 6, 4, 1, 0, 3, 5], dtype=float) >>> fourier = np.fft.fft(signal) >>> n = signal.size >>> timestep = 0.1 >>> freq = np.fft.fftfreq(n, d=timestep) >>> freq array([ 0. , 1.25, 2.5 , 3.75, -5. , -3.75, -2.5 , -1.25])
Why are there negative values in the output array?
I am trying to produce a plot of amplitude vs frequency. I can get the amplitude by running the abs()
function over the elements of fourier
, but how do I convert freq
into a series of frequencies that I can use as an x-axis when plotting fourier
?
Advertisement
Answer
It’s inherent to FFT algorithm. The second half of FFT array is the conjugate of the first half, so don’t contain any new information.
To visualize the spectrum, just use the first half.
f=freq[:n/2] s=abs(fourier[:n/2]) plot(f,s)