python numpy np.convolve()函数(返回两个一维序列的离散线性卷积)

from numpy.core.numeric()

def convolve(a, v, mode='full'):
    """
    Returns the discrete, linear convolution of two one-dimensional sequences.
    返回两个一维序列的离散线性卷积。

    The convolution operator is often seen in signal processing, where it
    models the effect of a linear time-invariant system on a signal [1]_.  In
    probability theory, the sum of two independent random variables is
    distributed according to the convolution of their individual
    distributions.
	卷积算子经常出现在信号处理中,它在信号[1] _上模拟线性时不变系统的效果。 
	在概率论中,两个独立随机变量之和是根据它们各自分布的卷积分布的。

    If `v` is longer than `a`, the arrays are swapped before computation.
    如果“ v”长于“ a”,则在计算之前交换数组。

    Parameters
    ----------
    a : (N,) array_like
        First one-dimensional input array.
        第一个一维输入数组。
    v : (M,) array_like
        Second one-dimensional input array.
        第二个一维输入数组。
    mode : {'full', 'valid', 'same'}, optional
        'full':
          By default, mode is 'full'.  This returns the convolution
          at each point of overlap, with an output shape of (N+M-1,). At
          the end-points of the convolution, the signals do not overlap
          completely, and boundary effects may be seen.
		默认情况下,模式为“完整”。 
		这将在每个重叠点返回卷积,输出形状为(N + M-1,)。 
		在卷积的端点,信号没有完全重叠,并且可以看到边界效应。

        'same':
          Mode 'same' returns output of length ``max(M, N)``.  Boundary
          effects are still visible.
          模式'same'返回长度为``max(M,N)``的输出。 边界效果仍然可见。

        'valid':
          Mode 'valid' returns output of length
          ``max(M, N) - min(M, N) + 1``.  The convolution product is only given
          for points where the signals overlap completely.  Values outside
          the signal boundary have no effect.
          模式'valid'返回长度为``max(M,N)-min(M,N)+ 1''的输出。 
          卷积仅针对信号完全重叠的点给出。 信号边界之外的值无效。

    Returns
    -------
    out : ndarray
        Discrete, linear convolution of `a` and `v`.
        a和v的离散线性卷积。

    See Also
    --------
    scipy.signal.fftconvolve : Convolve two arrays using the Fast Fourier
                               Transform.
                               scipy.signal.fftconvolve:使用快速傅立叶变换对两个数组进行卷积。
                               
    scipy.linalg.toeplitz : Used to construct the convolution operator.
    scipy.linalg.toeplitz:用于构造卷积运算符。
    
    polymul : Polynomial multiplication. Same output as convolve, but also
              accepts poly1d objects as input.
              polymul:多项式乘法。 与卷积相同的输出,但也接受poly1d对象作为输入。

    Notes
    -----
    The discrete convolution operation is defined as

    .. math:: (a * v)[n] = \\sum_{m = -\\infty}^{\\infty} a[m] v[n - m]

    It can be shown that a convolution :math:`x(t) * y(t)` in time/space
    is equivalent to the multiplication :math:`X(f) Y(f)` in the Fourier
    domain, after appropriate padding (padding is necessary to prevent
    circular convolution).  Since multiplication is more efficient (faster)
    than convolution, the function `scipy.signal.fftconvolve` exploits the
    FFT to calculate the convolution of large data-sets.

	离散卷积运算定义为

     .. math ::(a * v)[n] = \\ sum_ {m =-\\ infty} ^ {\\ infty} a [m] v [n-m]

     可以证明,在适当的情况下,时间/空间中的卷积:x(t)* y(t)等价于傅立叶域中的乘积:math:`X(f)Y(f)`。 
     填充(必须填充以防止圆形卷积)。 
     由于乘法比卷积更有效(更快),因此函数“ scipy.signal.fftconvolve”利用FFT来计算大数据集的卷积。

    References
    ----------
    .. [1] Wikipedia, "Convolution", http://en.wikipedia.org/wiki/Convolution.

    Examples
    --------
    Note how the convolution operator flips the second array
    before "sliding" the two across one another:
    请注意,卷积运算符如何翻转第二个数组,然后才将它们“滑动”在一起:

    >>> np.convolve([1, 2, 3], [0, 1, 0.5])
    array([ 0. ,  1. ,  2.5,  4. ,  1.5])

    Only return the middle values of the convolution.
    Contains boundary effects, where zeros are taken
    into account:

    >>> np.convolve([1,2,3],[0,1,0.5], 'same')
    array([ 1. ,  2.5,  4. ])

    The two arrays are of the same length, so there
    is only one position where they completely overlap:

    >>> np.convolve([1,2,3],[0,1,0.5], 'valid')
    array([ 2.5])

    """
    a, v = array(a, copy=False, ndmin=1), array(v, copy=False, ndmin=1)
    if (len(v) > len(a)):
        a, v = v, a
    if len(a) == 0:
        raise ValueError('a cannot be empty')
    if len(v) == 0:
        raise ValueError('v cannot be empty')
    mode = _mode_from_name(mode)
    return multiarray.correlate(a, v[::-1], mode)

咋算的,我一点都没看懂??

计算流程

示例:
数字输入的是离散信号,如下图。
已知x[0] = a,x[1]=b,x[2]=c
在这里插入图片描述
已知y[0]=i,y[1]=j,y[2]=k
在这里插入图片描述
下面演示x[n]*y[n]过程
第一步,x[n]乘以y[0]并平移到位置0:
在这里插入图片描述
第二步,x[n]乘以y[1]并平移到位置1:
在这里插入图片描述
第三步,x[n]乘以y[2]并平移到位置2:
在这里插入图片描述
最后,把上面三个图叠加,就得到了x[n] * y[n]:
在这里插入图片描述

参考文章:numpy中的convolve的理解

发布了771 篇原创文章 · 获赞 32 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/Dontla/article/details/104080345