Ejemplo n.º 1
0
def test_IFT_FT():
    # Test IFT(FT(x)) = x
    np.random.seed(0)
    t = -50 + 0.01 * np.arange(10000.)
    x = np.random.random(10000)

    f, y = FT_continuous(t, x)
    t, xp = IFT_continuous(f, y)

    assert_allclose(x, xp, atol=1E-7)
Ejemplo n.º 2
0
np.random.seed(5)

t = np.linspace(-40, 40, 2001)[:-1]
h = np.exp(-0.5 * ((t - 20.) / 1.0)**2)
hN = h + np.random.normal(0, 0.5, size=h.shape)

#------------------------------------------------------------
# Compute the convolution via the continuous Fourier transform
# This is more exact than using the discrete transform, because
# we have an analytic expression for the FT of the wavelet.
Q = 0.3
f0 = 2**np.linspace(-3, -1, 100)

f, H = FT_continuous(t, hN)
W = np.conj(wavelet_FT(f, 0, f0[:, None], Q))
t, HW = IFT_continuous(f, H * W)

#------------------------------------------------------------
# Plot the results
fig = plt.figure(figsize=(5, 5))
fig.subplots_adjust(hspace=0.05, left=0.12, right=0.95, bottom=0.08, top=0.95)

# First panel: the signal
ax = fig.add_subplot(311)
ax.plot(t, hN, '-k', lw=1)

ax.text(0.02,
        0.95, ("Input Signal:\n"
               "Localized spike plus noise"),
        ha='left',
        va='top',
Ejemplo n.º 3
0
def check_IFT_continuous(a, t0, f0, method):
    f = np.linspace(-9, 10, 10000)
    H = sinegauss_FT(f, t0, f0, a)
    t, h = IFT_continuous(f, H, method=method)
    assert_allclose(h, sinegauss(t, t0, f0, a), atol=1E-12)
Ejemplo n.º 4
0
def check_IFT_continuous(a, t0, f0, method, f):
    H = sinegauss_FT(f, t0, f0, a)
    t, h = IFT_continuous(f, H, method=method)
    assert_allclose(h, sinegauss(t, t0, f0, a), atol=1E-12)
Ejemplo n.º 5
0
np.random.seed(5)
t = np.linspace(0, 100, 2001)[:-1]
h = np.exp(-0.5 * ((t - 20.) / 1.0) ** 2)
hN = h + np.random.normal(0, 0.5, size=h.shape)

#----------------------------------------------------------------------
# compute the PSD
N = len(t)
Df = 1. / N / (t[1] - t[0])
f = fftpack.ifftshift(Df * (np.arange(N) - N / 2))

h_wiener, PSD, P_S, P_N, Phi = wiener_filter(t, hN, return_PSDs=True)

#------------------------------------------------------------
# inverse fourier transform Phi to find the effective kernel
t_plot, kernel = IFT_continuous(f, Phi)

#------------------------------------------------------------
# perform kernel smoothing on the data.  This is faster in frequency
# space (ie using the standard Wiener filter above) but we will do
# it in the slow & simple way here to demonstrate the equivalence
# explicitly
kernel_func = interpolate.interp1d(t_plot, kernel.real)

t_eval = np.linspace(0, 90, 1000)
t_KDE = t_eval[:, np.newaxis] - t
t_KDE[t_KDE < t_plot[0]] = t_plot[0]
t_KDE[t_KDE > t_plot[-1]] = t_plot[-1]
F = kernel_func(t_KDE)

h_smooth = np.dot(F, hN) / np.sum(F, 1)
Ejemplo n.º 6
0
def compute_Wavelet():

    from astroML.fourier import FT_continuous, IFT_continuous

#----------------------------------------------------------------------
# This function adjusts matplotlib settings for a uniform feel in the textbook.
# Note that with usetex=True, fonts are rendered with LaTeX.  This may
# result in an error if LaTeX is not installed on your system.  In that case,
# you can set usetex to False.
    from astroML.plotting import setup_text_plots
    setup_text_plots(fontsize=8, usetex=False)


    def wavelet(t, t0, f0, Q):
        return (np.exp(-(f0 / Q * (t - t0)) ** 2)
            * np.exp(2j * np.pi * f0 * (t - t0)))


    def wavelet_FT(f, t0, f0, Q):
    # this is its fourier transform using
    # H(f) = integral[ h(t) exp(-2pi i f t) dt]
        return (np.sqrt(np.pi) * Q / f0
            * np.exp(-2j * np.pi * f * t0)
            * np.exp(-(np.pi * (f - f0) * Q / f0) ** 2))


    def check_funcs(t0=1, f0=2, Q=3):
        t = np.linspace(-5, 5, 10000)
        h = wavelet(t, t0, f0, Q)

        f, H = FT_continuous(t, h)
        assert np.allclose(H, wavelet_FT(f, t0, f0, Q))

#------------------------------------------------------------
# Create the simulated dataset
    np.random.seed(5)

    t = np.linspace(-40, 40, 2001)[:-1]
    h = np.exp(-0.5 * ((t - 20.) / 1.0) ** 2)
    hN = h + np.random.normal(0, 0.5, size=h.shape)

#------------------------------------------------------------
# Compute the convolution via the continuous Fourier transform
# This is more exact than using the discrete transform, because
# we have an analytic expression for the FT of the wavelet.
    Q = 0.3
    f0 = 2 ** np.linspace(-3, -1, 100)

    f, H = FT_continuous(t, hN)
    W = np.conj(wavelet_FT(f, 0, f0[:, None], Q))
    t, HW = IFT_continuous(f, H * W)

#------------------------------------------------------------
# Plot the results
    fig = plt.figure(figsize=(5, 5))
    fig.subplots_adjust(hspace=0.05, left=0.12, right=0.95, bottom=0.08, top=0.95)

# First panel: the signal
    ax = fig.add_subplot(311)
    ax.plot(t, hN, '-k', lw=1)

    ax.text(0.02, 0.95, ("Input Signal:\n"
                     "Localized spike plus noise"),
        ha='left', va='top', transform=ax.transAxes)

    ax.set_xlim(-40, 40)
    ax.set_ylim(-1.2, 2.2)
    ax.xaxis.set_major_formatter(plt.NullFormatter())
    ax.set_ylabel('$h(t)$')

# Second panel: the wavelet
    ax = fig.add_subplot(312)
    W = wavelet(t, 0, 0.125, Q)
    ax.plot(t, W.real, '-k', label='real part', lw=1)
    ax.plot(t, W.imag, '--k', label='imag part', lw=1)

    ax.legend(loc=1)
    ax.text(0.02, 0.95, ("Example Wavelet\n"
                     "$t_0 = 0$, $f_0=1/8$, $Q=0.3$"),
        ha='left', va='top', transform=ax.transAxes)
    ax.text(0.98, 0.05,
        (r"$w(t; t_0, f_0, Q) = e^{-[f_0 (t - t_0) / Q]^2}"
         "e^{2 \pi i f_0 (t - t_0)}$"),
        ha='right', va='bottom', transform=ax.transAxes)

    ax.set_xlim(-40, 40)
    ax.set_ylim(-1.4, 1.4)
    ax.set_ylabel('$w(t; t_0, f_0, Q)$')
    ax.xaxis.set_major_formatter(plt.NullFormatter())

# Third panel: the spectrogram
    ax = fig.add_subplot(313)
    ax.imshow(abs(HW) ** 2, origin='lower', aspect='auto', cmap=plt.cm.binary,
          extent=[t[0], t[-1], np.log2(f0)[0], np.log2(f0)[-1]])
    ax.set_xlim(-40, 40)

    ax.text(0.02, 0.95, ("Wavelet PSD"), color='w',
        ha='left', va='top', transform=ax.transAxes)

    ax.set_ylim(np.log2(f0)[0], np.log2(f0)[-1])
    ax.set_xlabel('$t$')
    ax.set_ylabel('$f_0$')

    ax.yaxis.set_major_locator(plt.MultipleLocator(1))
    ax.yaxis.set_major_formatter(plt.FuncFormatter(lambda x, *args: ("1/%i"
                                                                 % (2 ** -x))))
    plt.show()
Ejemplo n.º 7
0
def compute_Kernel():


    from scipy import optimize, fftpack, interpolate
    from astroML.fourier import IFT_continuous
    from astroML.filters import wiener_filter

#----------------------------------------------------------------------
# This function adjusts matplotlib settings for a uniform feel in the textbook.
# Note that with usetex=True, fonts are rendered with LaTeX.  This may
# result in an error if LaTeX is not installed on your system.  In that case,
# you can set usetex to False.
    from astroML.plotting import setup_text_plots
    setup_text_plots(fontsize=8, usetex=False)

#----------------------------------------------------------------------
# sample the same data as the previous Wiener filter figure
    np.random.seed(5)
    t = np.linspace(0, 100, 2001)[:-1]
    h = np.exp(-0.5 * ((t - 20.) / 1.0) ** 2)
    hN = h + np.random.normal(0, 0.5, size=h.shape)

#----------------------------------------------------------------------
# compute the PSD
    N = len(t)
    Df = 1. / N / (t[1] - t[0])
    f = fftpack.ifftshift(Df * (np.arange(N) - N / 2))

    h_wiener, PSD, P_S, P_N, Phi = wiener_filter(t, hN, return_PSDs=True)

#------------------------------------------------------------
# inverse fourier transform Phi to find the effective kernel
    t_plot, kernel = IFT_continuous(f, Phi)

#------------------------------------------------------------
# perform kernel smoothing on the data.  This is faster in frequency
# space (ie using the standard Wiener filter above) but we will do
# it in the slow & simple way here to demonstrate the equivalence
# explicitly
    kernel_func = interpolate.interp1d(t_plot, kernel.real)

    t_eval = np.linspace(0, 90, 1000)
    t_KDE = t_eval[:, np.newaxis] - t
    t_KDE[t_KDE < t_plot[0]] = t_plot[0]
    t_KDE[t_KDE > t_plot[-1]] = t_plot[-1]
    F = kernel_func(t_KDE)

    h_smooth = np.dot(F, hN) / np.sum(F, 1)

#------------------------------------------------------------
# Plot the results
    fig = plt.figure(figsize=(5, 2.2))
    fig.subplots_adjust(left=0.1, right=0.95, wspace=0.25,
                    bottom=0.15, top=0.9)

# First plot: the equivalent Kernel to the WF
    ax = fig.add_subplot(121)
    ax.plot(t_plot, kernel.real, '-k')
    ax.text(0.95, 0.95, "Effective Wiener\nFilter Kernel",
        ha='right', va='top', transform=ax.transAxes)

    ax.set_xlim(-10, 10)
    ax.set_ylim(-0.05, 0.45)
    ax.set_xlabel(r'$\lambda$')
    ax.set_ylabel(r'$K(\lambda)$')

# Second axes: Kernel smoothed results
    ax = fig.add_subplot(122)
    ax.plot(t_eval, h_smooth, '-k', lw=1)
    ax.plot(t_eval, 0 * t_eval, '-k', lw=1)
    ax.text(0.95, 0.95, "Kernel smoothing\nresult",
        ha='right', va='top', transform=ax.transAxes)

    ax.set_xlim(0, 90)
    ax.set_ylim(-0.5, 1.5)

    ax.set_xlabel('$\lambda$')
    ax.set_ylabel('flux')

    plt.show()