Skip to content Skip to sidebar Skip to footer

Constant Lines Occur In Plot Of Fft With Scipy.fftpack

When I am computing a FFT with scipy.fftpack on a signal and plot it afterwards, I get a constant horizontal line (and a vertical line on my data) Can anyone explain why these line

Solution 1:

You are right that this line joins the first and last data. As previously explained by @SleuthEye, standard fft function prefers to plot the positive frequencies first, then the negative frequencies.

It is very easy to fix this problem by adding the third and fourth lines, as follows

fftData = np.fft.fft(data)
freq = np.fft.fftfreq(lenData, 1/fSamp)
fftData = np.fft.fftshift(fftData)
freq = np.fft.fftshift(freq)

Solution 2:

The data from the FFT includes the positive frequencies first, then the negative frequencies. The line you are seeing is the line that connects the last point of the positive frequencies to the first point of the negative frequencies.

To avoid this, you can swap the two halfs of the spectrum so that the data occurs in the natural order:

plt.plot(scipy.fftpack.fftshift(freq), scipy.fftpack.fftshift(fft))

Post a Comment for "Constant Lines Occur In Plot Of Fft With Scipy.fftpack"