Power Spectrum by numpy.fft.fft

The drawing I draw with the code below is just the peak around ZERO, no matter how I modify the data. My data is just one column in which all the time points of a signal are recorded. Is the time_step value a value that I have to determine according to the interval of two neighboring points in my data?

 data=np.loadtxt("timesequence",delimiter=",",usecols=(0,),unpack=True) ps = np.abs(np.fft.fft(data))**2 time_step = 1 freqs = np.fft.fftfreq(data.size, time_step) idx = np.argsort(freqs) pl.plot(freqs[idx], ps[idx]) pl.show() 
+7
python numpy fft signal-processing
source share
3 answers

As others have hinted, your signals should have a large non-zero component. The peak at 0 (DC) indicates the average value of your signal. This comes from the Fourier transform itself. This cosine function cos (0) * ps (0) indicates a measure of the average value of the signal. Other components of the Fourier transform are cosine waves of variable amplitude, which show the frequency content at these values.

Note that stationary signals will not have a large DC component, as they are already zero mean signals. If you do not need a large DC component, you must calculate the average value of your signal and subtract the values ​​from it. Regardless of whether your data is 0, ..., 999 or 1, ..., 1000 or even 1000, ..., 2000, you will get a peak with a frequency of 0 Hz. The only difference will be the magnitude of the peak, since it measures the average value.

 data1 = arange(1000) data2 = arange(1000)+1000 dataTransformed3 = data - mean(data) data4 = numpy.zeros(1000) data4[::10] = 1 #simulate a photon counter where a 1 indicates a photon came in at time indexed by array. # we could assume that the sample rate was 10 Hz for example ps1 = np.abs(np.fft.fft(data))**2 ps2 = np.abs(np.fft.fft(data))**2 ps3 = np.abs(np.fft.fft(dataTransformed))**2 figure() plot(ps1) #shows the peak at 0 Hz figure() plot(ps2) #shows the peak at 0 Hz figure() plot(ps3) #shows the peak at 1 Hz this is because we removed the mean value but since #the function is a step function the next largest component is the 1 Hz cosine wave. #notice the order of magnitude difference in the two plots. 
+3
source share

Here is a bare-bones example that shows input and output with a peak, as you expected:

 import numpy as np from scipy.fftpack import rfft, irfft, fftfreq time = np.linspace(0,10,2000) signal = np.cos(5*np.pi*time) W = fftfreq(signal.size, d=time[1]-time[0]) f_signal = rfft(signal) import pylab as plt plt.subplot(121) plt.plot(time,signal) plt.subplot(122) plt.plot(W,f_signal) plt.xlim(0,10) plt.show() 

enter image description here

I use rfft , because most likely your input comes from a physical data source and as such is real.

+2
source share

If you make your data positive:

 ps = np.abs(np.fft.fft(data))**2 time_step = 1 

then most likely you will create a large β€œDC” component or 0 Hz. Therefore, if your actual data has a small amplitude compared to this component, it will disappear from the graph using the auto-scale function.

0
source share

All Articles