在 Python 中返回两个一维序列的离散线性卷积
要返回两个一维序列的离散线性卷积,请在 Python Numpy 中使用 numpy.convolve() 方法。
卷积运算符通常出现在信号处理中,它模拟线性时不变系统对信号的影响。在概率论中,两个独立随机变量的和服从其各自分布的卷积。如果 v 比 a 长,则在计算之前交换数组。该方法返回 a 和 v 的离散线性卷积。第一个参数 a 是第一个一维输入数组。第二个参数 v 是第二个一维输入数组。第三个参数 mode 是可选的,其值可以是“full”、“valid”、“same”
步骤
首先,导入所需的库 -
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)
要返回两个一维序列的离散线性卷积,请使用 numpy.convolve() 方法 -
print("\nResult....\n",np.convolve(arr1, arr2 ))
示例
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 ))
输出
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.... [0. 1. 2.5 4. 1.5]
广告