I'm new to Python and signal processing, and I'm having a problem with FFT.
I'm supposed to analyze a set of data and find the modulation frequencies from it. I wrote a basic FFT script to do this, and the output looked kinda weird. It does show the peaks like a normal FFT graph. However, for each line it has a horizontal line that connects the two ends, instead of the ends spreading out.
I would like to ask what might be the problem here.
This is my script:
from numpy.fft import fft, fftfreq
import matplotlib.pyplot as plt
Nsample = len(data)
n=96 #for zero-padded
window = np.hanning(Nsample/2) #Hann window
data1 = data[(data['condition']=='a')]
data2 = data[(data['condition']=='b')]
#Apply Hann window then do FFT
data1_Hann = data1['val']*window
data1_FFT = fft(data1_Hann, n)
freq1 = fftfreq(n, d=0.026)
data2_Hann = data2['val']*window
data2_FFT = fft(data2_Hann, n)
freq2 = fftfreq(n, d=0.026)
plt.plot(freq1, np.abs(data1_FFT), freq2, np.abs(data2_FFT))
plt.xlabel("Frequency (Hz)")
plt.ylabel("Amplitude")
data
is a DataFrame
containing values of 2 different conditions shuffled together, that's why I separate them and the Hanning window is applied for half the initial sample number. For each condition there are only 12 values, so I do zero padding so as to make the peaks appear clearly and the graph look smoother (I did try with smaller number for zero padding, but the line still remain).
Answer: The graph look like that because of the order of the fft calculation output: it starts with 0 Hz (more details presented here: https://numpy.org/doc/stable/reference/generated/numpy.fft.fftfreq.html)
I fixed this by using numpy.fft.fftshift() function (more details presented here: https://numpy.org/doc/stable/reference/generated/numpy.fft.fftshift.html).
Solution has been suggested by @gavinb and @yanziselman.