Ejemplo n.º 1
0
def rv_sample(obs = None, tspan = 180, npernight = 3, drun = 10, \
                  nrun = 3, nrand = 10, dnight = 8./24.):
    if obs != None:
        # Read in RV data
        if obs == 'corot7':
            rv = atpy.Table(corotdefs.ROOTDIR + 'LRa01/cands/corot7_rv.ipac')
            time = rv.JDB
        if obs == 'hd189':
            rv = atpy.Table('/Users/suz/Data/HD189_rv.ipac')
            time = rv.hjd
    else:
        # One point per night
        days = scipy.arange(tspan)
        dt_night = dnight / float(npernight + 1)
        # Multiple points per night, with small deviations from regularity
        obs = scipy.zeros((tspan, npernight))
        for i in scipy.arange(npernight):
            obs[:,i] = days[:] + dt_night * float(i) + \
                pylab.normal(0, dt_night/2., tspan)
# Select points in "intensive" runs
        if drun == tspan:
            take = scipy.ones((tspan, npernight), 'int')
        else:
            take = scipy.zeros((tspan, npernight), 'int')
            for i in scipy.arange(nrun):
                ok = 0
                while ok == 0:
                    tstart = scipy.fix(scipy.rand(1) * float(tspan))
                    tstart = tstart[0]
                    tend = tstart + drun
                    if tend > tspan: continue
                    if take[tstart:tend, :].any(): continue
                    take[tstart:tend, :] = 1
                    ok = 1


# Select additional individual points
        ntot = tspan * npernight
        obs = scipy.reshape(obs, ntot)
        take = scipy.reshape(take, ntot)
        index = scipy.argsort(obs)
        obs = obs[index]
        take = take[index]
        for i in scipy.arange(nrand):
            ok = 0
            while ok == 0:
                t = scipy.fix(scipy.rand(1) * float(ntot))
                t = t[0]
                if take[t] == 1: continue
                take[t] = 1
                ok = 1
        time = obs[(take == 1)]
    time -= time[0]
    return time
Ejemplo n.º 2
0
def rv_sample(obs = None, tspan = 180, npernight = 3, drun = 10, \
                  nrun = 3, nrand = 10, dnight = 8./24.):
    if obs != None:
# Read in RV data 
        if obs == 'corot7':
            rv = atpy.Table(corotdefs.ROOTDIR + 'LRa01/cands/corot7_rv.ipac')
            time = rv.JDB
        if obs == 'hd189':
            rv = atpy.Table('/Users/suz/Data/HD189_rv.ipac')
            time = rv.hjd
    else:
# One point per night
        days = scipy.arange(tspan)
        dt_night = dnight / float(npernight+1)
# Multiple points per night, with small deviations from regularity
        obs = scipy.zeros((tspan, npernight)) 
        for i in scipy.arange(npernight):
            obs[:,i] = days[:] + dt_night * float(i) + \
                pylab.normal(0, dt_night/2., tspan)
# Select points in "intensive" runs
        if drun == tspan:
            take = scipy.ones((tspan, npernight), 'int')
        else:
            take = scipy.zeros((tspan, npernight), 'int')
            for i in scipy.arange(nrun):
                ok = 0
                while ok == 0:
                    tstart = scipy.fix(scipy.rand(1) * float(tspan))
                    tstart = tstart[0]
                    tend = tstart + drun
                    if tend > tspan: continue
                    if take[tstart:tend,:].any(): continue
                    take[tstart:tend,:] = 1
                    ok = 1
# Select additional individual points
        ntot = tspan*npernight
        obs = scipy.reshape(obs, ntot)
        take = scipy.reshape(take, ntot)
        index = scipy.argsort(obs)
        obs = obs[index]
        take = take[index]
        for i in scipy.arange(nrand):
            ok = 0
            while ok == 0:
                t = scipy.fix(scipy.rand(1) * float(ntot))
                t = t[0]
                if take[t] == 1: continue
                take[t] = 1
                ok = 1
        time = obs[(take==1)]
    time -= time[0]
    return time
Ejemplo n.º 3
0
def enframe(x, win, inc):
    """
    Splits the vector up into (overlapping) frames beginning at increments
    of inc. Each frame is multiplied by the window win().
    The length of the frames is given by the length of the window win().
    The centre of frame I is x((I-1)*inc+(length(win)+1)/2) for I=1,2,...

    :param x: signal to split in frames
    :param win: window multiplied to each frame, length determines frame length
    :param inc: increment to shift frames, in samples
    :return f: output matrix, each frame occupies one row
    :return length, no_win: length of each frame in samples, number of frames
    """
    nx = len(x)
    nwin = len(win)
    if (nwin == 1):
        length = win
    else:
        # length = next_pow_2(nwin)
        length = nwin
    nf = int(fix((nx - length + inc) // inc))
    # f = np.zeros((nf, length))
    indf = inc * np.arange(nf)
    inds = np.arange(length) + 1
    f = x[(np.transpose(np.vstack([indf] * length)) + np.vstack([inds] * nf)) -
          1]
    if (nwin > 1):
        w = np.transpose(win)
        f = f * np.vstack([w] * nf)
    f = signal.detrend(f, type='constant')
    no_win, _ = f.shape
    return f, length, no_win
Ejemplo n.º 4
0
def enframe(x, win, inc):
    """
    Splits the vector up into (overlapping) frames beginning at increments
    of inc. Each frame is multiplied by the window win().
    The length of the frames is given by the length of the window win().
    The centre of frame I is x((I-1)*inc+(length(win)+1)/2) for I=1,2,...

    :param x: signal to split in frames
    :param win: window multiplied to each frame, length determines frame length
    :param inc: increment to shift frames, in samples
    :return f: output matrix, each frame occupies one row
    :return length, no_win: length of each frame in samples, number of frames
    """
    nx = len(x)
    nwin = len(win)
    if (nwin == 1):
        length = win
    else:
        # length = next_pow_2(nwin)
        length = nwin
    nf = int(fix((nx - length + inc) // inc))
    # f = np.zeros((nf, length))
    indf = inc * np.arange(nf)
    inds = np.arange(length) + 1
    f = x[(np.transpose(np.vstack([indf] * length)) +
           np.vstack([inds] * nf)) - 1]
    if (nwin > 1):
        w = np.transpose(win)
        f = f * np.vstack([w] * nf)
    f = signal.detrend(f, type='constant')
    no_win, _ = f.shape
    return f, length, no_win
Ejemplo n.º 5
0
def haar(N):
    h = np.zeros((N, N), dtype=float)
    h[0] = np.ones(N) / m.sqrt(N)

    for k in range(1, N):
        # TODO: What are the following lines doing?????
        p = sc.fix(m.log(k) / m.log(2))
        q = float(k - pow(2, p))
        k1 = float(pow(2, p))
        t1 = float(N / k1)
        k2 = float(pow(2, p+1))
        t2 = float(N / k2)

        for i in range(1, int(sc.fix(t2)) + 1):
            h[k, i+q*t1-1] = pow(2, (p/2)) / m.sqrt(N)
            h[k, i+q*t1+t2-1] = -pow(2, (p/2)) / m.sqrt(N)

    return h
Ejemplo n.º 6
0
def haar(N):
    
    #Assuming N is a power of 2
    import numpy as np
    import math as m
    import scipy as sc
    h = np.zeros((N,N), dtype = float)
    h[0] = np.ones(N)/m.sqrt(N)
    for k in range(1,N) :
        
        p = sc.fix(m.log(k)/m.log(2))
        q = float(k - pow(2,p))
        k1 = float(pow(2,p))
        t1 = float(N / k1)
        k2 = float(pow(2,p+1))
        t2 = float(N / k2)
        
        for i in range(1,int(sc.fix(t2))+1):
            h[k,i+q*t1-1] = pow(2,(p/2))/m.sqrt(N)
            h[k,i+q*t1+t2-1] = -pow(2,(p/2))/m.sqrt(N)
    
    return h
Ejemplo n.º 7
0
def audio_to_frame(audio, win=signal.boxcar(160), inc=80):
    ## Same as obspy.signal.util.enframe
    nx = len(audio)
    nwin = len(win)
    if (nwin == 1):
        length = win
    else:
        length = nwin
    nf = int(fix((nx - length + inc) // inc))
    indf = inc * np.arange(nf)
    inds = np.arange(length) + 1
    f = audio[(np.transpose(np.vstack([indf] * length)) +
               np.vstack([inds] * nf)).astype(int) - 1]
    if (nwin > 1):
        w = np.transpose(win)
        f = f * np.vstack([w] * nf)
    f = np.transpose(f)
    return f
Ejemplo n.º 8
0
def rv_noise(time, tau = 0.5, ninit = 10):
    npt = len(time)
    # Burn-in section
    dt = 2.3 * tau / float(ninit)
    tinit = (scipy.arange(ninit) + 1) * dt
    tsim = scipy.append(time[0] - tinit[::-1], time)
    nsim = len(tsim)
    # Instead of Gaussian white noise, to simulate effects of moon and
    # such like, each point in the "WGN" component is drawn from a
    # distribution with a different sigma each night, where the sigma itself is
    # drawn from a powerlaw distribution between 1 and 10
    wt = pylab.normal(0, 1, nsim)
    ye = scipy.ones(nsim)
    nights = scipy.fix(tsim)
    unights = scipy.unique(nights)
    nnights = len(unights)
    sigma = 10.0 ** (scipy.rand(nnights))**4
    for i in scipy.arange(nnights):
        l = scipy.where(nights == unights[i])[0]
        if l.any():
            wt[l] *= sigma[i]
            ye[l] = sigma[i]
    # Now apply MA model with exponentially decaying correlation
    ysim = scipy.copy(wt)
    for i in scipy.arange(nsim):
        j = i - 1
        coeff = 1.0
        while (j > 0) * (coeff > 0.1):
            dt = tsim[i] - tsim[j]
            coeff = scipy.exp(-dt/tau)
            ysim[i] += coeff * wt[j]
            j -= 1
    # Discard burn-in and globally re-scale before returning
    yout = ysim[ninit:]
    eout = ye[ninit:]
    med, sig = filter.medsig(yout)
    yout /= sig
    eout /= sig
    return yout, eout
Ejemplo n.º 9
0
def enframe(x, win, inc):

    nx = len(x)
    try:
        nwin = len(win)
    except TypeError:
        nwin = 1
    if nwin == 1:
        length = win
    else:
        length = nwin

    nf = int(fix((nx - length + inc) // inc))
    indf = inc * np.arange(nf)
    inds = np.arange(length) + 1
    f = x[(np.transpose(np.vstack([indf] * length)) + np.vstack([inds] * nf)) -
          1]
    if (nwin > 1):
        w = np.transpose(win)
        f = f * np.vstack([w] * nf)
    #f = signal.detrend(f, type='constant')
    #no_win, _ = f
    return f
Ejemplo n.º 10
0
def jmat(j, *args):
    """Higher-order spin operators:

    Parameters
    ----------
    j : float
        Spin of operator

    args : str
        Which operator to return 'x','y','z','+','-'.
        If no args given, then output is ['x','y','z']

    Returns
    -------
    jmat : qobj/list
        ``qobj`` for requested spin operator(s).


    Examples
    --------
    >>> jmat(1)
    [ Quantum object: dims = [[3], [3]], \
shape = [3, 3], type = oper, isHerm = True
    Qobj data =
    [[ 0.          0.70710678  0.        ]
     [ 0.70710678  0.          0.70710678]
     [ 0.          0.70710678  0.        ]]
     Quantum object: dims = [[3], [3]], \
shape = [3, 3], type = oper, isHerm = True
    Qobj data =
    [[ 0.+0.j          0.+0.70710678j  0.+0.j        ]
     [ 0.-0.70710678j  0.+0.j          0.+0.70710678j]
     [ 0.+0.j          0.-0.70710678j  0.+0.j        ]]
     Quantum object: dims = [[3], [3]], \
shape = [3, 3], type = oper, isHerm = True
    Qobj data =
    [[ 1.  0.  0.]
     [ 0.  0.  0.]
     [ 0.  0. -1.]]]


    Notes
    -----
    If no 'args' input, then returns array of ['x','y','z'] operators.

    """
    if (scipy.fix(2 * j) != 2 * j) or (j < 0):
        raise TypeError('j must be a non-negative integer or half-integer')
    if not args:
        a1 = Qobj(0.5 * (jplus(j) + jplus(j).conj().T))
        a2 = Qobj(0.5 * 1j * (jplus(j) - jplus(j).conj().T))
        a3 = Qobj(jz(j))
        return np.array([a1, a2, a3])
    if args[0] == '+':
        A = jplus(j)
    elif args[0] == '-':
        A = jplus(j).conj().T
    elif args[0] == 'x':
        A = 0.5 * (jplus(j) + jplus(j).conj().T)
    elif args[0] == 'y':
        A = -0.5 * 1j * (jplus(j) - jplus(j).conj().T)
    elif args[0] == 'z':
        A = jz(j)
    else:
        raise TypeError('Invalid type')
    return Qobj(A.tocsr())
Ejemplo n.º 11
0
def jmat(j, *args):
    """Higher-order spin operators:

    Parameters
    ----------
    j : float
        Spin of operator

    args : str
        Which operator to return 'x','y','z','+','-'.
        If no args given, then output is ['x','y','z']

    Returns
    -------
    jmat : qobj/list
        ``qobj`` for requested spin operator(s).


    Examples
    --------
    >>> jmat(1)
    [ Quantum object: dims = [[3], [3]], \
shape = [3, 3], type = oper, isHerm = True
    Qobj data =
    [[ 0.          0.70710678  0.        ]
     [ 0.70710678  0.          0.70710678]
     [ 0.          0.70710678  0.        ]]
     Quantum object: dims = [[3], [3]], \
shape = [3, 3], type = oper, isHerm = True
    Qobj data =
    [[ 0.+0.j          0.+0.70710678j  0.+0.j        ]
     [ 0.-0.70710678j  0.+0.j          0.+0.70710678j]
     [ 0.+0.j          0.-0.70710678j  0.+0.j        ]]
     Quantum object: dims = [[3], [3]], \
shape = [3, 3], type = oper, isHerm = True
    Qobj data =
    [[ 1.  0.  0.]
     [ 0.  0.  0.]
     [ 0.  0. -1.]]]


    Notes
    -----
    If no 'args' input, then returns array of ['x','y','z'] operators.

    """
    if (scipy.fix(2 * j) != 2 * j) or (j < 0):
        raise TypeError('j must be a non-negative integer or half-integer')
    if not args:
        a1 = Qobj(0.5 * (jplus(j) + jplus(j).conj().T))
        a2 = Qobj(0.5 * 1j * (jplus(j) - jplus(j).conj().T))
        a3 = Qobj(jz(j))
        return np.array([a1, a2, a3])
    if args[0] == '+':
        A = jplus(j)
    elif args[0] == '-':
        A = jplus(j).conj().T
    elif args[0] == 'x':
        A = 0.5 * (jplus(j) + jplus(j).conj().T)
    elif args[0] == 'y':
        A = -0.5 * 1j * (jplus(j) - jplus(j).conj().T)
    elif args[0] == 'z':
        A = jz(j)
    else:
        raise TypeError('Invlaid type')
    return Qobj(A.tocsr())