Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correlate2d's output does not agree with correlate's output in scipy.signal module (Trac #1443) #1968

Closed
scipy-gitbot opened this issue Apr 25, 2013 · 5 comments · Fixed by #11487
Labels
Documentation Issues related to the SciPy documentation. Also check https://github.com/scipy/scipy.org good first issue Good topic for first contributor pull requests, with a relatively straightforward solution scipy.signal
Milestone

Comments

@scipy-gitbot
Copy link

Original ticket http://projects.scipy.org/scipy/ticket/1443 on 2011-05-23 by trac user SevenSea, assigned to unknown.

When the 2-dimensional array size is even, correlate2d's output does not agree with correlate's output in scipy.signal module. There is a shift of index in the output of correlate2d function. (Information for comparition: convolv2d works good.)

Testing code:

import numpy as np
import scipy.signal as sn

d = np.ones((4,4))
print "data: "
print d

print "correlate(d,d,'same'): "
print sn.correlate(d,d,'same')

print "correlate2d(d,d,'same'): "
print sn.correlate2d(d,d,'same')

The output:

data: 
[[ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]]
correlate(d,d,'same'): 
[[  4.   6.   8.   6.]
 [  6.   9.  12.   9.]
 [  8.  12.  16.  12.]
 [  6.   9.  12.   9.]]
correlate2d(d,d,'same'): 
[[  9.  12.   9.   6.]
 [ 12.  16.  12.   8.]
 [  9.  12.   9.   6.]
 [  6.   8.   6.   4.]]
@orodbhen
Copy link

What is the purpose of correlate2d anyways? I don't see the purpose of having a redundant function, when correlate can do the same thing. Especially when it appears to be broken, and has been for quite some time. What am I missing here?

@endolith
Copy link
Member

@orodbhen correlate2d has different boundary options for image processing

@orodbhen
Copy link

@endolith That makes sense then. correlate doesn't appear to allow explicitly setting the boundary, though. Is it assumed to be zero-padded? Then anything else would have to be done manually in the input, correct?

I'm not clear on why this difference would cause the output to be offset, though. Is that really a bug, or expected behavior? I see this using same and the default boundary setting, like the OP.

@endolith
Copy link
Member

endolith commented Sep 14, 2017

correlate doesn't appear to allow explicitly setting the boundary, though. Is it assumed to be zero-padded? Then anything else would have to be done manually in the input, correct?

Yes

I'm not clear on why this difference would cause the output to be offset, though.

It wouldn't, that's unrelated.

The full output of convolve/correlate is always odd-length, so for odd-length input, same mode is unambiguous, you center the truncated output:

a = [[1, 0, 1], [0, 1, 1], [1, 1, 2]]

b = [[0, 1, 1], [2, 1, 1], [2, 0, 1]]

correlate(a, b)
Out[6]: 
array([[1, 0, 3, 0, 2],
       [1, 2, 4, 3, 4],
       [2, 3, 7, 6, 6],
       [1, 3, 7, 5, 4],
       [1, 2, 3, 2, 0]])

correlate2d(a, b)
Out[7]: 
array([[1, 0, 3, 0, 2],
       [1, 2, 4, 3, 4],
       [2, 3, 7, 6, 6],
       [1, 3, 7, 5, 4],
       [1, 2, 3, 2, 0]])

correlate(a, b, 'same')
Out[8]: 
array([[2, 4, 3],
       [3, 7, 6],
       [3, 7, 5]])

correlate2d(a, b, 'same')
Out[9]: 
array([[2, 4, 3],
       [3, 7, 6],
       [3, 7, 5]])

But for even-length input you can't center the output, so you have to shift it one way or the other. correlate author chose one option and correlate2d author chose the other.

a = [[1, 0], [1, 2]]

b = [[0, 1], [2, 0]]

correlate(a, b)
Out[12]: 
array([[0, 2, 0],
       [1, 2, 4],
       [1, 2, 0]])

correlate2d(a, b)
Out[13]: 
array([[0, 2, 0],
       [1, 2, 4],
       [1, 2, 0]])

correlate(a, b, 'same')
Out[14]: 
array([[0, 2],
       [1, 2]])

correlate2d(a, b, 'same')
Out[15]: 
array([[2, 4],
       [2, 0]])

I'm not sure if one choice makes more sense in certain contexts or if this is just an accident.

It might be this way for consistency with other software, too. Should compare with Octave and Matlab.

@larsoner larsoner added Documentation Issues related to the SciPy documentation. Also check https://github.com/scipy/scipy.org good first issue Good topic for first contributor pull requests, with a relatively straightforward solution and removed Migrated from Trac defect A clear bug or issue that prevents SciPy from being installed or used as expected prio-normal labels Aug 8, 2019
@larsoner
Copy link
Member

larsoner commented Aug 8, 2019

Based on what @endolith concluded, I'm marking this as a documentation issue. I think we need to add to the Notes section of these about which outputs are used in the "same" modes for even-length inputs. Probably not worth a deprecation cycle to unify the behaviors.

AtsushiSakai added a commit to AtsushiSakai/scipy that referenced this issue Feb 9, 2020
@ilayn ilayn added this to the 1.5.0 milestone Feb 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Issues related to the SciPy documentation. Also check https://github.com/scipy/scipy.org good first issue Good topic for first contributor pull requests, with a relatively straightforward solution scipy.signal
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants