python-scipyHow do I calculate the cross-correlation of two arrays using Python and NumPy?
To calculate the cross-correlation of two arrays using Python and NumPy, the np.correlate()
function can be used. This function computes the correlation as a discrete linear convolution of two one-dimensional sequences.
Example code
import numpy as np
a = np.array([1, 2, 3])
b = np.array([0, 1, 0.5])
np.correlate(a, b, 'full')
Output example
array([0.5, 2. , 3.5, 2. , 0.5])
Code explanation
import numpy as np
: imports the NumPy library as npa = np.array([1, 2, 3])
: creates an array a with the values 1, 2, 3b = np.array([0, 1, 0.5])
: creates an array b with the values 0, 1, 0.5np.correlate(a, b, 'full')
: calculates the cross-correlation of the arrays a and b with the full option, which returns the entire cross-correlation sequence
Helpful links
More of Python Scipy
- How can I check if a certain version of Python is compatible with SciPy?
- How can I use Python Numpy to select elements from an array based on multiple conditions?
- How do I use the scipy ttest_ind function in Python?
- How do I use the NumPy transpose function in Python?
- How do I create a 2D array of zeros using Python and NumPy?
- How do I use Python Numpy to read and write Excel (.xlsx) files?
- How do I use Python and SciPy to write a WAV file?
- How can I use Python and SciPy to generate a uniform distribution?
- How do I use Python and SciPy to create a tutorial PDF?
- How can I use Python and SciPy to perform a Short-Time Fourier Transform?
See more codes...