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, being one of the most popular programming languages, offers an efficient and user−friendly way to compute cross−correlation between numpy arrays using the NumPy library. The NumPy library offers the numpy.correlate() function to calculate the cross−correlation of one−dimensional numpy arrays. However, for two−dimensional arrays, we need to first flatten them and then use the same function to compute the cross−correlation.

In this article, we will provide a detailed discussion on how to compute cross−correlation of two given numpy arrays using the numpy.correlate() function. We will also demonstrate examples of computing cross−correlation for both one−dimensional and two−dimensional numpy arrays using different modes.

Syntax of numpy.correlate()

The syntax of the numpy.correlate() function is as follows:

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

This function takes two arrays as inputs − which are typically named a and v − and calculates the similarity between them. Optionally, you can also set the mode parameter to specify the size of the output array. By default, the mode is set to 'valid', which means that the resulting output will only show the parts of the arrays where they overlap. In other words, the output will be trimmed to only display the areas where a and v are similar.

Example 1: Cross−correlation of one−dimensional numpy arrays

Let's take two one−dimensional numpy arrays a and v and calculate the cross−correlation between them.

Now open your Python editor and refer to the following example and make sure to read the comments for your understanding.

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 the '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)

Output

The output will look like this:

Cross-correlation using 'valid' mode: [2.5 4.  6.5]
Cross-correlation using 'same' mode: [2.  2.5 4.  6.5 4. ]
Cross-correlation using 'full' mode: [0.  0.5 2.  2.5 4.  6.5 4.  0. ]

In the above example, we created two numpy arrays a and v. The first array a has shape (5,) and the second array v has shape (3,). We computed the cross−correlation of these two arrays using the numpy.correlate() function with three different modes: 'valid', 'same', and 'full'.

In the 'valid' mode, the cross−correlation is computed only for the overlapping parts of the two arrays. As a result, the output has shape (3,). In the 'same' mode, the output has the same shape as the input array a. In the 'full' mode, the output has shape (7,), which is the sum of the lengths of the two input arrays minus one.

Example 2: Cross−correlation of two−dimensional numpy arrays

Let's take two two−dimensional numpy arrays a and v and calculate the cross−correlation between them.

Open your Python editor and start creating a two−dimensional array to calculate the cross−correlation between them.

import numpy as np

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

To calculate the cross−correlation between two−dimensional numpy arrays, we need to flatten the arrays into one−dimensional arrays and then apply the numpy.correlate() function.

For that, refer to the following code:

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

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

# Cross-correlation using the 'same' mode
cross_corr_same = np.correlate(a_flat, v_flat, mode='same')
print("Cross-correlation using 'same' mode:", cross_corr_same)

# Cross-correlation using 'full' mode
cross_corr_full = np.correlate(a_flat, v_flat, mode='full')
print("Cross-correlation using 'full' mode:", cross_corr_full)

Output

The output will look like this:

Cross-correlation using 'valid' mode: [27.5 38.  27. ]
Cross-correlation using 'same' mode: [13.5 27.5 38.  27.  13.5]
Cross-correlation using 'full' mode: [ 0.   4.5 13.5 27.5 38.  27.  13.5  2. ]

In the above example, we created two two−dimensional numpy arrays a and v. The first array a has shape (3, 3) and the second array v has shape (2, 2). We flattened both arrays and then computed the cross−correlation using the numpy.correlate() function with three different modes: 'valid', 'same', and 'full'.

In the 'valid' mode, the cross−correlation is computed only for the overlapping parts of the two arrays. As a result, the output has shape (3,). In the 'same' mode, the output has the same shape as the flattened input array a. In the 'full' mode, the output has shape (8,), which is the sum of the lengths of the two flattened input arrays minus one.

Conclusion

Cross−correlation is an essential concept in signal processing and image processing that can help measure the similarity between two signals or images. Fortunately, Python's NumPy library provides a convenient way to calculate cross−correlation using the numpy.correlate() function. This function is easy to use and can help you compare the similarity between two arrays, regardless of whether they are one−dimensional or two−dimensional. By leveraging the different modes of the numpy.correlate() function, you can customize your results to meet your specific requirements. Familiarity with this function can be valuable in a wide range of applications, including speech recognition, image analysis, and pattern recognition. This article aims to provide you with a clear understanding of how to compute cross−correlation between two numpy arrays and how to utilize the numpy.correlate() function effectively. With the knowledge gained from this article, you will be better equipped to leverage the power of cross−correlation in your signal and image processing projects.

Updated on: 20-Jul-2023

951 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements