Currently the SciPy’s scipy.signal.correlate function supports computing the cross-correlation between two 1D arrays. However, there is no direct support for axis-wise cross-correlation between two 2D arrays of the same shape. For example, computing the cross-correlation between each row (or column) pair across two 2D arrays currently requires implementing a Python for loop. This limitation becomes frustrating when dealing with large datasets, as loops are computationally inefficient, and workarounds can be non-intuitive for many users.
Is there a way to add a parameter to scipy.signal.correlate (e.g., axis) that allows computation of axis-wise cross-correlation between two 2D arrays as shown in the example below
import numpy as np
from scipy.signal import correlate
# Two 2D arrays of the same shape
A = np.random.rand(1000, 100)
B = np.random.rand(1000, 100)
# Compute axis-wise cross-correlation along rows
result = correlate(A, B, axis=1, mode='full')
To enable cross-correlation for 2D numpy arrays along a specified axis, we can utilize the scipy.signal.correlate
function in a more efficient way than using a Python for loop. Instead of processing each row or column individually, we can transform the 2D arrays to compute the cross-correlation using the appropriate axis while leveraging numpy’s array operations.
Here’s a solution that accomplishes this:
import numpy as np
from scipy.signal import correlate
def axiswise_cross_correlation(A, B, axis=0, mode='full'):
# Ensure A and B have the same shape
if A.shape != B.shape:
raise ValueError("Input arrays must have the same shape.")
# Swap axes if axis is not 0, using numpy's swapaxes
transformed_A = np.swapaxes(A, axis, 0) # Bring the desired axis to the first
transformed_B = np.swapaxes(B, axis, 0) # Similarly for B
# Compute the cross-correlation for each row (now the first axis after swap)
result = correlate(transformed_A, transformed_B, mode=mode)
# Swap back the axes to the original arrangement
result = np.swapaxes(result, 0, axis)
return result
# Example usage
A = np.random.rand(1000, 100)
B = np.random.rand(1000, 100)
# Compute axis-wise cross-correlation along rows (axis 1)
result = axiswise_cross_correlation(A, B, axis=1, mode='full')
# The shape of result will depend on the mode and the original arrays' shape
print(result.shape)
Explanation:
-
Axis Handling: The function allows you to specify which axis to cross-correlate over. It swaps the specified axis with the first axis because
scipy.signal.correlate
operates along the first axis of the input arrays. -
Correlation Calculation: We apply
correlate
after transforming the arrays. The input arrays keep their relationship by maintaining their respective structures. -
Restore Original Shape: After calculating the correlation, we swap the axes back to their original positions.
Important Notes:
- This method leverages NumPy’s capabilities to handle operations in a vectorized manner, which makes it efficient.
- Ensure that the input arrays
A
andB
are of the same shape. - This approach provides flexibility as you can choose to correlate along either rows (axis=
1
) or columns (axis=0
).
1