예제 #1
0
def convolve(in1, in2, mode='full'):
    """Convolve two N-dimensional arrays.

  Description:

     Convolve in1 and in2 with output size determined by mode.

  Inputs:

    in1 -- an N-dimensional array.
    in2 -- an array with the same number of dimensions as in1.
    mode -- a flag indicating the size of the output
            'valid'  (0): The output consists only of those elements that
                            are computed by scaling the larger array with all
                            the values of the smaller array.
            'same'   (1): The output is the same size as the largest input
                            centered with respect to the 'full' output.
            'full'   (2): The output is the full discrete linear convolution
                            of the inputs. (Default)

  Outputs:  (out,)

    out -- an N-dimensional array containing a subset of the discrete linear
           convolution of in1 with in2.

    """
    volume = asarray(in1)
    kernel = asarray(in2)
    if rank(volume) == rank(kernel) == 0:
        return volume * kernel
    if (product(kernel.shape, axis=0) > product(volume.shape, axis=0)):
        temp = kernel
        kernel = volume
        volume = temp
        del temp

    slice_obj = [slice(None, None, -1)] * len(kernel.shape)
    val = _valfrommode(mode)

    return sigtools._correlateND(volume, kernel[slice_obj], val)
예제 #2
0
def convolve(in1, in2, mode='full'):
    """Convolve two N-dimensional arrays.

  Description:

     Convolve in1 and in2 with output size determined by mode.

  Inputs:

    in1 -- an N-dimensional array.
    in2 -- an array with the same number of dimensions as in1.
    mode -- a flag indicating the size of the output
            'valid'  (0): The output consists only of those elements that
                            are computed by scaling the larger array with all
                            the values of the smaller array.
            'same'   (1): The output is the same size as the largest input
                            centered with respect to the 'full' output.
            'full'   (2): The output is the full discrete linear convolution
                            of the inputs. (Default)

  Outputs:  (out,)

    out -- an N-dimensional array containing a subset of the discrete linear
           convolution of in1 with in2.

    """
    volume = asarray(in1)
    kernel = asarray(in2)
    if rank(volume) == rank(kernel) == 0:
        return volume*kernel
    if (product(kernel.shape,axis=0) > product(volume.shape,axis=0)):
        temp = kernel
        kernel = volume
        volume = temp
        del temp

    slice_obj = [slice(None,None,-1)]*len(kernel.shape)
    val = _valfrommode(mode)

    return sigtools._correlateND(volume,kernel[slice_obj],val)
예제 #3
0
def correlate(in1, in2, mode='full'):
    """Cross-correlate two N-dimensional arrays.

  Description:

     Cross-correlate in1 and in2 with the output size determined by mode.

  Inputs:

    in1 -- an N-dimensional array.
    in2 -- an array with the same number of dimensions as in1.
    mode -- a flag indicating the size of the output
            'valid'  (0): The output consists only of those elements that
                            do not rely on the zero-padding.
            'same'   (1): The output is the same size as the largest input
                            centered with respect to the 'full' output.
            'full'   (2): The output is the full discrete linear
                            cross-correlation of the inputs. (Default)

  Outputs:  (out,)

    out -- an N-dimensional array containing a subset of the discrete linear
           cross-correlation of in1 with in2.

    """
    # Code is faster if kernel is smallest array.
    volume = asarray(in1)
    kernel = asarray(in2)
    if rank(volume) == rank(kernel) == 0:
        return volume*kernel
    if (product(kernel.shape,axis=0) > product(volume.shape,axis=0)):
        temp = kernel
        kernel = volume
        volume = temp
        del temp

    val = _valfrommode(mode)

    return sigtools._correlateND(volume, kernel, val)
예제 #4
0
def correlate(in1, in2, mode='full'):
    """Cross-correlate two N-dimensional arrays.

  Description:

     Cross-correlate in1 and in2 with the output size determined by mode.

  Inputs:

    in1 -- an N-dimensional array.
    in2 -- an array with the same number of dimensions as in1.
    mode -- a flag indicating the size of the output
            'valid'  (0): The output consists only of those elements that
                            do not rely on the zero-padding.
            'same'   (1): The output is the same size as the largest input
                            centered with respect to the 'full' output.
            'full'   (2): The output is the full discrete linear
                            cross-correlation of the inputs. (Default)

  Outputs:  (out,)

    out -- an N-dimensional array containing a subset of the discrete linear
           cross-correlation of in1 with in2.

    """
    # Code is faster if kernel is smallest array.
    volume = asarray(in1)
    kernel = asarray(in2)
    if rank(volume) == rank(kernel) == 0:
        return volume * kernel
    if (product(kernel.shape, axis=0) > product(volume.shape, axis=0)):
        temp = kernel
        kernel = volume
        volume = temp
        del temp

    val = _valfrommode(mode)

    return sigtools._correlateND(volume, kernel, val)
예제 #5
0
def correlate(in1, in2, mode='full'):
    """
    Cross-correlate two N-dimensional arrays.

    Cross-correlate in1 and in2 with the output size determined by the mode
    argument.

    Parameters
    ----------
    in1: array
        first input.
    in2: array
        second input. Should have the same number of dimensions as in1.
    mode: str {'valid', 'same', 'full'}, optional
        A string indicating the size of the output:

            - 'valid': the output consists only of those elements that do not
              rely on the zero-padding.
            - 'same': the output is the same size as ``in1`` centered
              with respect to the 'full' output.
            - 'full': the output is the full discrete linear cross-correlation
              of the inputs (default).

    Returns
    -------
    out: array
        an N-dimensional array containing a subset of the discrete linear
        cross-correlation of in1 with in2.

    Notes
    -----
    The correlation z of two arrays x and y of rank d is defined as::

      z[...,k,...] = sum[..., i_l, ...]
            x[..., i_l,...] * conj(y[..., i_l + k,...])

    """
    val = _valfrommode(mode)

    if mode == 'valid':
        ps = [i - j + 1 for i, j in zip(in1.shape, in2.shape)]
        out = np.empty(ps, in1.dtype)
        for i in range(len(ps)):
            if ps[i] <= 0:
                raise ValueError("Dimension of x(%d) < y(%d) " \
                                 "not compatible with valid mode" % \
                                 (in1.shape[i], in2.shape[i]))

        z = sigtools._correlateND(in1, in2, out, val)
    else:
        ps = [i + j - 1 for i, j in zip(in1.shape, in2.shape)]
        # zero pad input
        in1zpadded = np.zeros(ps, in1.dtype)
        sc = [slice(0, i) for i in in1.shape]
        in1zpadded[sc] = in1.copy()

        if mode == 'full':
            out = np.empty(ps, in1.dtype)
            z = sigtools._correlateND(in1zpadded, in2, out, val)
        elif mode == 'same':
            out = np.empty(in1.shape, in1.dtype)

            z = sigtools._correlateND(in1zpadded, in2, out, val)
        else:
            raise ValueError("Uknown mode %s" % mode)

    return z
예제 #6
0
def correlate(in1, in2, mode='full'):
    """
    Cross-correlate two N-dimensional arrays.

    Cross-correlate in1 and in2 with the output size determined by the mode
    argument.

    Parameters
    ----------
    in1: array
        first input.
    in2: array
        second input. Should have the same number of dimensions as in1.
    mode: str {'valid', 'same', 'full'}
        a string indicating the size of the output:
            - 'valid': the output consists only of those elements that do not
            rely on the zero-padding.
            - 'same': the output is the same size as the largest input centered
              with respect to the 'full' output.
            - 'full': the output is the full discrete linear cross-correlation
              of the inputs. (Default)

    Returns
    -------
    out: array
        an N-dimensional array containing a subset of the discrete linear
        cross-correlation of in1 with in2.

    Notes
    -----
    The correlation z of two arrays x and y of rank d is defined as

      z[...,k,...] = sum[..., i_l, ...]
            x[..., i_l,...] * conj(y[..., i_l + k,...])

    """
    val = _valfrommode(mode)

    if mode == 'valid':
        ps = [i - j + 1 for i, j in zip(in1.shape, in2.shape)]
        out = np.empty(ps, in1.dtype)
        for i in range(len(ps)):
            if ps[i] <= 0:
                raise ValueError("Dimension of x(%d) < y(%d) " \
                                 "not compatible with valid mode" % \
                                 (in1.shape[i], in2.shape[i]))

        z = sigtools._correlateND(in1, in2, out, val)
    else:
        ps = [i + j - 1 for i, j in zip(in1.shape, in2.shape)]
        # zero pad input
        in1zpadded = np.zeros(ps, in1.dtype)
        sc = [slice(0, i) for i in in1.shape]
        in1zpadded[sc] = in1.copy()

        if mode == 'full':
            out = np.empty(ps, in1.dtype)
            z = sigtools._correlateND(in1zpadded, in2, out, val)
        elif mode == 'same':
            out = np.empty(in1.shape, in1.dtype)

            z = sigtools._correlateND(in1zpadded, in2, out, val)
        else:
            raise ValueError("Uknown mode %s" % mode)

    return z
예제 #7
0
def correlate(in1, in2, mode='full', old_behavior=True):
    """
    Cross-correlate two N-dimensional arrays.

    Cross-correlate in1 and in2 with the output size determined by the mode
    argument.

    Parameters
    ----------
    in1: array
        first input.
    in2: array
        second input. Should have the same number of dimensions as in1.
    mode: str {'valid', 'same', 'full'}
        a string indicating the size of the output:
            - 'valid': the output consists only of those elements that do not
            rely on the zero-padding.
            - 'same': the output is the same size as the largest input centered
              with respect to the 'full' output.
            - 'full': the output is the full discrete linear cross-correlation
              of the inputs. (Default)
    old_behavior: bool
        If True (default), the old behavior of correlate is implemented:
            - if in1.size < in2.size, in1 and in2 are swapped (correlate(in1,
              in2) == correlate(in2, in1))
            - For complex inputs, the conjugate is not taken for in2
        If False, the new, conventional definition of correlate is implemented.

    Returns
    -------
    out: array
        an N-dimensional array containing a subset of the discrete linear
        cross-correlation of in1 with in2.

    Notes
    -----
    The correlation z of two arrays x and y of rank d is defined as

      z[...,k,...] = sum[..., i_l, ...]
            x[..., i_l,...] * conj(y[..., i_l + k,...])

    """
    val = _valfrommode(mode)

    if old_behavior:
        warnings.warn(DeprecationWarning(_SWAP_INPUTS_DEPRECATION_MSG))
        if np.iscomplexobj(in2):
            in2 = in2.conjugate()
        if in1.size < in2.size:
            swp = in2
            in2 = in1
            in1 = swp

    if mode == 'valid':
        ps = [i - j + 1 for i, j in zip(in1.shape, in2.shape)]
        out = np.empty(ps, in1.dtype)
        for i in range(len(ps)):
            if ps[i] <= 0:
                raise ValueError("Dimension of x(%d) < y(%d) " \
                                 "not compatible with valid mode" % \
                                 (in1.shape[i], in2.shape[i]))

        z = sigtools._correlateND(in1, in2, out, val)
    else:
        ps = [i + j - 1 for i, j in zip(in1.shape, in2.shape)]
        # zero pad input
        in1zpadded = np.zeros(ps, in1.dtype)
        sc = [slice(0, i) for i in in1.shape]
        in1zpadded[sc] = in1.copy()

        if mode == 'full':
            out = np.empty(ps, in1.dtype)
            z = sigtools._correlateND(in1zpadded, in2, out, val)
        elif mode == 'same':
            out = np.empty(in1.shape, in1.dtype)

            z = sigtools._correlateND(in1zpadded, in2, out, val)
        else:
            raise ValueError("Uknown mode %s" % mode)

    return z
예제 #8
0
파일: signaltools.py 프로젝트: minrk/scipy
def correlate(in1, in2, mode='full', old_behavior=True):
    """
    Cross-correlate two N-dimensional arrays.

    Cross-correlate in1 and in2 with the output size determined by the mode
    argument.

    Parameters
    ----------
    in1: array
        first input.
    in2: array
        second input. Should have the same number of dimensions as in1.
    mode: str {'valid', 'same', 'full'}
        a string indicating the size of the output:
            - 'valid': the output consists only of those elements that do not
            rely on the zero-padding.
            - 'same': the output is the same size as the largest input centered
              with respect to the 'full' output.
            - 'full': the output is the full discrete linear cross-correlation
              of the inputs. (Default)
    old_behavior: bool
        If True (default), the old behavior of correlate is implemented:
            - if in1.size < in2.size, in1 and in2 are swapped (correlate(in1,
              in2) == correlate(in2, in1))
            - For complex inputs, the conjugate is not taken for in2
        If False, the new, conventional definition of correlate is implemented.

    Returns
    -------
    out: array
        an N-dimensional array containing a subset of the discrete linear
        cross-correlation of in1 with in2.

    Notes
    -----
    The correlation z of two arrays x and y of rank d is defined as

      z[...,k,...] = sum[..., i_l, ...]
            x[..., i_l,...] * conj(y[..., i_l + k,...])

    """
    val = _valfrommode(mode)

    if old_behavior:
        warnings.warn(DeprecationWarning(_SWAP_INPUTS_DEPRECATION_MSG))
        if np.iscomplexobj(in2):
            in2 = in2.conjugate()
        if in1.size < in2.size:
            swp = in2
            in2 = in1
            in1 = swp

    if mode == 'valid':
        ps = [i - j + 1 for i, j in zip(in1.shape, in2.shape)]
        out = np.empty(ps, in1.dtype)
        for i in range(len(ps)):
            if ps[i] <= 0:
                raise ValueError("Dimension of x(%d) < y(%d) " \
                                 "not compatible with valid mode" % \
                                 (in1.shape[i], in2.shape[i]))

        z = sigtools._correlateND(in1, in2, out, val)
    else:
        ps = [i + j - 1 for i, j in zip(in1.shape, in2.shape)]
        # zero pad input
        in1zpadded = np.zeros(ps, in1.dtype)
        sc = [slice(0, i) for i in in1.shape]
        in1zpadded[sc] = in1.copy()

        if mode == 'full':
            out = np.empty(ps, in1.dtype)
            z = sigtools._correlateND(in1zpadded, in2, out, val)
        elif mode == 'same':
            out = np.empty(in1.shape, in1.dtype)

            z = sigtools._correlateND(in1zpadded, in2, out, val)
        else:
            raise ValueError("Uknown mode %s" % mode)

    return z