예제 #1
0
파일: fft.py 프로젝트: RuthAngus/K2rotation
def FFT(x, y, fs, nufft=False):
    if nufft: return nufft.nufft3(x, y, fs * 2 * np.pi)
    pgram = np.fft.fft(y)
    plt.clf()
    plt.plot(np.real(pgram[100:-100])**2 + np.imag(pgram[:-100])**2)
    plt.savefig("test")
    assert 0
    return pgram
예제 #2
0
def make_pgram(kid, x, y, yerr, ivar, nm, dn, fft=False):

    # compute spg or fft
    fs = np.arange(nm-(10*dn), nm+(10*dn), 1e-2) * 1e-6
    if fft:
        comp_pgram = nufft.nufft3(x, y, fs*2*np.pi)
        pgram = np.imag(comp_pgram)**2 + np.real(comp_pgram)**2
    else:
        starts, stops, centres = real_footprint_sc(x)
        pgram = spg.superpgram(starts, stops, y, ivar, fs*2*np.pi)

    # save spg
    np.savetxt("pgram_%s.txt" % str(int(kid)), np.transpose((fs, pgram)))
    plt.clf()
    plt.plot(fs, pgram)
    plt.savefig("pgram_%s" % str(int(kid)))
    return fs, pgram
예제 #3
0
def dofft(time,flux, over):
    """Take a fourier transform using nufft
    """
    
    dt=(time[3]-time[1])/2.0;
    n=len(time)
    endf=1/(dt*2.0); #Nyquist Frequency
    step=endf/(n*over);
    
    freq=np.arange(0,endf,step)
    freq.size
    
    fft=nu.nufft3(time,flux,freq)
    amp=2*np.sqrt(fft.real**2 + fft.imag**2)
    
      
    return(freq,amp)
예제 #4
0
    plt.subplot(3, 1, 1)
    print ("calculating superpgram for long cadence data")
    x, y, yerr, ivar = load_data(kid, kepler_lc_dir, sc=False)
    starts, stops, centres = real_footprint(x)
    amp2s = spg.superpgram(starts, stops, y, ivar, ws)
    plt.plot((fs-fs0)*1e6, amp2s, "k", alpha=.7)
    if len(truths):
        for truth in truths:
            plt.axvline(truth*1e-6, color="r", linestyle="-.")
    plt.ylabel("$\mathrm{SuperPgram}$")
    plt.xlabel("$\mathrm{Frequency-%s~(uHz)}$" % (fs0*1e6))

    # fft long cadence data
    plt.subplot(3, 1, 2)
    print ("calculating  fft for long cadence data")
    pgram = nufft.nufft3(stops, y, ws)
    plt.plot((fs-fs0)*1e6, pgram, "k", alpha=.7)
    if len(truths):
        for truth in truths:
            plt.axvline(truth*1e-6, color="r", linestyle="-.")
    plt.ylabel("$\mathrm{FFT}$")
    plt.xlabel("$\mathrm{Frequency-%s~(uHz)}$" % (fs0*1e6))

    # LombScargle long cadence data
    plt.subplot(3, 1, 3)
    print ("calculating LombScargle for long cadence data")
    periods = 1./fs
    model = LombScargle().fit(x, y, yerr)
    power = model.periodogram(periods)
    plt.plot((fs-fs0)*1e6, power, "k", alpha=.7)
    if len(truths):
def forward(x, u_initial, psi, f, T, R):
    """Solves advection-diffusion equation forward in time using the FFT.

    The equation is
    $$
    u_t - u_{xx} - \psi u_x - f(x) = 0 \:,
    $$
    with initial condition $u(x,0) = u_initial$. We assume that 
    the support of f(x) is a subset of [-R/2, R/2].

    Args:
        x: grid in physical space
        u_initial: initial field value
        psi: advection coefficient
        f: time independent source
        T: final time
        R: The support of the source is a subset of $[-R / 2, R / 2]$
    
    Returns:
        u_final: final field value

    """
    # add $R / 2$ to the grid, and evaluate Riemann sums on the right endpoint 
    x = np.append(x, R / 2.0)
    u_initial = np.append(u_initial, 0.0)
    f = np.append(f, 0.0)
    N = len(x)
    dx = np.zeros(N)
    dx[0] = x[0] - (- R / 2.0)
    dx[1 : N] = x[1 :  N] - x[0 : N - 1]
    
    # nufft frecuencies
    xi = (np.arange(-N // 2, N // 2) + N % 2)
    zero_freq_index = N // 2
    
    # Fourier transform, NOTICE that nufft3 calculates the normalized
    # sum: line 71 in https://github.com/dfm/python-nufft/blob/master/nufft/nufft.py
    u_hat_initial = N * nufft3(x, u_initial * dx, xi)
    f_hat = N * nufft3(x, f * dx, xi)
    
    # solve ODE's analitically in Fourier space
    a = (-1j * psi - xi) * xi
    u_hat_final = np.zeros(N)
    u_hat_final = np.array(u_hat_final, dtype=complex)
    
    for n in range (0, zero_freq_index):
        u_hat_final[n] = (
            u_hat_initial[n] * np.exp(a[n] * T) - 
            (f_hat[n] / a[n]) * (1.0 - np.exp(a[n] * T)))
    for n in range (zero_freq_index + 1, N):
        u_hat_final[n] = (
            u_hat_initial[n] * np.exp(a[n] * T) - 
            (f_hat[n] / a[n]) * (1.0 - np.exp(a[n] * T)))    
    u_hat_final[zero_freq_index] = (
        u_hat_initial[zero_freq_index] + 
        (T + 0j) * f_hat[zero_freq_index])
    
    # inverse Fourier transform,  NOTICE that nufft3 calculates the normalized
    # sum: line 71 in https://github.com/dfm/python-nufft/blob/master/nufft/nufft.py
    u_final = (
        (1.0 / (2 * np.pi)) * N * 
        nufft3(xi, u_hat_final, x, iflag=-1))
    return np.real(u_final[0 : N - 1])
def forwardGradient(x, u_initial, psi, f, T, R):
    """Solves for u_{x} in the advection-diffusion equation,
    forward in time using the FFT.

    The equation is
    $$
    u_t - u_{xx} - \psi u_x - f(x) = 0 \:,
    $$
    with initial condition $u(x,0) = u_initial$. We assume that 
    the support of f(x) is a subset of [-R/2, R/2].

    Args:
        x: grid in physical space. 
        u_initial: initial field value
        psi: advection coefficient
        f: time independent source
        T: final time
        R: The support of the source is a subset of $[-R,R]$
    
    Returns:
        u_final: final field value

    """
    # add $R / 2$ to the grid, and evaluate Riemann sums on the right endpoint 
    x = np.append(x, R / 2.0)
    u_initial = np.append(u_initial, 0.0)
    f = np.append(f, 0.0)
    N = len(x)
    dx = np.zeros(N)
    dx[0] = x[0] - (- R / 2.0)
    dx[1 : N] = x[1 :  N] - x[0 : N - 1]
    
    # nufft frecuencies
    xi = (np.arange(-N // 2, N // 2) + N % 2)
    zero_freq_index = N // 2
    
    # Fourier transform
    u_hat_initial = N * nufft3(x, u_initial * dx, xi)
    f_hat = N * nufft3(x, f * dx, xi)
    
    # Solve ODE's analitically in Fourier space
    a = (-1j * psi - xi) * xi
    u_hat_final_x = np.zeros(N)
    u_hat_final_x = np.array(u_hat_final_x, dtype=complex)
    
    # $u_x$ in Fourier space is equivalent to multiplication by $-i * \xi$
    u_hat_initial_x = u_hat_initial * (-1j * xi)
    f_hat_x = f_hat * (-1j * xi)
    
    for n in range (0, zero_freq_index):
        u_hat_final_x[n] = (
            u_hat_initial_x[n] * np.exp(a[n] * T) - 
            (f_hat_x[n] / a[n]) * (1.0 - np.exp(a[n] * T)))
    for n in range (zero_freq_index + 1, N):
        u_hat_final_x[n] = (
            u_hat_initial_x[n] * np.exp(a[n] * T) - 
            (f_hat_x[n]/a[n]) * (1.0 - np.exp(a[n] * T)))    
    u_hat_final_x[zero_freq_index] = (
        u_hat_initial_x[zero_freq_index] + 
        (T + 0j) * f_hat_x[zero_freq_index])
    
    # inverse Fourier transform
    u_final_x = (
        (1.0 / (2.0 * np.pi)) * N * 
        nufft3(xi, u_hat_final_x, x, iflag=-1))
    return np.real(u_final_x[0 : N - 1])