使用Python返回两个一维序列的离散线性卷积
要返回两个一维序列的离散线性卷积,请在Python Numpy中使用numpy.convolve()方法。卷积运算符经常出现在信号处理中,它模拟线性时不变系统对信号的影响。在概率论中,两个独立随机变量的和服从其各自分布卷积的分布。如果v比a长,则在计算之前交换数组。
该方法返回a和v的离散线性卷积。第一个参数a (N,)是第一个一维输入数组。第二个参数v (M,)是第二个一维输入数组。第三个参数mode是可选的,其值为'full'、'valid'、'same'。模式'valid'返回长度为max(M, N) - min(M, N) + 1的输出。卷积积只给出信号完全重叠的点。信号边界外的值没有影响。
默认模式为'full'。这将返回每个重叠点的卷积,输出形状为(N+M-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 = 'full' ))
示例
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 = 'full' ))
输出
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]
广告