def fft ( t , y , pow2 = False , window = None , rescale = False ) : # make sure they're numpy arrays, and make copies to avoid the referencing error y = _n . array ( y ) t = _n . array ( t ) # if we're doing the power of 2, do it if pow2 : keep = 2 ** int ( _n . log2 ( len ( y ) ) ) # now resize the data y . resize ( keep ) t . resize ( keep ) # Window the data if not window in [ None , False , 0 ] : try : # Get the windowing array w = eval ( "_n." + window , dict ( _n = _n ) ) ( len ( y ) ) # Store the original variance v0 = _n . average ( abs ( y ) ** 2 ) # window the time domain data  y = y * w # Rescale by the variance ratio if rescale : y = y * _n . sqrt ( v0 / _n . average ( abs ( y ) ** 2 ) ) except : print ( "ERROR: Bad window!" ) return # do the actual fft, and normalize Y = _n . fft . fftshift ( _n . fft . fft ( y ) / len ( t ) ) f = _n . fft . fftshift ( _n . fft . fftfreq ( len ( t ) , t [ 1 ] - t [ 0 ] ) ) return f , Y
python designing a time series filter after fourier analysis
FFT of y assuming complex or real - valued inputs . This goes through the numpy fourier transform process assembling and returning ( frequencies complex fft ) given time and signal data y .
def fft(t, y, pow2=False, window=None, rescale=False):
    """
    FFT of y, assuming complex or real-valued inputs. This goes through the 
    numpy fourier transform process, assembling and returning (frequencies, 
    complex fft) given time and signal data y.

    Parameters
    ----------
    t,y
        Time (t) and signal (y) arrays with which to perform the fft. Note the t
        array is assumed to be evenly spaced.
        
    pow2 = False
        Set this to true if you only want to keep the first 2^n data
        points (speeds up the FFT substantially)

    window = None
        Can be set to any of the windowing functions in numpy that require only
        the number of points as the argument, e.g. window='hanning'. 
        
    rescale = False
        If True, the FFT will be rescaled by the square root of the ratio of 
        variances before and after windowing, such that the sum of component 
        amplitudes squared is equal to the actual variance.
    """
    # make sure they're numpy arrays, and make copies to avoid the referencing error
    y = _n.array(y)
    t = _n.array(t)

    # if we're doing the power of 2, do it
    if pow2:
        keep  = 2**int(_n.log2(len(y)))

        # now resize the data
        y.resize(keep)
        t.resize(keep)

    # Window the data
    if not window in [None, False, 0]:
        try:
            # Get the windowing array
            w = eval("_n."+window, dict(_n=_n))(len(y))
            
            # Store the original variance
            v0 = _n.average(abs(y)**2)
            
            # window the time domain data 
            y = y * w
            
            # Rescale by the variance ratio
            if rescale: y = y * _n.sqrt(v0 / _n.average(abs(y)**2))
            
        except:
            print("ERROR: Bad window!")
            return

    # do the actual fft, and normalize
    Y = _n.fft.fftshift( _n.fft.fft(y) / len(t) )
    f = _n.fft.fftshift( _n.fft.fftfreq(len(t), t[1]-t[0]) )
    
    return f, Y