Esempio n. 1
0
def ftwindow(x, xmin=None, xmax=None, dx=1, dx2=None,
             window='hanning', _larch=None, **kws):
    """
    create a Fourier transform window array.

    Parameters:
    -------------
      x:        1-d array array to build window on.
      xmin:     starting x for FT Window
      xmax:     ending x for FT Window
      dx:       tapering parameter for FT Window
      dx2:      second tapering parameter for FT Window (=dx)
      window:   name of window type

    Returns:
    ----------
    1-d window array.

    Notes:
    -------
    Valid Window names:
        hanning              cosine-squared taper
        parzen               linear taper
        welch                quadratic taper
	gaussian             Gaussian (normal) function window
	sine                 sine function window
	kaiser               Kaiser-Bessel function-derived window

    """
    if window is None:
        window = VALID_WINDOWS[0]
    nam = window.strip().lower()[:3]
    if nam not in VALID_WINDOWS:
        raise RuntimeError("invalid window name %s" % window)

    dx1 = dx
    if dx2 is None:  dx2 = dx1
    if xmin is None: xmin = min(x)
    if xmax is None: xmax = max(x)

    xstep = (x[-1] - x[0]) / (len(x)-1)
    xeps  = 1.e-4 * xstep
    x1 = max(min(x), xmin - dx1 / 2.0)
    x2 = xmin + dx1 / 2.0  + xeps
    x3 = xmax - dx2 / 2.0  - xeps
    x4 = min(max(x), xmax + dx2 / 2.0)

    if nam == 'fha':
        if dx1 < 0: dx1 = 0
        if dx2 > 1: dx2 = 1
        x2 = x1 + xeps + dx1*(xmax-xmin)/2.0
        x3 = x4 - xeps - dx2*(xmax-xmin)/2.0
    elif nam == 'gau':
        dx1 = max(dx1, xeps)

    def asint(val): return int((val+xeps)/xstep)
    i1, i2, i3, i4 = asint(x1), asint(x2), asint(x3), asint(x4)
    i1, i2 = max(0, i1), max(0, i2)
    i3, i4 = min(len(x)-1, i3), min(len(x)-1, i4)
    if i2 == i1: i1 = max(0, i2-1)
    if i4 == i3: i3 = max(i2, i4-1)
    x1, x2, x3, x4 = x[i1], x[i2], x[i3], x[i4]
    if x1 == x2: x2 = x2+xeps
    if x3 == x4: x4 = x4+xeps
    # initial window
    fwin =  zeros(len(x))
    if i3 > i2:
        fwin[i2:i3] = ones(i3-i2)

    # now finish making window
    if nam in ('han', 'fha'):
        fwin[i1:i2+1] = sin((pi/2)*(x[i1:i2+1]-x1) / (x2-x1))**2
        fwin[i3:i4+1] = cos((pi/2)*(x[i3:i4+1]-x3) / (x4-x3))**2
    elif nam == 'par':
        fwin[i1:i2+1] =     (x[i1:i2+1]-x1) / (x2-x1)
        fwin[i3:i4+1] = 1 - (x[i3:i4+1]-x3) / (x4-x3)
    elif nam == 'wel':
        fwin[i1:i2+1] = 1 - ((x[i1:i2+1]-x2) / (x2-x1))**2
        fwin[i3:i4+1] = 1 - ((x[i3:i4+1]-x3) / (x4-x3))**2
    elif nam  in ('kai', 'bes'):
        cen  = (x4+x1)/2
        wid  = (x4-x1)/2
        arg  = 1 - (x-cen)**2 / (wid**2)
        arg[where(arg<0)] = 0
        if nam == 'bes': # 'bes' : ifeffit 1.0 implementation of kaiser-bessel
            fwin = bessel_i0(dx* sqrt(arg)) / bessel_i0(dx)
            fwin[where(x<=x1)] = 0
            fwin[where(x>=x4)] = 0
        else: # better version
            scale = max(1.e-10, bessel_i0(dx)-1)
            fwin = (bessel_i0(dx * sqrt(arg)) - 1) / scale
    elif nam == 'sin':
        fwin[i1:i4+1] = sin(pi*(x4-x[i1:i4+1]) / (x4-x1))
    elif nam == 'gau':
        cen  = (x4+x1)/2
        fwin =  exp(-(((x - cen)**2)/(2*dx1*dx1)))
    return fwin
Esempio n. 2
0
def ftwindow(x,
             xmin=None,
             xmax=None,
             dx=1,
             dx2=None,
             window='hanning',
             _larch=None,
             **kws):
    """
    create a Fourier transform window array.

    Parameters:
    -------------
      x:        1-d array array to build window on.
      xmin:     starting x for FT Window
      xmax:     ending x for FT Window
      dx:       tapering parameter for FT Window
      dx2:      second tapering parameter for FT Window (=dx)
      window:   name of window type

    Returns:
    ----------
    1-d window array.

    Notes:
    -------
    Valid Window names:
        hanning              cosine-squared taper
        parzen               linear taper
        welch                quadratic taper
        gaussian             Gaussian (normal) function window
        sine                 sine function window
        kaiser               Kaiser-Bessel function-derived window

    """
    if window is None:
        window = VALID_WINDOWS[0]
    nam = window.strip().lower()[:3]
    if nam not in VALID_WINDOWS:
        raise RuntimeError("invalid window name %s" % window)

    dx1 = dx
    if dx2 is None: dx2 = dx1
    if xmin is None: xmin = min(x)
    if xmax is None: xmax = max(x)

    xstep = (x[-1] - x[0]) / (len(x) - 1)
    xeps = 1.e-4 * xstep
    x1 = max(min(x), xmin - dx1 / 2.0)
    x2 = xmin + dx1 / 2.0 + xeps
    x3 = xmax - dx2 / 2.0 - xeps
    x4 = min(max(x), xmax + dx2 / 2.0)

    if nam == 'fha':
        if dx1 < 0: dx1 = 0
        if dx2 > 1: dx2 = 1
        x2 = x1 + xeps + dx1 * (xmax - xmin) / 2.0
        x3 = x4 - xeps - dx2 * (xmax - xmin) / 2.0
    elif nam == 'gau':
        dx1 = max(dx1, xeps)

    def asint(val):
        return int((val + xeps) / xstep)

    i1, i2, i3, i4 = asint(x1), asint(x2), asint(x3), asint(x4)
    i1, i2 = max(0, i1), max(0, i2)
    i3, i4 = min(len(x) - 1, i3), min(len(x) - 1, i4)
    if i2 == i1: i1 = max(0, i2 - 1)
    if i4 == i3: i3 = max(i2, i4 - 1)
    x1, x2, x3, x4 = x[i1], x[i2], x[i3], x[i4]
    if x1 == x2: x2 = x2 + xeps
    if x3 == x4: x4 = x4 + xeps
    # initial window
    fwin = zeros(len(x))
    if i3 > i2:
        fwin[i2:i3] = ones(i3 - i2)

    # now finish making window
    if nam in ('han', 'fha'):
        fwin[i1:i2 + 1] = sin((pi / 2) * (x[i1:i2 + 1] - x1) / (x2 - x1))**2
        fwin[i3:i4 + 1] = cos((pi / 2) * (x[i3:i4 + 1] - x3) / (x4 - x3))**2
    elif nam == 'par':
        fwin[i1:i2 + 1] = (x[i1:i2 + 1] - x1) / (x2 - x1)
        fwin[i3:i4 + 1] = 1 - (x[i3:i4 + 1] - x3) / (x4 - x3)
    elif nam == 'wel':
        fwin[i1:i2 + 1] = 1 - ((x[i1:i2 + 1] - x2) / (x2 - x1))**2
        fwin[i3:i4 + 1] = 1 - ((x[i3:i4 + 1] - x3) / (x4 - x3))**2
    elif nam in ('kai', 'bes'):
        cen = (x4 + x1) / 2
        wid = (x4 - x1) / 2
        arg = 1 - (x - cen)**2 / (wid**2)
        arg[where(arg < 0)] = 0
        if nam == 'bes':  # 'bes' : ifeffit 1.0 implementation of kaiser-bessel
            fwin = bessel_i0(dx * sqrt(arg)) / bessel_i0(dx)
            fwin[where(x <= x1)] = 0
            fwin[where(x >= x4)] = 0
        else:  # better version
            scale = max(1.e-10, bessel_i0(dx) - 1)
            fwin = (bessel_i0(dx * sqrt(arg)) - 1) / scale
    elif nam == 'sin':
        fwin[i1:i4 + 1] = sin(pi * (x4 - x[i1:i4 + 1]) / (x4 - x1))
    elif nam == 'gau':
        cen = (x4 + x1) / 2
        fwin = exp(-(((x - cen)**2) / (2 * dx1 * dx1)))
    return fwin
Esempio n. 3
0
def ftwindow(x: ndarray,
             x_range: list = [-inf, inf],
             dx1: float = 1,
             dx2: float = None,
             win: str = 'hanning') -> ndarray:
    """Returns a FT window.

    Parameters
    ----------
    x
        Array for the FT window.
    x_range
        Range for the FT window. The default is [-:data:`~numpy.inf`, :data:`~numpy.inf`].
    dx1
        First tapering parameter for FT window. The defaut is 1.
    dx2
        Second tapering parameter for FT window. If None it will use the same value as dx1.
    win
        Name of the window type.  The default is 'hanning'. See Notes for valid names.

    Returns
    -------
    :
        1-d array with the FT window.

    Raises
    ------
    ValueError
        If ``window`` name is not recognized.

    Notes
    -----
    Valid window names:
    
    - 'hanning'  : cosine-squared function window.
    - 'parzen'   : linear function window.
    - 'welch'    : quadratic function window.
    - 'gaussian' : Gaussian (normal) function window.
    - 'sine'     : sine function window.
    - 'kaiser'   : Kaiser-Bessel function-derived window.

    Example
    --------
    .. plot::
        :context: reset

        >>> from numpy import arange
        >>> import matplotlib.pyplot as plt
        >>> from araucaria.xas import ftwindow
        >>> from araucaria.plot import fig_xas_template
        >>> k       = arange(0, 10.1, 0.05)
        >>> k_range = [2,8]
        >>> windows = ['hanning', 'parzen', 'welch', 
        ...            'gaussian', 'sine', 'kaiser']
        >>> dk      = 1.0
        >>> fig_kws = {'sharex' : True}
        >>> fig, axes = fig_xas_template(panels='ee/ee/ee', **fig_kws)
        >>> for i, ax in enumerate(axes.flatten()):
        ...     win  = ftwindow(k, k_range, dk, win= windows[i])
        ...     line = ax.plot(k, win, label=windows[i])
        ...     for val in k_range:
        ...         line = ax.axvline(val - dk/2, color='gray', ls=':')
        ...         line = ax.axvline(val + dk/2, color='gray', ls=':')
        ...     leg  = ax.legend()
        ...     text = ax.set_ylabel('')
        ...     text = ax.set_xlabel('')
        >>> fig.tight_layout()
        >>> plt.show(block=False)
    """
    # available windows
    windows = ['han', 'fha', 'gau', 'kai', 'par', 'wel', 'sin', 'bes']
    name = win.strip().lower()[:3]
    if name not in windows:
        raise ValueError("window name %s not recognized." % win)

    if dx2 is None:
        dx2 = dx1

    # asigning values inside the x array
    xrange = check_xrange(x_range, x)
    xstep = (x[-1] - x[0]) / (len(x) - 1)
    xeps = 1.e-4 * xstep

    x1 = max(min(x), xrange[0] - dx1 / 2.0)
    x2 = xrange[0] + dx1 / 2.0 + xeps
    x3 = xrange[1] - dx2 / 2.0 - xeps
    x4 = min(max(x), xrange[1] + dx2 / 2.0)

    if name == 'fha':
        if dx1 < 0: dx1 = 0
        if dx2 > 1: dx2 = 1
        x2 = x1 + xeps + dx1 * (xrange[1] - xrange[0]) / 2.0
        x3 = x4 - xeps - dx2 * (xrange[1] - xrange[0]) / 2.0

    elif name == 'gau':
        dx1 = max(dx1, xeps)

    # return values as integer for indices
    def asint(val):
        return int((val + xeps) / xstep)

    i1 = max(0, asint(x1))
    i2 = max(0, asint(x2))
    i3 = min(len(x) - 1, asint(x3))
    i4 = min(len(x) - 1, asint(x4))
    if i2 == i1:
        i1 = max(0, i2 - 1)
    if i4 == i3:
        i3 = max(i2, i4 - 1)

    x1, x2, x3, x4 = x[i1], x[i2], x[i3], x[i4]
    if x1 == x2: x2 = x2 + xeps
    if x3 == x4: x4 = x4 + xeps

    # initial window
    fwin = zeros(len(x))
    if i3 > i2:
        fwin[i2:i3] = 1.0

    # final window
    if name in ('han', 'fha'):
        fwin[i1:i2 + 1] = sin((pi / 2) * (x[i1:i2 + 1] - x1) / (x2 - x1))**2
        fwin[i3:i4 + 1] = cos((pi / 2) * (x[i3:i4 + 1] - x3) / (x4 - x3))**2

    elif name == 'par':
        fwin[i1:i2 + 1] = (x[i1:i2 + 1] - x1) / (x2 - x1)
        fwin[i3:i4 + 1] = 1 - (x[i3:i4 + 1] - x3) / (x4 - x3)

    elif name == 'wel':
        fwin[i1:i2 + 1] = 1 - ((x[i1:i2 + 1] - x2) / (x2 - x1))**2
        fwin[i3:i4 + 1] = 1 - ((x[i3:i4 + 1] - x3) / (x4 - x3))**2

    elif name in ('kai', 'bes'):
        cen = (x4 + x1) / 2
        wid = (x4 - x1) / 2
        arg = 1 - (x - cen)**2 / (wid**2)

        arg[where(arg < 0)] = 0
        if name == 'bes':  # 'bes' : ifeffit 1.0 implementation of kaiser-bessel
            fwin = bessel_i0(dx1 * sqrt(arg)) / bessel_i0(dx1)
            fwin[where(x <= x1)] = 0
            fwin[where(x >= x4)] = 0
        else:  # better version
            scale = max(1.e-10, bessel_i0(dx1) - 1)
            fwin = (bessel_i0(dx1 * sqrt(arg)) - 1) / scale
    elif name == 'sin':
        fwin[i1:i4 + 1] = sin(pi * (x4 - x[i1:i4 + 1]) / (x4 - x1))

    elif name == 'gau':
        cen = (x4 + x1) / 2
        fwin = exp(-(((x - cen)**2) / (2 * dx1**2)))

    return fwin
Esempio n. 4
0
def vis_data_path(realization):
    path = os.path.join(vis_data_dir,
                        'realization_{0}.uvh5'.format(realization))
    return path


# Load beam data
omegas_data_path = zdir + '/hera_validation/test-0.1/HERA_dipole_Omegas.h5'

with h5py.File(omegas_data_path, 'r') as h5f:
    Omega = h5f['Omega'].value
    Omegapp = h5f['Omegapp'].value

# Add a new window function to hera_pspec's aipy instance
kaiser6 = lambda x, L: bessel_i0(np.pi * 6 * np.sqrt(1 - (2 * x / (
    L - 1) - 1)**2)) / bessel_i0(np.pi * 6)
hps.pspecdata.aipy.dsp.WINDOW_FUNC['kaiser6'] = kaiser6


def astropyPlanck15_for_hera_pspec():
    H0 = 67.74
    h = H0 / 100.

    Om_b = 0.02230 / h**2.
    Om_c = 0.1188 / h**2.
    Om_L = 0.6911
    Om_k = 1. - (Om_b + Om_c + Om_L)

    hps_cosmo = hps.conversions.Cosmo_Conversions(
        Om_L=Om_L,
        Om_b=Om_b,