如何计算两个给定 NumPy 数组的互相关?
在 Numpy 程序中,我们可以借助 correlate() 计算两个给定数组的互相关。在第一个参数和第二个参数传递给定数组时,它将返回两个给定数组的互相关。
Syntax : numpy.correlate(a, v, mode = ‘valid’)
Parameters :
a, v : [array_like] Input sequences.
mode : [{‘valid’, ‘same’, ‘full’}, optional] Refer to the convolve docstring. Default is ‘valid’.
Return : [ndarray] Discrete cross-correlation of a and v.
示例 1:
在这个例子中,我们将创建两个 NumPy 数组,任务是使用correlate()计算互相关。
Python3
import numpy as np
array1 = np.array([0, 1, 2])
array2 = np.array([3, 4, 5])
# Original array1
print(array1)
# Original array2
print(array2)
# ross-correlation of the arrays
print("\nCross-correlation:\n",
np.correlate(array1, array2))
Python3
import numpy as np
array1 = np.array([1,2])
array2 = np.array([1,2])
# Original array1
print(array1)
# Original array2
print(array2)
# Cross-correlation of the arrays
print("\nCross-correlation:\n",
np.correlate(array1, array2))
输出:
[0 1 2]
[3 4 5]
Cross-correlation:
[14]
示例2:
蟒蛇3
import numpy as np
array1 = np.array([1,2])
array2 = np.array([1,2])
# Original array1
print(array1)
# Original array2
print(array2)
# Cross-correlation of the arrays
print("\nCross-correlation:\n",
np.correlate(array1, array2))
输出:
[1 2]
[1 2]
Cross-correlation:
[5]