Beispiel #1
0
def _correlate_and_convolve_body(vector, filter, d, mode):
    """ The body of correlate() and convolve() are identical"""

    # Anything to do?
    if vector.size <= 0:
        return vector.copy()

    # Complex correlate includes a conjugation
    if numpy.iscomplexobj(filter):
        filter = numpy.conj(filter)

    dtype = numpy.result_type(vector, filter)
    rows = int(
        ufuncs.ceil((vector.size + 2 * filter.size - 2) / float(filter.size)))
    padded = array_create.empty([rows * filter.size], dtype=dtype)
    padded[0:filter.size - 1] = 0
    padded[filter.size - 1:vector.size + filter.size - 1] = vector
    padded[vector.size + filter.size - 1:] = 0
    s = numpy_backport.as_strided(padded,
                                  shape=(padded.shape[0] - filter.size + 1,
                                         filter.size),
                                  strides=(padded.strides[0],
                                           padded.strides[0]))
    result = linalg.dot(s, filter)
    if mode == 'same':
        return result[d:vector.size + d]
    elif mode == 'full':
        return result[0:vector.size + filter.size - 1]
    elif mode == 'valid':
        return result[filter.size - 1:vector.size]
    else:
        raise ValueError("correlate1d: invalid mode '%s'" % mode)
Beispiel #2
0
def _correlate_and_convolve_body(vector, filter, d, mode):
    """ The body of correlate() and convolve() are identical"""

    # Anything to do?
    if vector.size <= 0:
        return vector.copy()

    # Complex correlate includes a conjugation
    if numpy.iscomplexobj(filter):
        filter = numpy.conj(filter)

    dtype = numpy.result_type(vector, filter)
    rows = int(ufuncs.ceil((vector.size + 2 * filter.size - 2) / float(filter.size)))
    padded = array_create.empty([rows * filter.size], dtype=dtype)
    padded[0:filter.size - 1] = 0
    padded[filter.size - 1:vector.size + filter.size - 1] = vector
    padded[vector.size + filter.size - 1:] = 0
    s = numpy_backport.as_strided(padded, shape=(padded.shape[0] - filter.size + 1, filter.size),
                                  strides=(padded.strides[0], padded.strides[0]))
    result = linalg.dot(s, filter)
    if mode == 'same':
        return result[d:vector.size + d]
    elif mode == 'full':
        return result[0:vector.size + filter.size - 1]
    elif mode == 'valid':
        return result[filter.size - 1:vector.size]
    else:
        raise ValueError("correlate1d: invalid mode '%s'" % mode)
Beispiel #3
0
def _correlate_kernel(Array, Filter, mode):
    # Anything to do?
    if Array.size <= 0:
        return Array.copy()

    # Complex correlate includes a conjugation
    if numpy.iscomplexobj(Filter):
        Filter = numpy.conj(Filter)

    # Get sizes as arrays for easier manipulation
    ArrSize = numpy.array(Array.shape, dtype=numpy.int32)
    FilterSize = numpy.array(Filter.shape, dtype=numpy.int32)

    # Check that mode='valid' is allowed given the array sizes
    if mode == 'valid':
        diffSize = ArrSize[:FilterSize.size] - FilterSize
        nSmaller = summations.sum(diffSize < 0)
        if nSmaller > 0:
            raise ValueError(
                "correlateNd: For 'valid' mode, one must be at least as large as the other in every dimension")

    # Use numpy convention for result dype
    dtype = numpy.result_type(Array, Filter)

    # Add zeros along relevant dimensions
    Padded = _addZerosNd(Array, FilterSize, dtype)
    PaddedSize = numpy.array(Padded.shape, dtype=numpy.int32)

    # Get positions of first view
    IndiVec = _findIndices(PaddedSize, FilterSize)
    CenterPos = tuple((FilterSize - 1) // 2)
    IndiMat = IndiVec.reshape(FilterSize, order='F')
    nPre = IndiMat[CenterPos]  # Required zeros before Array for correct alignment
    nPost = IndiVec[Filter.size - 1] - nPre
    n = Padded.size
    nTot = n + nPre + nPost  # Total size after pre/post padding
    V = array_create.empty([nTot], dtype=dtype, bohrium=bhary.check(Array))
    V[nPre:n + nPre] = Padded.flatten(order='F')
    V[:nPre] = 0
    V[n + nPre:] = 0
    A = Filter.flatten(order='F')

    # Actual correlation calculation
    Correlated = V[IndiVec[0]:n + IndiVec[0]] * A[0]
    for i in range(1, Filter.size):
        Correlated += V[IndiVec[i]:n + IndiVec[i]] * A[i]
        # TODO: we need this flush because of very slow fusion
        if bhary.check(V):
            _bh.flush()

    Full = Correlated.reshape(PaddedSize, order='F')
    if mode == 'full':
        return Full
    elif mode == 'same':
        return _findSame(Full, FilterSize)
    elif mode == 'valid':
        return _findValid(Full, FilterSize)
    else:
        raise ValueError("correlateNd: invalid mode '%s'" % mode)
Beispiel #4
0
def _correlate_kernel(Array, Filter, mode):
    # Anything to do?
    if Array.size <= 0:
        return Array.copy()

    # Complex correlate includes a conjugation
    if numpy.iscomplexobj(Filter):
        Filter = numpy.conj(Filter)

    # Get sizes as arrays for easier manipulation
    ArrSize = array_create.array(Array.shape, dtype=numpy.int32)
    FilterSize = array_create.array(Filter.shape, dtype=numpy.int32)

    # Check that mode='valid' is allowed given the array sizes
    if mode == 'valid':
        diffSize = ArrSize[:FilterSize.size] - FilterSize
        nSmaller = summations.sum(diffSize < 0)
        if nSmaller > 0:
            raise ValueError(
                "correlateNd: For 'valid' mode, one must be at least as large as the other in every dimension"
            )

    # Use numpy convention for result dype
    dtype = numpy.result_type(Array, Filter)

    # Add zeros along relevant dimensions
    Padded = _addZerosNd(Array, FilterSize, dtype)
    PaddedSize = array_create.array(Padded.shape, dtype=numpy.int32)

    # Get positions of first view
    IndiVec = _findIndices(PaddedSize, FilterSize)
    CenterPos = tuple((FilterSize - 1) // 2)
    IndiMat = IndiVec.reshape(FilterSize, order='F')
    nPre = IndiMat[
        CenterPos]  # Required zeros before Array for correct alignment
    nPost = IndiVec[Filter.size - 1] - nPre
    n = Padded.size
    nTot = n + nPre + nPost  # Total size after pre/post padding
    V = array_create.empty([nTot], dtype=dtype)
    V[nPre:n + nPre] = Padded.flatten(order='F')
    V[:nPre] = 0
    V[n + nPre:] = 0
    A = Filter.flatten(order='F')

    # Actual correlation calculation
    Correlated = V[IndiVec[0]:n + IndiVec[0]] * A[0]
    for i in range(1, Filter.size):
        Correlated += V[IndiVec[i]:n + IndiVec[i]] * A[i]

    Full = Correlated.reshape(PaddedSize, order='F')
    if mode == 'full':
        return Full
    elif mode == 'same':
        return _findSame(Full, FilterSize)
    elif mode == 'valid':
        return _findValid(Full, FilterSize)
    else:
        raise ValueError("correlateNd: invalid mode '%s'" % mode)