import numpy as np
from scipy.fftpack import fft

def plot(x,y, block = True):    
    import matplotlib.pyplot as plt            
    plt.figure()
    plt.plot(x, y)
    plt.grid()
    #if show:
    plt.show(block=block)

    
def get_f(N,T,F):
    x = np.linspace(0.0, N*T, N)
    y=0.0
    for (a,f) in F:
        y = y + a*np.sin(f * 2.0*np.pi*x)    
    return (x,y)

def get_fft(x,y,T):        
    N = len(x)    
    yf = fft(y)
    xf = np.linspace(0.0, 1.0/(2.0*T), N / 2)
    yfp = 2.0/N * np.abs(yf[0:N/2])    
    return (xf,yf,yfp)
 
def get_fft2(y):        
    T = 1.0 / 800.0 
    N = len(y)       
    yf = fft(y)   
    return yf
    xf = np.linspace(0.0, 1.0/(2.0*T), N/2)
    yfp = 2.0/N * np.abs(yf[0:N/2])    
    return (xf,yf,yfp)


if __name__ == '__main__':
    N = 600    # Number of sample points
    T = 1.0 / 800.0    # sample spacing
    F = [(1.0,50.0), (0.5,80.0)]    #Sinuoisids
    (x,y) = get_f(N,T,F)
    (xf,yf,yfp) = get_fft(x,y,T)

    plot(x,y, False)    
    plot(xf,yfp)     