返回两个一维序列的离散线性卷积,并在Python中获取它们的重叠部分
要返回两个一维序列的离散线性卷积,请使用Python Numpy中的numpy.convolve()方法。卷积操作在信号处理中经常出现,它模拟了线性时不变系统对信号的影响。在概率论中,两个独立随机变量的和服从它们各自分布的卷积。
如果v的长度大于a的长度,在计算之前会交换数组。该方法返回a和v的离散线性卷积。第一个参数a是第一个一维输入数组,第二个参数v是第二个一维输入数组。第三个参数mode是可选的,可取值为’full’、’valid’、’same’。
模式’valid’返回的输出长度为max(M, N) – min(M, N) + 1。只在信号完全重叠的点给出卷积积。信号边界之外的值没有影响。
步骤
首先,导入所需的库-
import numpy as np
使用array()方法创建两个numpy的一维数组 –
arr1 = np.array([1, 2, 3])
arr2 = np.array([0, 1, 0.5])
显示数组 –
print("Array1...\n",arr1)
print("\nArray2...\n",arr2)
检查两个数组的维度 –
print("\nDimensions of Array1...\n",arr1.ndim)
print("\nDimensions of Array2...\n",arr2.ndim)
检查两个数组的形状−
print("\nShape of Array1...\n",arr1.shape)
print("\nShape of Array2...\n",arr2.shape)
要返回两个一维序列的离散线性卷积,请在Python Numpy中使用numpy.convolve()方法 –
print("\nResult....\n",np.convolve(arr1, arr2, mode = 'valid' ))
示例
import numpy as np
# Creating two numpy One-Dimensional array using the array() method
arr1 = np.array([1, 2, 3])
arr2 = np.array([0, 1, 0.5])
# Display the arrays
print("Array1...\n",arr1)
print("\nArray2...\n",arr2)
# Check the Dimensions of both the arrays
print("\nDimensions of Array1...\n",arr1.ndim)
print("\nDimensions of Array2...\n",arr2.ndim)
# Check the Shape of both the arrays
print("\nShape of Array1...\n",arr1.shape)
print("\nShape of Array2...\n",arr2.shape)
# To return the discrete linear convolution of two one-dimensional sequences, use the numpy.convolve() method in Python Numpy
print("\nResult....\n",np.convolve(arr1, arr2, mode = 'valid' ))
输出
Array1...
[1 2 3]
Array2...
[0. 1. 0.5]
Dimensions of Array1...
1
Dimensions of Array2...
1
Shape of Array1...
(3,)
Shape of Array2...
(3,)
Result....
[2.5]