Skip to content

Commit

Permalink
ENH: add correlate helper for cross-correlation
Browse files Browse the repository at this point in the history
  • Loading branch information
kohr-h committed Jul 16, 2019
1 parent 5b5007f commit 093280a
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions odl/oplib/convolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from odl.util.npy_compat import roll


__all__ = ('DiscreteConvolution', 'convolve')
__all__ = ('DiscreteConvolution', 'convolve', 'correlate')


class DiscreteConvolution(Operator):
Expand Down Expand Up @@ -735,7 +735,7 @@ def _prepare_for_fft(kernel, padded_shape, axes=None, variant='forward'):


def convolve(x, y, out=None, **kwargs):
"""Return the convolution ``x * y``.
"""Return the convolution of ``x`` and ``y``.
This is a convenience function for quickly computing a convolution
without having to explicitly create a `DiscreteConvolution` instance.
Expand Down Expand Up @@ -791,6 +791,11 @@ def convolve(x, y, out=None, **kwargs):
padded_shape : sequence of ints, optional
Apply zero-padding with this target shape. Cannot be used
together with ``padding``.
See Also
--------
correlate
DiscreteConvolution
"""
y = np.asarray(y)
y_is_complex = issubclass(y.dtype.type, np.complexfloating)
Expand Down Expand Up @@ -826,6 +831,31 @@ def convolve(x, y, out=None, **kwargs):
return out


def correlate(x, y, out=None, **kwargs):
"""Return the cross-correlation of ``x`` and ``y``.
This function computes the cross-correlation defined in continuum as
.. math::
[x \star y](t) = \int x(t + s)\, y(s)\, \,\mathrm{d}s.
The order of ``x`` and ``y`` matters, i.e., this operation is
not commutative.
For details on the function arguments, see `convolve`.
See Also
--------
convolve
DiscreteConvolution
"""
if 'variant' in kwargs:
raise TypeError('cannot use `variant` argument in `correlate`')

kwargs['variant'] = 'adjoint'
return convolve(x, y, out, **kwargs)


if __name__ == '__main__':
from odl.util import run_doctests
run_doctests()

0 comments on commit 093280a

Please sign in to comment.