constant lines occur in plot of fft with scipy.fft

2019-07-29 16:30发布

问题:

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 lines occur and maybe present a solution to plot the spectrum without the lines?

Let's take a look at a simple signal. Sinusoidal wave with a frequenz of 12 Hertz. In this example you can see the horizontal line at about y = 2.1.

import numpy as np
import matplotlib.pyplot as plt
import scipy.fftpack

# test signal
omega = 2 * np.pi * 12
t = np.linspace(-15, 15, 1000)
ampl = np.sin(omega * t)

# compute fft, fft frequences
fft  = abs(scipy.fft(amp))
freq = scipy.fftpack.fftfreq(fft.size, t[1] - t[0])

# plot
plt.plot(freq, fft)

And here's the fft plot from my experimental signal (zoomed in). You can see the horizontal line (y =~ 0.0015) and also a vertical line (at x = 0).

I cannot find any data in my arrays according to these lines. Why do these lines occur?

回答1:

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))


标签: python fft