返回Python中两个一维序列的离散线性卷积
要返回两个一维序列的离散线性卷积,请使用Python Numpy中的numpy.convolve()方法。
卷积运算符通常用于信号处理,它模拟线性时不变系统对信号的影响。在概率论中,两个独立随机变量的和按照它们各自分布的卷积分布。如果v比a长,计算前会交换数组。该方法返回a和v的离散线性卷积。第一个参数a是第一个一维输入数组。第二个参数v是第二个一维输入数组。第三个参数mode是可选的,可以取值’full’,’valid’,’same’。
步骤
首先,导入所需的库−
import numpy as np
创建两个numpy一维数组使用array()方法−
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]