summaryrefslogtreecommitdiff
path: root/numpy/ma/core.py
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/ma/core.py')
-rw-r--r--numpy/ma/core.py83
1 files changed, 79 insertions, 4 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py
index c4a54acb4..4466dc0af 100644
--- a/numpy/ma/core.py
+++ b/numpy/ma/core.py
@@ -56,10 +56,10 @@ __all__ = [
'argmax', 'argmin', 'argsort', 'around', 'array', 'asanyarray',
'asarray', 'bitwise_and', 'bitwise_or', 'bitwise_xor', 'bool_', 'ceil',
'choose', 'clip', 'common_fill_value', 'compress', 'compressed',
- 'concatenate', 'conjugate', 'copy', 'cos', 'cosh', 'count', 'cumprod',
- 'cumsum', 'default_fill_value', 'diag', 'diagonal', 'diff', 'divide',
- 'dump', 'dumps', 'empty', 'empty_like', 'equal', 'exp', 'expand_dims',
- 'fabs', 'filled', 'fix_invalid', 'flatten_mask',
+ 'concatenate', 'conjugate', 'convolve', 'copy', 'correlate', 'cos', 'cosh',
+ 'count', 'cumprod', 'cumsum', 'default_fill_value', 'diag', 'diagonal',
+ 'diff', 'divide', 'dump', 'dumps', 'empty', 'empty_like', 'equal', 'exp',
+ 'expand_dims', 'fabs', 'filled', 'fix_invalid', 'flatten_mask',
'flatten_structured_array', 'floor', 'floor_divide', 'fmod',
'frombuffer', 'fromflex', 'fromfunction', 'getdata', 'getmask',
'getmaskarray', 'greater', 'greater_equal', 'harden_mask', 'hypot',
@@ -7366,6 +7366,81 @@ outer.__doc__ = doc_note(np.outer.__doc__,
outerproduct = outer
+def _convolve_or_correlate(f, a, v, mode, propagate_mask):
+ """
+ Helper function for ma.correlate and ma.convolve
+ """
+ if propagate_mask:
+ # results which are contributed to by either item in any pair being invalid
+ mask = (
+ f(getmaskarray(a), np.ones(np.shape(v), dtype=np.bool), mode=mode)
+ | f(np.ones(np.shape(a), dtype=np.bool), getmaskarray(v), mode=mode)
+ )
+ data = f(getdata(a), getdata(v), mode=mode)
+ else:
+ # results which are not contributed to by any pair of valid elements
+ mask = ~f(~getmaskarray(a), ~getmaskarray(v))
+ data = f(filled(a, 0), filled(v, 0), mode=mode)
+
+ return masked_array(data, mask=mask)
+
+
+def correlate(a, v, mode='valid', propagate_mask=True):
+ """
+ Cross-correlation of two 1-dimensional sequences.
+
+ Parameters
+ ----------
+ a, v : array_like
+ Input sequences.
+ mode : {'valid', 'same', 'full'}, optional
+ Refer to the `np.convolve` docstring. Note that the default
+ is 'valid', unlike `convolve`, which uses 'full'.
+ propagate_mask : bool
+ If True, then a result element is masked if any masked element contributes towards it.
+ If False, then a result element is only masked if no non-masked element
+ contribute towards it
+
+ Returns
+ -------
+ out : MaskedArray
+ Discrete cross-correlation of `a` and `v`.
+
+ See Also
+ --------
+ numpy.correlate : Equivalent function in the top-level NumPy module.
+ """
+ return _convolve_or_correlate(np.correlate, a, v, mode, propagate_mask)
+
+
+def convolve(a, v, mode='full', propagate_mask=True):
+ """
+ Returns the discrete, linear convolution of two one-dimensional sequences.
+
+ Parameters
+ ----------
+ a, v : array_like
+ Input sequences.
+ mode : {'valid', 'same', 'full'}, optional
+ Refer to the `np.convolve` docstring.
+ propagate_mask : bool
+ If True, then if any masked element is included in the sum for a result
+ element, then the result is masked.
+ If False, then the result element is only masked if no non-masked cells
+ contribute towards it
+
+ Returns
+ -------
+ out : MaskedArray
+ Discrete, linear convolution of `a` and `v`.
+
+ See Also
+ --------
+ numpy.convolve : Equivalent function in the top-level NumPy module.
+ """
+ return _convolve_or_correlate(np.convolve, a, v, mode, propagate_mask)
+
+
def allequal(a, b, fill_value=True):
"""
Return True if all entries of a and b are equal, using