How to execute an FFT analysis

From KratosWiki
Jump to: navigation, search

When a FFT analysis of a signal is needed, it could be implemented in python through the following function:

       from scipy import fftpack
       import math
       
       def ExtractFFt(time,data):
           
           N = len(time)
           P1 = (time[N-1]-time[0])
           n1 = 1.0/P1;
       
           dft = fftpack.fft(data)
           print dft
           x = []
           y = []
           
           for i in range(0,N/2):
               y.append(math.sqrt(real(dft[i])**2 + imag(dft[i])**2)*2.0/N )
               x.append(n1*i)
       
           return(x,y);

Initially the necessary libraries are imported: here scipy libraries and math module are used. The first ones, and all the documentation, can be found at http://www.scipy.org. The function above requires two input vectors defining the signal data and the time respectively (both obviously of the same lenght ). It calculates the maximum period considered for the analysis (and so the minimum frequency), then it executes FFT through "fftpack" contained in scipy and calculates the amplitude of the first N/2 sinusoides. Finally, it returns the values of the frequencies and amplitudes in two vectors with the same length (x and y).

In "..." there is a simple example that provides FFT analises of two different functions generated as sums of sinusoids with different frequencies. For more information on FFT and the scipy package used, look at http://www.scipy.org.

Personal tools
Categories