返回Python中两个一维序列的离散线性卷积并返回中间值
为了返回两个一维序列的离散线性卷积,可以使用Python Numpy中的numpy.convolve()方法。在信号处理中,卷积运算符经常用于模拟线性时不变系统对信号的影响。在概率论中,两个独立随机变量的和分布与它们各自分布的卷积相对应。如果v的长度比a长,则在计算之前交换数组。
该方法返回a和v的离散线性卷积。第一个参数a是第一个一维输入数组,第二个参数v是第二个一维输入数组。第三个可选参数mode可以取值’full’、’valid’、’same’。模式’same’返回长度为max(M, N)的输出。边界效应仍然可见。
步骤
首先,导入所需的库−
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, mode = 'same' ))
示例
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 = 'same' ))
输出
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....
[1. 2.5 4. ]