How to Compute Cross-Correlation of two given Numpy Arrays?

Cross-correlation is a concept widely used in signal processing and image processing to determine the similarity between two signals or images. Python offers an efficient way to compute cross-correlation between numpy arrays using the numpy.correlate() function.

The NumPy library provides numpy.correlate() for calculating cross-correlation of one-dimensional arrays. For two-dimensional arrays, we need to flatten them first and then use the same function.

Syntax

The syntax of the numpy.correlate() function is ?

numpy.correlate(a, v, mode='valid')

Parameters

  • a, v ? Input arrays to calculate cross-correlation
  • mode ? Size of output array ('valid', 'same', 'full')

By default, the mode is set to 'valid', which returns only the parts where the arrays overlap completely.

Cross-correlation of One-dimensional Arrays

Let's compute cross-correlation between two 1D arrays using different modes ?

import numpy as np

a = np.array([1, 2, 3, 4, 5])
v = np.array([0, 1, 0.5])

# Cross-correlation using 'valid' mode
cross_corr_valid = np.correlate(a, v, mode='valid')
print("Cross-correlation using 'valid' mode:", cross_corr_valid)

# Cross-correlation using 'same' mode
cross_corr_same = np.correlate(a, v, mode='same')
print("Cross-correlation using 'same' mode:", cross_corr_same)

# Cross-correlation using 'full' mode
cross_corr_full = np.correlate(a, v, mode='full')
print("Cross-correlation using 'full' mode:", cross_corr_full)
Cross-correlation using 'valid' mode: [2.5 4.  5.5]
Cross-correlation using 'same' mode: [1.  2.5 4.  5.5 2.5]
Cross-correlation using 'full' mode: [0.  0.5 1.  2.5 4.  5.5 2.5 0. ]

Cross-correlation of Two-dimensional Arrays

For 2D arrays, we flatten them into 1D arrays first ?

import numpy as np

a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
v = np.array([[0, 1], [0.5, 2]])

print("Original array a:", a.shape)
print("Original array v:", v.shape)

# Flatten the arrays
a_flat = a.flatten()
v_flat = v.flatten()

print("Flattened a:", a_flat)
print("Flattened v:", v_flat)

# Cross-correlation using different modes
cross_corr_valid = np.correlate(a_flat, v_flat, mode='valid')
cross_corr_same = np.correlate(a_flat, v_flat, mode='same')
cross_corr_full = np.correlate(a_flat, v_flat, mode='full')

print("Valid mode:", cross_corr_valid)
print("Same mode:", cross_corr_same)
print("Full mode:", cross_corr_full)
Original array a: (3, 3)
Original array v: (2, 2)
Flattened a: [1 2 3 4 5 6 7 8 9]
Flattened v: [0.  1.  0.5 2. ]
Valid mode: [27.5 38.  48.5 59.  69.5 80. ]
Same mode: [13.5 27.5 38.  48.5 59.  69.5 80.  40.  18. ]
Full mode: [ 0.   4.5 13.5 27.5 38.  48.5 59.  69.5 80.  40.  18.   0. ]

Understanding Different Modes

Mode Output Size Description
'valid' max(M, N) - min(M, N) + 1 Only complete overlaps
'same' Same as input array a Centered output
'full' M + N - 1 All possible overlaps

Conclusion

Cross-correlation using numpy.correlate() is essential for signal processing and pattern recognition. Use different modes based on your requirements: 'valid' for complete overlaps, 'same' for centered output, and 'full' for all possible correlations.

Updated on: 2026-03-27T09:08:10+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements