Esempio n. 1
0
def rk4(derivs, y0, t):
    """
    Integrate 1D or ND system of ODEs from initial state y0 at sample
    times t.  derivs returns the derivative of the system and has the
    signature

     dy = derivs(yi, ti)

    Example 1 :

        ## 2D system
        # Numeric solution
        def derivs6(x,t):
            d1 =  x[0] + 2*x[1]
            d2 =  -3*x[0] + 4*x[1]
            return (d1, d2)
        dt = 0.0005
        t = arange(0.0, 2.0, dt)
        y0 = (1,2)
        yout = rk4(derivs6, y0, t)

    Example 2:

        ## 1D system
        alpha = 2
        def derivs(x,t):
            return -alpha*x + exp(-t)

        y0 = 1
        yout = rk4(derivs, y0, t)


    """
   
    try: Ny = len(y0)
    except TypeError:
        yout = zeros( (len(t),), Float)
    else:
        yout = zeros( (len(t), Ny), Float)
        
        
    yout[0] = y0
    i = 0
    
    for i in arange(len(t)-1):

        thist = t[i]
        dt = t[i+1] - thist
        dt2 = dt/2.0
        y0 = yout[i]

        k1 = asarray(derivs(y0, thist))
        k2 = asarray(derivs(y0 + dt2*k1, thist+dt2))
        k3 = asarray(derivs(y0 + dt2*k2, thist+dt2))
        k4 = asarray(derivs(y0 + dt*k3, thist+dt))
        yout[i+1] = y0 + dt/6.0*(k1 + 2*k2 + 2*k3 + k4)
    return yout
Esempio n. 2
0
def rk4(derivs, y0, t):
    """
    Integrate 1D or ND system of ODEs from initial state y0 at sample
    times t.  derivs returns the derivative of the system and has the
    signature

     dy = derivs(yi, ti)

    Example 1 :

        ## 2D system
        # Numeric solution
        def derivs6(x,t):
            d1 =  x[0] + 2*x[1]
            d2 =  -3*x[0] + 4*x[1]
            return (d1, d2)
        dt = 0.0005
        t = arange(0.0, 2.0, dt)
        y0 = (1,2)
        yout = rk4(derivs6, y0, t)

    Example 2:

        ## 1D system
        alpha = 2
        def derivs(x,t):
            return -alpha*x + exp(-t)

        y0 = 1
        yout = rk4(derivs, y0, t)


    """

    try:
        Ny = len(y0)
    except TypeError:
        yout = zeros((len(t), ), Float)
    else:
        yout = zeros((len(t), Ny), Float)

    yout[0] = y0
    i = 0

    for i in arange(len(t) - 1):

        thist = t[i]
        dt = t[i + 1] - thist
        dt2 = dt / 2.0
        y0 = yout[i]

        k1 = asarray(derivs(y0, thist))
        k2 = asarray(derivs(y0 + dt2 * k1, thist + dt2))
        k3 = asarray(derivs(y0 + dt2 * k2, thist + dt2))
        k4 = asarray(derivs(y0 + dt * k3, thist + dt))
        yout[i + 1] = y0 + dt / 6.0 * (k1 + 2 * k2 + 2 * k3 + k4)
    return yout
Esempio n. 3
0
 def __init__(self, nmax):
     'buffer up to nmax points'
     self._xa = nx.zeros((nmax, ), typecode=nx.Float)
     self._ya = nx.zeros((nmax, ), typecode=nx.Float)
     self._xs = nx.zeros((nmax, ), typecode=nx.Float)
     self._ys = nx.zeros((nmax, ), typecode=nx.Float)
     self._ind = 0
     self._nmax = nmax
     self.dataLim = None
     self.callbackd = {}
Esempio n. 4
0
 def __init__(self, nmax):
     'buffer up to nmax points'
     self._xa = nx.zeros((nmax,), typecode=nx.Float)
     self._ya = nx.zeros((nmax,), typecode=nx.Float)        
     self._xs = nx.zeros((nmax,), typecode=nx.Float)
     self._ys = nx.zeros((nmax,), typecode=nx.Float)        
     self._ind = 0
     self._nmax = nmax
     self.dataLim = None
     self.callbackd = {}
Esempio n. 5
0
def unmasked_index_ranges(mask, compressed = True):
    '''
    Calculate the good data ranges in a masked 1-D array, based on mask.

    Returns Nx2 array with each row the start and stop indices
    for slices of the compressed array corresponding to each of N
    uninterrupted runs of unmasked values.
    If optional argument compressed is False, it returns the
    start and stop indices into the original array, not the
    compressed array.
    In either case, an empty array is returned if there are no
    unmasked values.

    Example:

    y = ma.array(arange(5), mask = [0,0,1,0,0])
    #ii = unmasked_index_ranges(y.mask())
    ii = unmasked_index_ranges(ma.getmask(y))
        # returns [[0,2,] [2,4,]]

    y.compressed().filled()[ii[1,0]:ii[1,1]]
        # returns array [3,4,]
        # (The 'filled()' method converts the masked array to a numerix array.)

    #i0, i1 = unmasked_index_ranges(y.mask(), compressed=False)
    i0, i1 = unmasked_index_ranges(ma.getmask(y), compressed=False)
        # returns [[0,3,] [2,5,]]

    y.filled()[ii[1,0]:ii[1,1]]
        # returns array [3,4,]

    '''
    m = concatenate(((1,), mask, (1,)))
    indices = arange(len(mask) + 1)
    mdif = m[1:] - m[:-1]
    i0 = compress(mdif == -1, indices)
    i1 = compress(mdif == 1, indices)
    assert len(i0) == len(i1)
    if not compressed:
        if len(i1):
            return concatenate((i0[:, ma.NewAxis], i1[:, ma.NewAxis]), axis=1)
        else:
            return zeros((0,0), 'i')
    seglengths = i1 - i0
    breakpoints = cumsum(seglengths)
    try:
        ic0 = concatenate(((0,), breakpoints[:-1]))
        ic1 = breakpoints
        return concatenate((ic0[:, ma.NewAxis], ic1[:, ma.NewAxis]), axis=1)
    except:
        return zeros((0,0), 'i')
Esempio n. 6
0
def unmasked_index_ranges(mask, compressed=True):
    '''
    Calculate the good data ranges in a masked 1-D array, based on mask.

    Returns Nx2 array with each row the start and stop indices
    for slices of the compressed array corresponding to each of N
    uninterrupted runs of unmasked values.
    If optional argument compressed is False, it returns the
    start and stop indices into the original array, not the
    compressed array.
    In either case, an empty array is returned if there are no
    unmasked values.

    Example:

    y = ma.array(arange(5), mask = [0,0,1,0,0])
    #ii = unmasked_index_ranges(y.mask())
    ii = unmasked_index_ranges(ma.getmask(y))
        # returns [[0,2,] [2,4,]]

    y.compressed().filled()[ii[1,0]:ii[1,1]]
        # returns array [3,4,]
        # (The 'filled()' method converts the masked array to a numerix array.)

    #i0, i1 = unmasked_index_ranges(y.mask(), compressed=False)
    i0, i1 = unmasked_index_ranges(ma.getmask(y), compressed=False)
        # returns [[0,3,] [2,5,]]

    y.filled()[ii[1,0]:ii[1,1]]
        # returns array [3,4,]

    '''
    m = concatenate(((1, ), mask, (1, )))
    indices = arange(len(mask) + 1)
    mdif = m[1:] - m[:-1]
    i0 = compress(mdif == -1, indices)
    i1 = compress(mdif == 1, indices)
    assert len(i0) == len(i1)
    if not compressed:
        if len(i1):
            return concatenate((i0[:, ma.NewAxis], i1[:, ma.NewAxis]), axis=1)
        else:
            return zeros((0, 0), 'i')
    seglengths = i1 - i0
    breakpoints = cumsum(seglengths)
    try:
        ic0 = concatenate(((0, ), breakpoints[:-1]))
        ic1 = breakpoints
        return concatenate((ic0[:, ma.NewAxis], ic1[:, ma.NewAxis]), axis=1)
    except:
        return zeros((0, 0), 'i')
Esempio n. 7
0
def psd(x, NFFT=256, Fs=2, detrend=detrend_none,
        window=window_hanning, noverlap=0):
    """
    The power spectral density by Welches average periodogram method.
    The vector x is divided into NFFT length segments.  Each segment
    is detrended by function detrend and windowed by function window.
    noperlap gives the length of the overlap between segments.  The
    absolute(fft(segment))**2 of each segment are averaged to compute Pxx,
    with a scaling to correct for power loss due to windowing.  Fs is
    the sampling frequency.

    -- NFFT must be a power of 2
    -- detrend and window are functions, unlike in matlab where they are
       vectors.
    -- if length x < NFFT, it will be zero padded to NFFT
    

    Returns the tuple Pxx, freqs

    Refs:
      Bendat & Piersol -- Random Data: Analysis and Measurement
        Procedures, John Wiley & Sons (1986)

    """

    if NFFT % 2:
        raise ValueError, 'NFFT must be a power of 2'

    # zero pad x up to NFFT if it is shorter than NFFT
    if len(x)<NFFT:
        n = len(x)
        x = resize(x, (NFFT,))
        x[n:] = 0
    

    # for real x, ignore the negative frequencies
    if x.typecode()==Complex: numFreqs = NFFT
    else: numFreqs = NFFT//2+1
        
    windowVals = window(ones((NFFT,),x.typecode()))
    step = NFFT-noverlap
    ind = range(0,len(x)-NFFT+1,step)
    n = len(ind)
    Pxx = zeros((numFreqs,n), Float)
    # do the ffts of the slices
    for i in range(n):
        thisX = x[ind[i]:ind[i]+NFFT]
        thisX = windowVals*detrend(thisX)
        fx = absolute(fft(thisX))**2
        Pxx[:,i] = divide(fx[:numFreqs], norm(windowVals)**2)

    # Scale the spectrum by the norm of the window to compensate for
    # windowing loss; see Bendat & Piersol Sec 11.5.2
    if n>1:
       Pxx = mean(Pxx,1)

    freqs = Fs/NFFT*arange(numFreqs)
    Pxx.shape = len(freqs),

    return Pxx, freqs
Esempio n. 8
0
    def __call__(self, X, alpha=1.0):
        """
        X is either a scalar or an array (of any dimension).
        If scalar, a tuple of rgba values is returned, otherwise
        an array with the new shape = oldshape+(4,).  Any values
        that are outside the 0,1 interval are clipped to that
        interval before generating rgb values.  
        Alpha must be a scalar
        """
        if not self._isinit: self._init()
        alpha = min(alpha, 1.0) # alpha must be between 0 and 1
        alpha = max(alpha, 0.0)
        if type(X) in [IntType, FloatType]:
            vtype = 'scalar'
            xa = array([X])
        else:
            vtype = 'array'
            xa = asarray(X)

        # assume the data is properly normalized
        #xa = where(xa>1.,1.,xa)
        #xa = where(xa<0.,0.,xa)


        xa = (xa *(self.N-1)).astype(Int)
        rgba = zeros(xa.shape+(4,), Float)
        rgba[...,0] = take(self._red_lut, xa)
        rgba[...,1] = take(self._green_lut, xa)
        rgba[...,2] = take(self._blue_lut, xa)
        rgba[...,3] = alpha
        if vtype == 'scalar':
            rgba = tuple(rgba[0,:])
        return rgba
Esempio n. 9
0
 def _init(self):
     rgb = array([colorConverter.to_rgb(c)
                 for c in self.colors], typecode=Float)
     self._lut = zeros((self.N + 3, 4), Float)
     self._lut[:-3, :-1] = rgb
     self._isinit = True
     self._set_extremes()
Esempio n. 10
0
def longest_ones(x):
    """
    return the indicies of the longest stretch of contiguous ones in x,
    assuming x is a vector of zeros and ones.

    If there are two equally long stretches, pick the first
    """
    x = asarray(x)
    if len(x) == 0: return array([])

    #print 'x', x
    ind = find(x == 0)
    if len(ind) == 0: return arange(len(x))
    if len(ind) == len(x): return array([])

    y = zeros((len(x) + 2, ), Int)
    y[1:-1] = x
    d = diff(y)
    #print 'd', d
    up = find(d == 1)
    dn = find(d == -1)

    #print 'dn', dn, 'up', up,
    ind = find(dn - up == max(dn - up))
    # pick the first
    if iterable(ind): ind = ind[0]
    ind = arange(up[ind], dn[ind])

    return ind
Esempio n. 11
0
def psd(x, NFFT=256, Fs=2, detrend=detrend_none,
        window=window_hanning, noverlap=0):
    """
    The power spectral density by Welches average periodogram method.
    The vector x is divided into NFFT length segments.  Each segment
    is detrended by function detrend and windowed by function window.
    noperlap gives the length of the overlap between segments.  The
    absolute(fft(segment))**2 of each segment are averaged to compute Pxx,
    with a scaling to correct for power loss due to windowing.  Fs is
    the sampling frequency.

    -- NFFT must be a power of 2
    -- detrend and window are functions, unlike in matlab where they are
       vectors.
    -- if length x < NFFT, it will be zero padded to NFFT
    

    Returns the tuple Pxx, freqs

    Refs:
      Bendat & Piersol -- Random Data: Analysis and Measurement
        Procedures, John Wiley & Sons (1986)

    """

    if NFFT % 2:
        raise ValueError, 'NFFT must be a power of 2'

    # zero pad x up to NFFT if it is shorter than NFFT
    if len(x)<NFFT:
        n = len(x)
        x = resize(x, (NFFT,))
        x[n:] = 0
    

    # for real x, ignore the negative frequencies
    if x.typecode()==Complex: numFreqs = NFFT
    else: numFreqs = NFFT//2+1
        
    windowVals = window(ones((NFFT,),x.typecode()))
    step = NFFT-noverlap
    ind = range(0,len(x)-NFFT+1,step)
    n = len(ind)
    Pxx = zeros((numFreqs,n), Float)
    # do the ffts of the slices
    for i in range(n):
        thisX = x[ind[i]:ind[i]+NFFT]
        thisX = windowVals*detrend(thisX)
        fx = absolute(fft(thisX))**2
        Pxx[:,i] = divide(fx[:numFreqs], norm(windowVals)**2)

    # Scale the spectrum by the norm of the window to compensate for
    # windowing loss; see Bendat & Piersol Sec 11.5.2
    if n>1:
       Pxx = mean(Pxx,1)

    freqs = Fs/NFFT*arange(numFreqs)
    Pxx.shape = len(freqs),

    return Pxx, freqs
Esempio n. 12
0
 def _init(self):
     rgb = array([colorConverter.to_rgb(c) for c in self.colors],
                 typecode=Float)
     self._lut = zeros((self.N + 3, 4), Float)
     self._lut[:-3, :-1] = rgb
     self._isinit = True
     self._set_extremes()
Esempio n. 13
0
def longest_ones(x):
    """
    return the indicies of the longest stretch of contiguous ones in x,
    assuming x is a vector of zeros and ones.

    If there are two equally long stretches, pick the first
    """
    x = asarray(x)
    if len(x)==0: return array([])

    #print 'x', x
    ind = find(x==0)
    if len(ind)==0:  return arange(len(x))
    if len(ind)==len(x): return array([])

    y = zeros( (len(x)+2,), Int)
    y[1:-1] = x
    d = diff(y)
    #print 'd', d
    up = find(d ==  1);
    dn = find(d == -1);

    #print 'dn', dn, 'up', up, 
    ind = find( dn-up == max(dn - up))
    # pick the first
    if iterable(ind): ind = ind[0]
    ind = arange(up[ind], dn[ind])

    return ind
Esempio n. 14
0
    def __call__(self, X, alpha=1.0):
        """
        X is either a scalar or an array (of any dimension).
        If scalar, a tuple of rgba values is returned, otherwise
        an array with the new shape = oldshape+(4,).  Any values
        that are outside the 0,1 interval are clipped to that
        interval before generating rgb values.  
        Alpha must be a scalar
        """
        alpha = min(alpha, 1.0) # alpha must be between 0 and 1
        alpha = max(alpha, 0.0)
        if type(X) in [IntType, FloatType]:
            vtype = 'scalar'
            xa = array([X])
        else:
            vtype = 'array'
            xa = array(X)

        xa = where(xa>1.,1.,xa)
        xa = where(xa<0.,0.,xa)
        xa = (xa *(self.N-1)).astype(Int)
        rgba = zeros(xa.shape+(4,), Float)
        rgba[...,0] = take(self._red_lut, xa)
        rgba[...,1] = take(self._green_lut, xa)
        rgba[...,2] = take(self._blue_lut, xa)
        rgba[...,3] = alpha
        if vtype == 'scalar':
            rgba = tuple(rgba[0,:])
        return rgba
Esempio n. 15
0
def get_sparse_matrix(M,N,frac=0.1):
    'return a MxN sparse matrix with frac elements randomly filled'
    data = zeros((M,N))*0.
    for i in range(int(M*N*frac)):
        x = random.randint(0,M-1)
        y = random.randint(0,N-1)
        data[x,y] = rand()
    return data
Esempio n. 16
0
def get_sparse_matrix(M, N, frac=0.1):
    'return a MxN sparse matrix with frac elements randomly filled'
    data = zeros((M, N)) * 0.
    for i in range(int(M * N * frac)):
        x = random.randint(0, M - 1)
        y = random.randint(0, N - 1)
        data[x, y] = rand()
    return data
Esempio n. 17
0
def movavg(x, n):
    'compute the len(n) moving average of x'
    n = int(n)
    N = len(x)
    assert (N > n)
    y = zeros(N - (n - 1), Float)
    for i in range(n):
        y += x[i:N - (n - 1) + i]
    y /= float(n)
    return y
Esempio n. 18
0
def movavg(x,n):
    'compute the len(n) moving average of x'
    n = int(n)
    N = len(x)
    assert(N>n)
    y = zeros(N-(n-1),Float)
    for i in range(n):
       y += x[i:N-(n-1)+i]
    y /= float(n)
    return y
Esempio n. 19
0
def symbols(epsoutfile):
    y = arange(5)/5.0+0.1
    
    g = pyxgraph(xlimits=(-1, 25), ylimits=(0, 1),           
                 xticks=(0, 24, 2), yticks=(0, 1, 1), key=None) 
    
    for i in xrange(25):
        x = zeros(5)+i   
        g.pyxplot((x, y), style="p", pt=i)   # ``pt=i`` can be omitted
                                             # (then the next symbol is choosen
                                             #  automatically)
    g.pyxsave(epsoutfile)
def makeMappingArray(N, data):
    """Create an N-element 1-d lookup table

    data represented by a list of x,y0,y1 mapping correspondences.
    Each element in this list represents how a value between 0 and 1
    (inclusive) represented by x is mapped to a corresponding value
    between 0 and 1 (inclusive). The two values of y are to allow
    for discontinuous mapping functions (say as might be found in a
    sawtooth) where y0 represents the value of y for values of x
    <= to that given, and y1 is the value to be used for x > than
    that given). The list must start with x=0, end with x=1, and
    all values of x must be in increasing order. Values between
    the given mapping points are determined by simple linear interpolation.

    The function returns an array "result" where result[x*(N-1)]
    gives the closest value for values of x between 0 and 1.
    """
    try:
        adata = array(data)
    except:
        raise TypeError("data must be convertable to an array")
    shape = adata.shape
    if len(shape) != 2 and shape[1] != 3:
        raise ValueError("data must be nx3 format")

    x  = adata[:,0]
    y0 = adata[:,1]
    y1 = adata[:,2]

    if x[0] != 0. or x[-1] != 1.0:
        raise ValueError(
           "data mapping points must start with x=0. and end with x=1")
    if sometrue(sort(x)-x):
        raise ValueError(
           "data mapping points must have x in increasing order")
    # begin generation of lookup table
    x = x * (N-1)
    lut = zeros((N,), Float)
    xind = arange(float(N))
    ind = searchsorted(x, xind)[1:-1]

    lut[1:-1] = ( divide(xind[1:-1] - take(x,ind-1),
                         take(x,ind)-take(x,ind-1) )
                  *(take(y0,ind)-take(y1,ind-1)) + take(y1,ind-1))
    lut[0] = y1[0]
    lut[-1] = y0[-1]
    # ensure that the lut is confined to values between 0 and 1 by clipping it
    clip(lut, 0.0, 1.0)
    #lut = where(lut > 1., 1., lut)
    #lut = where(lut < 0., 0., lut)
    return lut
Esempio n. 21
0
def specgram(x,
             NFFT=256,
             Fs=2,
             detrend=detrend_none,
             window=window_hanning,
             noverlap=128):
    """
    Compute a spectrogram of data in x.  Data are split into NFFT
    length segements and the PSD of each section is computed.  The
    windowing function window is applied to each segment, and the
    amount of overlap of each segment is specified with noverlap

    See pdf for more info.

    The returned times are the midpoints of the intervals over which
    the ffts are calculated
    """
    x = asarray(x)
    assert (NFFT > noverlap)
    if log(NFFT) / log(2) != int(log(NFFT) / log(2)):
        raise ValueError, 'NFFT must be a power of 2'

    # zero pad x up to NFFT if it is shorter than NFFT
    if len(x) < NFFT:
        n = len(x)
        x = resize(x, (NFFT, ))
        x[n:] = 0

    # for real x, ignore the negative frequencies
    if typecode(x) == Complex: numFreqs = NFFT
    else: numFreqs = NFFT // 2 + 1

    windowVals = window(ones((NFFT, ), typecode(x)))
    step = NFFT - noverlap
    ind = arange(0, len(x) - NFFT + 1, step)
    n = len(ind)
    Pxx = zeros((numFreqs, n), Float)
    # do the ffts of the slices

    for i in range(n):
        thisX = x[ind[i]:ind[i] + NFFT]
        thisX = windowVals * detrend(thisX)
        fx = absolute(fft(thisX))**2
        # Scale the spectrum by the norm of the window to compensate for
        # windowing loss; see Bendat & Piersol Sec 11.5.2
        Pxx[:, i] = divide(fx[:numFreqs], norm(windowVals)**2)
    t = 1 / Fs * (ind + NFFT / 2)
    freqs = Fs / NFFT * arange(numFreqs)

    return Pxx, freqs, t
Esempio n. 22
0
def makeMappingArray(N, data):
    """Create an N-element 1-d lookup table
    
    data represented by a list of x,y0,y1 mapping correspondences.
    Each element in this list represents how a value between 0 and 1
    (inclusive) represented by x is mapped to a corresponding value
    between 0 and 1 (inclusive). The two values of y are to allow 
    for discontinuous mapping functions (say as might be found in a
    sawtooth) where y0 represents the value of y for values of x
    <= to that given, and y1 is the value to be used for x > than
    that given). The list must start with x=0, end with x=1, and 
    all values of x must be in increasing order. Values between
    the given mapping points are determined by simple linear interpolation.
    
    The function returns an array "result" where result[x*(N-1)]
    gives the closest value for values of x between 0 and 1.
    """
    try:
        adata = array(data)
    except:
        raise TypeError("data must be convertable to an array")
    shape = adata.shape
    if len(shape) != 2 and shape[1] != 3:
        raise ValueError("data must be nx3 format")

    x  = adata[:,0]
    y0 = adata[:,1]
    y1 = adata[:,2]

    if x[0] != 0. or x[-1] != 1.0:
        raise ValueError(
           "data mapping points must start with x=0. and end with x=1")
    if sometrue(sort(x)-x):
        raise ValueError(
           "data mapping points must have x in increasing order")
    # begin generation of lookup table
    x = x * (N-1)
    lut = zeros((N,), Float)
    xind = arange(float(N))
    ind = searchsorted(x, xind)[1:-1]
    
    lut[1:-1] = ( divide(xind[1:-1] - take(x,ind-1),
                         take(x,ind)-take(x,ind-1) )
                  *(take(y0,ind)-take(y1,ind-1)) + take(y1,ind-1))
    lut[0] = y1[0]
    lut[-1] = y0[-1]
    # ensure that the lut is confined to values between 0 and 1 by clipping it
    lut = where(lut > 1., 1., lut)
    lut = where(lut < 0., 0., lut)
    return lut
Esempio n. 23
0
def specgram(x, NFFT=256, Fs=2, detrend=detrend_none,
             window=window_hanning, noverlap=128):
    """
    Compute a spectrogram of data in x.  Data are split into NFFT
    length segements and the PSD of each section is computed.  The
    windowing function window is applied to each segment, and the
    amount of overlap of each segment is specified with noverlap

    See pdf for more info.

    The returned times are the midpoints of the intervals over which
    the ffts are calculated
    """

    assert(NFFT>noverlap)
    if log(NFFT)/log(2) != int(log(NFFT)/log(2)):
       raise ValueError, 'NFFT must be a power of 2'

    # zero pad x up to NFFT if it is shorter than NFFT
    if len(x)<NFFT:
        n = len(x)
        x = resize(x, (NFFT,))
        x[n:] = 0
    

    # for real x, ignore the negative frequencies
    if x.typecode()==Complex: numFreqs = NFFT
    else: numFreqs = NFFT//2+1
        
    windowVals = window(ones((NFFT,),x.typecode()))
    step = NFFT-noverlap
    ind = arange(0,len(x)-NFFT+1,step)
    n = len(ind)
    Pxx = zeros((numFreqs,n), Float)
    # do the ffts of the slices

    for i in range(n):
        thisX = x[ind[i]:ind[i]+NFFT]
        thisX = windowVals*detrend(thisX)
        fx = absolute(fft(thisX))**2
        # Scale the spectrum by the norm of the window to compensate for
        # windowing loss; see Bendat & Piersol Sec 11.5.2
        Pxx[:,i] = divide(fx[:numFreqs], norm(windowVals)**2)
    t = 1/Fs*(ind+NFFT/2)
    freqs = Fs/NFFT*arange(numFreqs)

    return Pxx, freqs, t
Esempio n. 24
0
def identity(n, rank=2, typecode='l'):
    """identity(n,r) returns the identity matrix of shape (n,n,...,n) (rank r).

    For ranks higher than 2, this object is simply a multi-index Kronecker
    delta:
                        /  1  if i0=i1=...=iR,
    id[i0,i1,...,iR] = -|
                        \  0  otherwise.

    Optionally a typecode may be given (it defaults to 'l').

    Since rank defaults to 2, this function behaves in the default case (when
    only n is given) like the Numeric identity function."""

    iden = zeros((n, ) * rank, typecode=typecode)
    for i in range(n):
        idx = (i, ) * rank
        iden[idx] = 1
    return iden
Esempio n. 25
0
def identity(n,rank=2,typecode='l'):
    """identity(n,r) returns the identity matrix of shape (n,n,...,n) (rank r).

    For ranks higher than 2, this object is simply a multi-index Kronecker
    delta:
                        /  1  if i0=i1=...=iR,
    id[i0,i1,...,iR] = -|
                        \  0  otherwise.

    Optionally a typecode may be given (it defaults to 'l').

    Since rank defaults to 2, this function behaves in the default case (when
    only n is given) like the Numeric identity function."""
    
    iden = zeros((n,)*rank,typecode=typecode)
    for i in range(n):
        idx = (i,)*rank
        iden[idx] = 1
    return iden
Esempio n. 26
0
def longest_contiguous_ones(x):
    """
    return the indicies of the longest stretch of contiguous ones in x,
    assuming x is a vector of zeros and ones.
    """
    if len(x)==0: return array([])

    ind = find(x==0)
    if len(ind)==0:  return arange(len(x))
    if len(ind)==len(x): return array([])

    y = zeros( (len(x)+2,),  x.typecode())
    y[1:-1] = x
    dif = diff(y)
    up = find(dif ==  1);
    dn = find(dif == -1);
    ind = find( dn-up == max(dn - up))
    ind = arange(take(up, ind), take(dn, ind))

    return ind
Esempio n. 27
0
def longest_contiguous_ones(x):
    """
    return the indicies of the longest stretch of contiguous ones in x,
    assuming x is a vector of zeros and ones.
    """
    if len(x) == 0: return array([])

    ind = find(x == 0)
    if len(ind) == 0: return arange(len(x))
    if len(ind) == len(x): return array([])

    y = zeros((len(x) + 2, ), typecode(x))
    y[1:-1] = x
    dif = diff(y)
    up = find(dif == 1)
    dn = find(dif == -1)
    ind = find(dn - up == max(dn - up))
    ind = arange(take(up, ind), take(dn, ind))

    return ind
Esempio n. 28
0
    def _initialize_reg_tri(self, z, badmask):
        '''
        Initialize two arrays used by the low-level contour
        algorithm.  This is temporary code; most of the reg
        initialization should be done in c.

        For each masked point, we need to mark as missing
        the four regions with that point as a corner.
        '''
        imax, jmax = shape(z)
        nreg = jmax*(imax+1)+1
        reg = ones((1, nreg), typecode = 'i')
        reg[0,:jmax+1]=0
        reg[0,-jmax:]=0
        for j in range(0, nreg, jmax):
            reg[0,j]=0
        if badmask is not None:
            for i in range(imax):
                for j in range(jmax):
                    if badmask[i,j]:
                        ii = i*jmax+j
                        if ii < nreg:
                            reg[0,ii] = 0
                        ii += 1
                        if ii < nreg:
                            reg[0,ii] = 0
                        ii += jmax
                        if ii < nreg:
                            reg[0,ii] = 0
                        ii -= 1
                        if ii < nreg:
                            reg[0,ii] = 0

        triangle = zeros((imax,jmax), typecode='s')

        return reg, triangle
Esempio n. 29
0
    def _initialize_reg_tri(self, z, badmask):
        '''
        Initialize two arrays used by the low-level contour
        algorithm.  This is temporary code; most of the reg
        initialization should be done in c.

        For each masked point, we need to mark as missing
        the four regions with that point as a corner.
        '''
        imax, jmax = shape(z)
        nreg = jmax * (imax + 1) + 1
        reg = ones((1, nreg), typecode='i')
        reg[0, :jmax + 1] = 0
        reg[0, -jmax:] = 0
        for j in range(0, nreg, jmax):
            reg[0, j] = 0
        if badmask is not None:
            for i in range(imax):
                for j in range(jmax):
                    if badmask[i, j]:
                        ii = i * jmax + j
                        if ii < nreg:
                            reg[0, ii] = 0
                        ii += 1
                        if ii < nreg:
                            reg[0, ii] = 0
                        ii += jmax
                        if ii < nreg:
                            reg[0, ii] = 0
                        ii -= 1
                        if ii < nreg:
                            reg[0, ii] = 0

        triangle = zeros((imax, jmax), typecode='s')

        return reg, triangle
Esempio n. 30
0
    def contourf(self, *args, **kwargs):
        """
        contourf(self, *args, **kwargs)

        Function signatures

        contourf(Z) - make a filled contour plot of an array Z. The level
                 values are chosen automatically.

        contourf(X,Y,Z) - X,Y specify the (x,y) coordinates of the surface

        contourf(Z,N) and contourf(X,Y,Z,N) - make a filled contour plot
                 corresponding to N contour levels

        contourf(Z,V) and contourf(X,Y,Z,V) - fill len(V) regions,
                 between the levels specified in sequence V, and a final region
                 for values of Z greater than the last element in V

        contourf(Z, **kwargs) - Use keyword args to control colors,
                    origin, cmap ... see below

        [L,C] = contourf(...) returns a list of levels and a silent_list
             of PolyCollections

        Optional keywork args are shown with their defaults below (you must
        use kwargs for these):

            * colors = None: one of these:
              - a tuple of matplotlib color args (string, float, rgb, etc),
              different levels will be plotted in different colors in the order
              specified

              -  one string color, e.g. colors = 'r' or colors = 'red', all levels
              will be plotted in this color

              - if colors == None, the default colormap will be used

            * alpha=1.0 : the alpha blending value

            * cmap = None: a cm Colormap instance from matplotlib.cm.

            * origin = None: 'upper'|'lower'|'image'|None.
              If 'image', the rc value for image.origin will be used.
              If None (default), the first value of Z will correspond
              to the lower left corner, location (0,0).
              This keyword is active only if contourf is called with
              one or two arguments, that is, without explicitly
              specifying X and Y.

            * badmask = None: array with dimensions of Z, and with values
              of zero at locations corresponding to valid data, and one
              at locations where the value of Z should be ignored.
              This is experimental.  It presently works for edge regions
              for line and filled contours, but for interior regions it
              works correctly only for line contours.  The badmask kwarg
              may go away in the future, to be replaced by the use of
              NaN value in Z and/or the use of a masked array in Z.

            reg is a 1D region number array with of imax*(jmax+1)+1 size
            The values of reg should be positive region numbers, and zero fro
            zones wich do not exist.

            triangle - triangulation array - must be the same shape as reg

            contourf differs from the Matlab (TM) version in that it does not
                draw the polygon edges (because the contouring engine yields
                simply connected regions with branch cuts.)  To draw the edges,
                add line contours with calls to contour.

        """

        alpha = kwargs.get('alpha', 1.0)
        origin = kwargs.get('origin', None)
        extent = kwargs.get('extent', None)
        cmap = kwargs.get('cmap', None)
        colors = kwargs.get('colors', None)
        badmask = kwargs.get('badmask', None)

        if cmap is not None: assert (isinstance(cmap, Colormap))
        if origin is not None: assert (origin in ['lower', 'upper', 'image'])

        if colors is not None and cmap is not None:
            raise RuntimeError('Either colors or cmap must be None')
        if origin == 'image': origin = rcParams['image.origin']

        x, y, z, lev = self._contour_args(True, badmask, origin, extent, *args)
        # Manipulate the plot *after* checking the input arguments.
        if not self.ax.ishold(): self.ax.cla()

        Nlev = len(lev)

        reg, triangle = self._initialize_reg_tri(z, badmask)

        tcolors, mappable, collections = self._process_colors(
            z, colors, alpha, lev, cmap)

        region = 0
        lev_upper = list(lev[1:])
        lev_upper.append(1e38)
        for level, level_upper, color in zip(lev, lev_upper, tcolors):
            levs = (level, level_upper)
            ntotal, nparts = _contour.GcInit2(x, y, reg, triangle, region, z,
                                              levs, 30)
            np = zeros((nparts, ), typecode='l')
            xp = zeros((ntotal, ), Float64)
            yp = zeros((ntotal, ), Float64)
            nlist = _contour.GcTrace(np, xp, yp)
            col = PolyCollection(nlist, linewidths=(1, ))
            # linewidths = 1 is necessary to avoid artifacts
            # in rendering the region boundaries.
            col.set_color(color)  # sets both facecolor and edgecolor
            self.ax.add_collection(col)
            collections.append(col)

        collections = silent_list('PolyCollection', collections)
        collections.mappable = mappable
        return lev, collections
Esempio n. 31
0
    def contour(self, *args, **kwargs):
        """
        contour(self, *args, **kwargs)

        Function signatures

        contour(Z) - make a contour plot of an array Z. The level
                 values are chosen automatically.

        contour(X,Y,Z) - X,Y specify the (x,y) coordinates of the surface

        contour(Z,N) and contour(X,Y,Z,N) - draw N contour lines overriding
                         the automatic value

        contour(Z,V) and contour(X,Y,Z,V) - draw len(V) contour lines,
                       at the values specified in V (array, list, tuple)

        contour(Z, **kwargs) - Use keyword args to control colors, linewidth,
                    origin, cmap ... see below

        [L,C] = contour(...) returns a list of levels and a silent_list of LineCollections

        Optional keywork args are shown with their defaults below (you must
        use kwargs for these):

            * colors = None: one of these:
              - a tuple of matplotlib color args (string, float, rgb, etc),
              different levels will be plotted in different colors in the order
              specified

              -  one string color, e.g. colors = 'r' or colors = 'red', all levels
              will be plotted in this color

              - if colors == None, the default colormap will be used

            * alpha=1.0 : the alpha blending value

            * cmap = None: a cm Colormap instance from matplotlib.cm.

            * origin = None: 'upper'|'lower'|'image'|None.
              If 'image', the rc value for image.origin will be used.
              If None (default), the first value of Z will correspond
              to the lower left corner, location (0,0).
              This keyword is active only if contourf is called with
              one or two arguments, that is, without explicitly
              specifying X and Y.

            * extent = None: (x0,x1,y0,y1); also active only if X and Y
              are not specified.

            * badmask = None: array with dimensions of Z, and with values
              of zero at locations corresponding to valid data, and one
              at locations where the value of Z should be ignored.
              This is experimental.  It presently works for edge regions
              for line and filled contours, but for interior regions it
              works correctly only for line contours.  The badmask kwarg
              may go away in the future, to be replaced by the use of
              NaN value in Z and/or the use of a masked array in Z.

            * linewidths = None: or one of these:
              - a number - all levels will be plotted with this linewidth,
                e.g. linewidths = 0.6

              - a tuple of numbers, e.g. linewidths = (0.4, 0.8, 1.2) different
                levels will be plotted with different linewidths in the order
                specified

              - if linewidths == None, the default width in lines.linewidth in
                .matplotlibrc is used

            * fmt = '1.3f': a format string for adding a label to each collection.
              Useful for auto-legending.

        """

        alpha = kwargs.get('alpha', 1.0)
        linewidths = kwargs.get('linewidths', None)
        fmt = kwargs.get('fmt', '%1.3f')
        origin = kwargs.get('origin', None)
        extent = kwargs.get('extent', None)
        cmap = kwargs.get('cmap', None)
        colors = kwargs.get('colors', None)
        badmask = kwargs.get('badmask', None)

        if cmap is not None: assert (isinstance(cmap, Colormap))
        if origin is not None: assert (origin in ['lower', 'upper', 'image'])
        if extent is not None: assert (len(extent) == 4)
        if colors is not None and cmap is not None:
            raise RuntimeError('Either colors or cmap must be None')
        # todo: shouldn't this use the current image rather than the rc param?
        if origin == 'image': origin = rcParams['image.origin']

        x, y, z, lev = self._contour_args(False, badmask, origin, extent,
                                          *args)

        # Manipulate the plot *after* checking the input arguments.
        if not self.ax.ishold(): self.ax.cla()

        Nlev = len(lev)
        if cmap is None:
            if colors is None:
                Ncolors = Nlev
            else:
                Ncolors = len(colors)
        else:
            Ncolors = Nlev

        reg, triangle = self._initialize_reg_tri(z, badmask)

        tcolors, mappable, collections = self._process_colors(
            z, colors, alpha, lev, cmap)

        if linewidths == None:
            tlinewidths = [rcParams['lines.linewidth']] * Nlev
        else:
            if iterable(linewidths) and len(linewidths) < Nlev:
                linewidths = list(linewidths) * int(
                    ceil(Nlev / len(linewidths)))
            elif not iterable(linewidths) and type(linewidths) in [int, float]:
                linewidths = [linewidths] * Nlev
            tlinewidths = [(w, ) for w in linewidths]

        region = 0
        for level, color, width in zip(lev, tcolors, tlinewidths):
            ntotal, nparts = _contour.GcInit1(x, y, reg, triangle, region, z,
                                              level)
            np = zeros((nparts, ), typecode='l')
            xp = zeros((ntotal, ), Float64)
            yp = zeros((ntotal, ), Float64)
            nlist = _contour.GcTrace(np, xp, yp)
            col = LineCollection(nlist)
            col.set_color(color)
            col.set_linewidth(width)

            if level < 0.0 and Ncolors == 1:
                col.set_linestyle((0, (6., 6.)), )
                #print "setting dashed"
            col.set_label(fmt % level)
            self.ax.add_collection(col)
            collections.append(col)

        collections = silent_list('LineCollection', collections)
        # the mappable attr is for the pylab interface functions,
        # which maintain the current image
        collections.mappable = mappable
        return lev, collections
Esempio n. 32
0
def zero(data, *args):
    if isinstance(data, numerix.ndarray):
        return numerix.zeros(data.shape, data.dtype)
    return numerix.zeros(len(data))
Esempio n. 33
0
def zeros_like(a):
    """Return an array of zeros of the shape and typecode of a."""

    return zeros(a.shape, typecode(a))
Esempio n. 34
0
def zero(data, *args):
    if isinstance(data, numerix.ndarray):
        return numerix.zeros(data.shape, data.dtype)
    return numerix.zeros(len(data))
Esempio n. 35
0
    def contour(self, *args, **kwargs):
        """
        contour(self, *args, **kwargs)

        Function signatures

        contour(Z) - make a contour plot of an array Z. The level
                 values are chosen automatically.

        contour(X,Y,Z) - X,Y specify the (x,y) coordinates of the surface

        contour(Z,N) and contour(X,Y,Z,N) - draw N contour lines overriding
                         the automatic value

        contour(Z,V) and contour(X,Y,Z,V) - draw len(V) contour lines,
                       at the values specified in V (array, list, tuple)

        contour(Z, **kwargs) - Use keyword args to control colors, linewidth,
                    origin, cmap ... see below

        [L,C] = contour(...) returns a list of levels and a silent_list of LineCollections

        Optional keywork args are shown with their defaults below (you must
        use kwargs for these):

            * colors = None: one of these:
              - a tuple of matplotlib color args (string, float, rgb, etc),
              different levels will be plotted in different colors in the order
              specified

              -  one string color, e.g. colors = 'r' or colors = 'red', all levels
              will be plotted in this color

              - if colors == None, the default colormap will be used

            * alpha=1.0 : the alpha blending value

            * cmap = None: a cm Colormap instance from matplotlib.cm.

            * origin = None: 'upper'|'lower'|'image'|None.
              If 'image', the rc value for image.origin will be used.
              If None (default), the first value of Z will correspond
              to the lower left corner, location (0,0).
              This keyword is active only if contourf is called with
              one or two arguments, that is, without explicitly
              specifying X and Y.

            * extent = None: (x0,x1,y0,y1); also active only if X and Y
              are not specified.

            * badmask = None: array with dimensions of Z, and with values
              of zero at locations corresponding to valid data, and one
              at locations where the value of Z should be ignored.
              This is experimental.  It presently works for edge regions
              for line and filled contours, but for interior regions it
              works correctly only for line contours.  The badmask kwarg
              may go away in the future, to be replaced by the use of
              NaN value in Z and/or the use of a masked array in Z.

            * linewidths = None: or one of these:
              - a number - all levels will be plotted with this linewidth,
                e.g. linewidths = 0.6

              - a tuple of numbers, e.g. linewidths = (0.4, 0.8, 1.2) different
                levels will be plotted with different linewidths in the order
                specified

              - if linewidths == None, the default width in lines.linewidth in
                .matplotlibrc is used

            * fmt = '1.3f': a format string for adding a label to each collection.
              Useful for auto-legending.

        """

        alpha = kwargs.get('alpha', 1.0)
        linewidths = kwargs.get('linewidths', None)
        fmt = kwargs.get('fmt', '%1.3f')
        origin = kwargs.get('origin', None)
        extent = kwargs.get('extent', None)
        cmap = kwargs.get('cmap', None)
        colors = kwargs.get('colors', None)
        badmask = kwargs.get('badmask', None)


        if cmap is not None: assert(isinstance(cmap, Colormap))
        if origin is not None: assert(origin in ['lower', 'upper', 'image'])
        if extent is not None: assert(len(extent) == 4)
        if colors is not None and cmap is not None:
            raise RuntimeError('Either colors or cmap must be None')
        # todo: shouldn't this use the current image rather than the rc param?
        if origin == 'image': origin = rcParams['image.origin']


        x, y, z, lev = self._contour_args(False, badmask, origin, extent, *args)

        # Manipulate the plot *after* checking the input arguments.
        if not self.ax.ishold(): self.ax.cla()

        Nlev = len(lev)
        if cmap is None:
            if colors is None:
                Ncolors = Nlev
            else:
                Ncolors = len(colors)
        else:
            Ncolors = Nlev


        reg, triangle = self._initialize_reg_tri(z, badmask)

        tcolors, mappable, collections = self._process_colors(
            z, colors, alpha, lev, cmap)

        if linewidths == None:
            tlinewidths = [rcParams['lines.linewidth']] *Nlev
        else:
            if iterable(linewidths) and len(linewidths) < Nlev:
                linewidths = list(linewidths) * int(ceil(Nlev/len(linewidths)))
            elif not iterable(linewidths) and type(linewidths) in [int, float]:
                linewidths = [linewidths] * Nlev
            tlinewidths = [(w,) for w in linewidths]

        region = 0
        for level, color, width in zip(lev, tcolors, tlinewidths):
            ntotal, nparts  = _contour.GcInit1(x, y, reg, triangle,
                                               region, z, level)
            np = zeros((nparts,), typecode='l')
            xp = zeros((ntotal, ), Float64)
            yp = zeros((ntotal,), Float64)
            nlist = _contour.GcTrace(np, xp, yp)
            col = LineCollection(nlist)
            col.set_color(color)
            col.set_linewidth(width)

            if level < 0.0 and Ncolors == 1:
                col.set_linestyle((0, (6.,6.)),)
                #print "setting dashed"
            col.set_label(fmt%level)
            self.ax.add_collection(col)
            collections.append(col)

        collections = silent_list('LineCollection', collections)
        # the mappable attr is for the pylab interface functions,
        # which maintain the current image
        collections.mappable = mappable
        return lev, collections
Esempio n. 36
0
    def contourf(self, *args, **kwargs):
        """
        contourf(self, *args, **kwargs)

        Function signatures

        contourf(Z) - make a filled contour plot of an array Z. The level
                 values are chosen automatically.

        contourf(X,Y,Z) - X,Y specify the (x,y) coordinates of the surface

        contourf(Z,N) and contourf(X,Y,Z,N) - make a filled contour plot
                 corresponding to N contour levels

        contourf(Z,V) and contourf(X,Y,Z,V) - fill len(V) regions,
                 between the levels specified in sequence V, and a final region
                 for values of Z greater than the last element in V

        contourf(Z, **kwargs) - Use keyword args to control colors,
                    origin, cmap ... see below

        [L,C] = contourf(...) returns a list of levels and a silent_list
             of PolyCollections

        Optional keywork args are shown with their defaults below (you must
        use kwargs for these):

            * colors = None: one of these:
              - a tuple of matplotlib color args (string, float, rgb, etc),
              different levels will be plotted in different colors in the order
              specified

              -  one string color, e.g. colors = 'r' or colors = 'red', all levels
              will be plotted in this color

              - if colors == None, the default colormap will be used

            * alpha=1.0 : the alpha blending value

            * cmap = None: a cm Colormap instance from matplotlib.cm.

            * origin = None: 'upper'|'lower'|'image'|None.
              If 'image', the rc value for image.origin will be used.
              If None (default), the first value of Z will correspond
              to the lower left corner, location (0,0).
              This keyword is active only if contourf is called with
              one or two arguments, that is, without explicitly
              specifying X and Y.

            * badmask = None: array with dimensions of Z, and with values
              of zero at locations corresponding to valid data, and one
              at locations where the value of Z should be ignored.
              This is experimental.  It presently works for edge regions
              for line and filled contours, but for interior regions it
              works correctly only for line contours.  The badmask kwarg
              may go away in the future, to be replaced by the use of
              NaN value in Z and/or the use of a masked array in Z.

            reg is a 1D region number array with of imax*(jmax+1)+1 size
            The values of reg should be positive region numbers, and zero fro
            zones wich do not exist.

            triangle - triangulation array - must be the same shape as reg

            contourf differs from the Matlab (TM) version in that it does not
                draw the polygon edges (because the contouring engine yields
                simply connected regions with branch cuts.)  To draw the edges,
                add line contours with calls to contour.

        """

        alpha = kwargs.get('alpha', 1.0)
        origin = kwargs.get('origin', None)
        extent = kwargs.get('extent', None)
        cmap = kwargs.get('cmap', None)
        colors = kwargs.get('colors', None)
        badmask = kwargs.get('badmask', None)

        if cmap is not None: assert(isinstance(cmap, Colormap))
        if origin is not None: assert(origin in ['lower', 'upper', 'image'])

        if colors is not None and cmap is not None:
            raise RuntimeError('Either colors or cmap must be None')
        if origin == 'image': origin = rcParams['image.origin']

        x, y, z, lev = self._contour_args(True, badmask, origin, extent, *args)
        # Manipulate the plot *after* checking the input arguments.
        if not self.ax.ishold(): self.ax.cla()

        Nlev = len(lev)


        reg, triangle = self._initialize_reg_tri(z, badmask)

        tcolors, mappable, collections = self._process_colors(z, colors,
                                                               alpha,
                                                               lev, cmap)

        region = 0
        lev_upper = list(lev[1:])
        lev_upper.append(1e38)
        for level, level_upper, color in zip(lev, lev_upper, tcolors):
            levs = (level, level_upper)
            ntotal, nparts  = _contour.GcInit2(x, y, reg, triangle,
                                               region, z, levs, 30)
            np = zeros((nparts,), typecode='l')
            xp = zeros((ntotal, ), Float64)
            yp = zeros((ntotal,), Float64)
            nlist = _contour.GcTrace(np, xp, yp)
            col = PolyCollection(nlist,
                                         linewidths=(1,))
                  # linewidths = 1 is necessary to avoid artifacts
                  # in rendering the region boundaries.
            col.set_color(color) # sets both facecolor and edgecolor
            self.ax.add_collection(col)
            collections.append(col)

        collections = silent_list('PolyCollection', collections)
        collections.mappable = mappable
        return lev, collections
Esempio n. 37
0
def cohere_pairs( X, ij, NFFT=256, Fs=2, detrend=detrend_none,
                  window=window_hanning, noverlap=0,
                  preferSpeedOverMemory=True,
                  progressCallback=donothing_callback,
                  returnPxx=False):

    """
    Cxy, Phase, freqs = cohere_pairs( X, ij, ...)
    
    Compute the coherence for all pairs in ij.  X is a
    numSamples,numCols Numeric array.  ij is a list of tuples (i,j).
    Each tuple is a pair of indexes into the columns of X for which
    you want to compute coherence.  For example, if X has 64 columns,
    and you want to compute all nonredundant pairs, define ij as

      ij = []
      for i in range(64):
          for j in range(i+1,64):
              ij.append( (i,j) )

    The other function arguments, except for 'preferSpeedOverMemory'
    (see below), are explained in the help string of 'psd'.

    Return value is a tuple (Cxy, Phase, freqs).

      Cxy -- a dictionary of (i,j) tuples -> coherence vector for that
        pair.  Ie, Cxy[(i,j) = cohere(X[:,i], X[:,j]).  Number of
        dictionary keys is len(ij)
      
      Phase -- a dictionary of phases of the cross spectral density at
        each frequency for each pair.  keys are (i,j).

      freqs -- a vector of frequencies, equal in length to either the
        coherence or phase vectors for any i,j key.  Eg, to make a coherence
        Bode plot:

          subplot(211)
          plot( freqs, Cxy[(12,19)])
          subplot(212)
          plot( freqs, Phase[(12,19)])
      
    For a large number of pairs, cohere_pairs can be much more
    efficient than just calling cohere for each pair, because it
    caches most of the intensive computations.  If N is the number of
    pairs, this function is O(N) for most of the heavy lifting,
    whereas calling cohere for each pair is O(N^2).  However, because
    of the caching, it is also more memory intensive, making 2
    additional complex arrays with approximately the same number of
    elements as X.

    The parameter 'preferSpeedOverMemory', if false, limits the
    caching by only making one, rather than two, complex cache arrays.
    This is useful if memory becomes critical.  Even when
    preferSpeedOverMemory is false, cohere_pairs will still give
    significant performace gains over calling cohere for each pair,
    and will use subtantially less memory than if
    preferSpeedOverMemory is true.  In my tests with a 43000,64 array
    over all nonredundant pairs, preferSpeedOverMemory=1 delivered a
    33% performace boost on a 1.7GHZ Athlon with 512MB RAM compared
    with preferSpeedOverMemory=0.  But both solutions were more than
    10x faster than naievly crunching all possible pairs through
    cohere.

    See test/cohere_pairs_test.py in the src tree for an example
    script that shows that this cohere_pairs and cohere give the same
    results for a given pair.

    """
    numRows, numCols = X.shape

    # zero pad if X is too short
    if numRows < NFFT:
        tmp = X
        X = zeros( (NFFT, numCols), X.typecode())
        X[:numRows,:] = tmp
        del tmp

    numRows, numCols = X.shape
    # get all the columns of X that we are interested in by checking
    # the ij tuples
    seen = {}
    for i,j in ij:
        seen[i]=1; seen[j] = 1
    allColumns = seen.keys()
    Ncols = len(allColumns)
    del seen
    
    # for real X, ignore the negative frequencies
    if X.typecode()==Complex: numFreqs = NFFT
    else: numFreqs = NFFT//2+1

    # cache the FFT of every windowed, detrended NFFT length segement
    # of every channel.  If preferSpeedOverMemory, cache the conjugate
    # as well
    windowVals = window(ones((NFFT,), X.typecode()))
    ind = range(0, numRows-NFFT+1, NFFT-noverlap)
    numSlices = len(ind)
    FFTSlices = {}
    FFTConjSlices = {}
    Pxx = {}
    slices = range(numSlices)
    normVal = norm(windowVals)**2
    for iCol in allColumns:
        progressCallback(i/Ncols, 'Cacheing FFTs')
        Slices = zeros( (numSlices,numFreqs), Complex)
        for iSlice in slices:                    
            thisSlice = X[ind[iSlice]:ind[iSlice]+NFFT, iCol]
            thisSlice = windowVals*detrend(thisSlice)
            Slices[iSlice,:] = fft(thisSlice)[:numFreqs]
            
        FFTSlices[iCol] = Slices
        if preferSpeedOverMemory:
            FFTConjSlices[iCol] = conjugate(Slices)
        Pxx[iCol] = divide(mean(absolute(Slices)**2), normVal)
    del Slices, ind, windowVals    

    # compute the coherences and phases for all pairs using the
    # cached FFTs
    Cxy = {}
    Phase = {}
    count = 0
    N = len(ij)
    for i,j in ij:
        count +=1
        if count%10==0:
            progressCallback(count/N, 'Computing coherences')

        if preferSpeedOverMemory:
            Pxy = FFTSlices[i] * FFTConjSlices[j]
        else:
            Pxy = FFTSlices[i] * conjugate(FFTSlices[j])
        if numSlices>1: Pxy = mean(Pxy)
        Pxy = divide(Pxy, normVal)
        Cxy[(i,j)] = divide(absolute(Pxy)**2, Pxx[i]*Pxx[j])
        Phase[(i,j)] =  arctan2(Pxy.imag, Pxy.real)

    freqs = Fs/NFFT*arange(numFreqs)
    if returnPxx:
       return Cxy, Phase, freqs, Pxx
    else:
       return Cxy, Phase, freqs
Esempio n. 38
0
def csd(x, y, NFFT=256, Fs=2, detrend=detrend_none,
        window=window_hanning, noverlap=0):
    """
    The cross spectral density Pxy by Welches average periodogram
    method.  The vectors x and y are divided into NFFT length
    segments.  Each segment is detrended by function detrend and
    windowed by function window.  noverlap gives the length of the
    overlap between segments.  The product of the direct FFTs of x and
    y are averaged over each segment to compute Pxy, with a scaling to
    correct for power loss due to windowing.  Fs is the sampling
    frequency.

    NFFT must be a power of 2

    Returns the tuple Pxy, freqs

    

    Refs:
      Bendat & Piersol -- Random Data: Analysis and Measurement
        Procedures, John Wiley & Sons (1986)

    """

    if NFFT % 2:
        raise ValueError, 'NFFT must be a power of 2'

    # zero pad x and y up to NFFT if they are shorter than NFFT
    if len(x)<NFFT:
        n = len(x)
        x = resize(x, (NFFT,))
        x[n:] = 0
    if len(y)<NFFT:
        n = len(y)
        y = resize(y, (NFFT,))
        y[n:] = 0

    # for real x, ignore the negative frequencies
    if x.typecode()==Complex: numFreqs = NFFT
    else: numFreqs = NFFT//2+1
        
    windowVals = window(ones((NFFT,),x.typecode()))
    step = NFFT-noverlap
    ind = range(0,len(x)-NFFT+1,step)
    n = len(ind)
    Pxy = zeros((numFreqs,n), Complex)

    # do the ffts of the slices
    for i in range(n):
        thisX = x[ind[i]:ind[i]+NFFT]
        thisX = windowVals*detrend(thisX)
        thisY = y[ind[i]:ind[i]+NFFT]
        thisY = windowVals*detrend(thisY)
        fx = fft(thisX)
        fy = fft(thisY)
        Pxy[:,i] = conjugate(fx[:numFreqs])*fy[:numFreqs]



    # Scale the spectrum by the norm of the window to compensate for
    # windowing loss; see Bendat & Piersol Sec 11.5.2
    if n>1: Pxy = mean(Pxy,1)
    Pxy = divide(Pxy, norm(windowVals)**2)
    freqs = Fs/NFFT*arange(numFreqs)
    Pxy.shape = len(freqs),
    return Pxy, freqs
Esempio n. 39
0
def cohere_pairs(X,
                 ij,
                 NFFT=256,
                 Fs=2,
                 detrend=detrend_none,
                 window=window_hanning,
                 noverlap=0,
                 preferSpeedOverMemory=True,
                 progressCallback=donothing_callback,
                 returnPxx=False):
    """
    Cxy, Phase, freqs = cohere_pairs( X, ij, ...)
    
    Compute the coherence for all pairs in ij.  X is a
    numSamples,numCols Numeric array.  ij is a list of tuples (i,j).
    Each tuple is a pair of indexes into the columns of X for which
    you want to compute coherence.  For example, if X has 64 columns,
    and you want to compute all nonredundant pairs, define ij as

      ij = []
      for i in range(64):
          for j in range(i+1,64):
              ij.append( (i,j) )

    The other function arguments, except for 'preferSpeedOverMemory'
    (see below), are explained in the help string of 'psd'.

    Return value is a tuple (Cxy, Phase, freqs).

      Cxy -- a dictionary of (i,j) tuples -> coherence vector for that
        pair.  Ie, Cxy[(i,j) = cohere(X[:,i], X[:,j]).  Number of
        dictionary keys is len(ij)
      
      Phase -- a dictionary of phases of the cross spectral density at
        each frequency for each pair.  keys are (i,j).

      freqs -- a vector of frequencies, equal in length to either the
        coherence or phase vectors for any i,j key.  Eg, to make a coherence
        Bode plot:

          subplot(211)
          plot( freqs, Cxy[(12,19)])
          subplot(212)
          plot( freqs, Phase[(12,19)])
      
    For a large number of pairs, cohere_pairs can be much more
    efficient than just calling cohere for each pair, because it
    caches most of the intensive computations.  If N is the number of
    pairs, this function is O(N) for most of the heavy lifting,
    whereas calling cohere for each pair is O(N^2).  However, because
    of the caching, it is also more memory intensive, making 2
    additional complex arrays with approximately the same number of
    elements as X.

    The parameter 'preferSpeedOverMemory', if false, limits the
    caching by only making one, rather than two, complex cache arrays.
    This is useful if memory becomes critical.  Even when
    preferSpeedOverMemory is false, cohere_pairs will still give
    significant performace gains over calling cohere for each pair,
    and will use subtantially less memory than if
    preferSpeedOverMemory is true.  In my tests with a 43000,64 array
    over all nonredundant pairs, preferSpeedOverMemory=1 delivered a
    33% performace boost on a 1.7GHZ Athlon with 512MB RAM compared
    with preferSpeedOverMemory=0.  But both solutions were more than
    10x faster than naievly crunching all possible pairs through
    cohere.

    See test/cohere_pairs_test.py in the src tree for an example
    script that shows that this cohere_pairs and cohere give the same
    results for a given pair.

    """
    numRows, numCols = X.shape

    # zero pad if X is too short
    if numRows < NFFT:
        tmp = X
        X = zeros((NFFT, numCols), typecode(X))
        X[:numRows, :] = tmp
        del tmp

    numRows, numCols = X.shape
    # get all the columns of X that we are interested in by checking
    # the ij tuples
    seen = {}
    for i, j in ij:
        seen[i] = 1
        seen[j] = 1
    allColumns = seen.keys()
    Ncols = len(allColumns)
    del seen

    # for real X, ignore the negative frequencies
    if typecode(X) == Complex: numFreqs = NFFT
    else: numFreqs = NFFT // 2 + 1

    # cache the FFT of every windowed, detrended NFFT length segement
    # of every channel.  If preferSpeedOverMemory, cache the conjugate
    # as well
    windowVals = window(ones((NFFT, ), typecode(X)))
    ind = range(0, numRows - NFFT + 1, NFFT - noverlap)
    numSlices = len(ind)
    FFTSlices = {}
    FFTConjSlices = {}
    Pxx = {}
    slices = range(numSlices)
    normVal = norm(windowVals)**2
    for iCol in allColumns:
        progressCallback(i / Ncols, 'Cacheing FFTs')
        Slices = zeros((numSlices, numFreqs), Complex)
        for iSlice in slices:
            thisSlice = X[ind[iSlice]:ind[iSlice] + NFFT, iCol]
            thisSlice = windowVals * detrend(thisSlice)
            Slices[iSlice, :] = fft(thisSlice)[:numFreqs]

        FFTSlices[iCol] = Slices
        if preferSpeedOverMemory:
            FFTConjSlices[iCol] = conjugate(Slices)
        Pxx[iCol] = divide(mean(absolute(Slices)**2), normVal)
    del Slices, ind, windowVals

    # compute the coherences and phases for all pairs using the
    # cached FFTs
    Cxy = {}
    Phase = {}
    count = 0
    N = len(ij)
    for i, j in ij:
        count += 1
        if count % 10 == 0:
            progressCallback(count / N, 'Computing coherences')

        if preferSpeedOverMemory:
            Pxy = FFTSlices[i] * FFTConjSlices[j]
        else:
            Pxy = FFTSlices[i] * conjugate(FFTSlices[j])
        if numSlices > 1: Pxy = mean(Pxy)
        Pxy = divide(Pxy, normVal)
        Cxy[(i, j)] = divide(absolute(Pxy)**2, Pxx[i] * Pxx[j])
        Phase[(i, j)] = arctan2(Pxy.imag, Pxy.real)

    freqs = Fs / NFFT * arange(numFreqs)
    if returnPxx:
        return Cxy, Phase, freqs, Pxx
    else:
        return Cxy, Phase, freqs
Esempio n. 40
0
def csd(x,
        y,
        NFFT=256,
        Fs=2,
        detrend=detrend_none,
        window=window_hanning,
        noverlap=0):
    """
    The cross spectral density Pxy by Welches average periodogram
    method.  The vectors x and y are divided into NFFT length
    segments.  Each segment is detrended by function detrend and
    windowed by function window.  noverlap gives the length of the
    overlap between segments.  The product of the direct FFTs of x and
    y are averaged over each segment to compute Pxy, with a scaling to
    correct for power loss due to windowing.  Fs is the sampling
    frequency.

    NFFT must be a power of 2

    Returns the tuple Pxy, freqs

    

    Refs:
      Bendat & Piersol -- Random Data: Analysis and Measurement
        Procedures, John Wiley & Sons (1986)

    """

    if NFFT % 2:
        raise ValueError, 'NFFT must be a power of 2'

    # zero pad x and y up to NFFT if they are shorter than NFFT
    if len(x) < NFFT:
        n = len(x)
        x = resize(x, (NFFT, ))
        x[n:] = 0
    if len(y) < NFFT:
        n = len(y)
        y = resize(y, (NFFT, ))
        y[n:] = 0

    # for real x, ignore the negative frequencies
    if typecode(x) == Complex: numFreqs = NFFT
    else: numFreqs = NFFT // 2 + 1

    windowVals = window(ones((NFFT, ), typecode(x)))
    step = NFFT - noverlap
    ind = range(0, len(x) - NFFT + 1, step)
    n = len(ind)
    Pxy = zeros((numFreqs, n), Complex)

    # do the ffts of the slices
    for i in range(n):
        thisX = x[ind[i]:ind[i] + NFFT]
        thisX = windowVals * detrend(thisX)
        thisY = y[ind[i]:ind[i] + NFFT]
        thisY = windowVals * detrend(thisY)
        fx = fft(thisX)
        fy = fft(thisY)
        Pxy[:, i] = conjugate(fx[:numFreqs]) * fy[:numFreqs]

    # Scale the spectrum by the norm of the window to compensate for
    # windowing loss; see Bendat & Piersol Sec 11.5.2
    if n > 1: Pxy = mean(Pxy, 1)
    Pxy = divide(Pxy, norm(windowVals)**2)
    freqs = Fs / NFFT * arange(numFreqs)
    Pxy.shape = len(freqs),
    return Pxy, freqs
Esempio n. 41
0
def zeros_like(a):
    """Return an array of zeros of the shape and typecode of a."""

    return zeros(a.shape,a.typecode())