python numpy np.convolve()函数(返回两个一维序列的离散线性卷积)
生活随笔
收集整理的這篇文章主要介紹了
python numpy np.convolve()函数(返回两个一维序列的离散线性卷积)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- from numpy.core.numeric()
- 計算流程
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 itmodels the effect of a linear time-invariant system on a signal [1]_. Inprobability theory, the sum of two independent random variables isdistributed according to the convolution of their individualdistributions.卷積算子經常出現在信號處理中,它在信號[1] _上模擬線性時不變系統的效果。 在概率論中,兩個獨立隨機變量之和是根據它們各自分布的卷積分布的。If `v` is longer than `a`, the arrays are swapped before computation.如果“ v”長于“ a”,則在計算之前交換數組。Parameters----------a : (N,) array_likeFirst one-dimensional input array.第一個一維輸入數組。v : (M,) array_likeSecond one-dimensional input array.第二個一維輸入數組。mode : {'full', 'valid', 'same'}, optional'full':By default, mode is 'full'. This returns the convolutionat each point of overlap, with an output shape of (N+M-1,). Atthe end-points of the convolution, the signals do not overlapcompletely, and boundary effects may be seen.默認情況下,模式為“完整”。 這將在每個重疊點返回卷積,輸出形狀為(N + M-1,)。 在卷積的端點,信號沒有完全重疊,并且可以看到邊界效應。'same':Mode 'same' returns output of length ``max(M, N)``. Boundaryeffects 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 givenfor points where the signals overlap completely. Values outsidethe signal boundary have no effect.模式'valid'返回長度為``max(M,N)-min(M,N)+ 1''的輸出。 卷積僅針對信號完全重疊的點給出。 信號邊界之外的值無效。Returns-------out : ndarrayDiscrete, linear convolution of `a` and `v`.a和v的離散線性卷積。See Also--------scipy.signal.fftconvolve : Convolve two arrays using the Fast FourierTransform.scipy.signal.fftconvolve:使用快速傅立葉變換對兩個數組進行卷積。scipy.linalg.toeplitz : Used to construct the convolution operator.scipy.linalg.toeplitz:用于構造卷積運算符。polymul : Polynomial multiplication. Same output as convolve, but alsoaccepts 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/spaceis equivalent to the multiplication :math:`X(f) Y(f)` in the Fourierdomain, after appropriate padding (padding is necessary to preventcircular convolution). Since multiplication is more efficient (faster)than convolution, the function `scipy.signal.fftconvolve` exploits theFFT 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 arraybefore "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 takeninto 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 thereis 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, aif 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的理解
總結
以上是生活随笔為你收集整理的python numpy np.convolve()函数(返回两个一维序列的离散线性卷积)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Intel Realsense pyre
- 下一篇: python 如何用指数函数拟合数据?(