convolution_matrix(a, n, mode='full')
Constructs the Toeplitz matrix representing one-dimensional convolution . See the notes below for details.
The code:
A = convolution_matrix(a, n, mode)
creates a Toeplitz matrix A
such that A @ v
is equivalent to using convolve(a, v, mode)
. The returned array always has n
columns. The number of rows depends on the specified :None:None:`mode`
, as explained above.
In the default 'full' mode, the entries of A
are given by:
A[i, j] == (a[i-j] if (0 <= (i-j) < m) else 0)
where m = len(a)
. Suppose, for example, the input array is [x, y, z]
. The convolution matrix has the form:
[x, 0, 0, ..., 0, 0] [y, x, 0, ..., 0, 0] [z, y, x, ..., 0, 0] ... [0, 0, 0, ..., x, 0] [0, 0, 0, ..., y, x] [0, 0, 0, ..., z, y] [0, 0, 0, ..., 0, z]
In 'valid' mode, the entries of A
are given by:
A[i, j] == (a[i-j+m-1] if (0 <= (i-j+m-1) < m) else 0)
This corresponds to a matrix whose rows are the subset of those from the 'full' case where all the coefficients in a
are contained in the row. For input [x, y, z]
, this array looks like:
[z, y, x, 0, 0, ..., 0, 0, 0] [0, z, y, x, 0, ..., 0, 0, 0] [0, 0, z, y, x, ..., 0, 0, 0] ... [0, 0, 0, 0, 0, ..., x, 0, 0] [0, 0, 0, 0, 0, ..., y, x, 0] [0, 0, 0, 0, 0, ..., z, y, x]
In the 'same' mode, the entries of A
are given by:
d = (m - 1) // 2 A[i, j] == (a[i-j+d] if (0 <= (i-j+d) < m) else 0)
The typical application of the 'same' mode is when one has a signal of length n
(with n
greater than len(a)
), and the desired output is a filtered signal that is still of length n
.
For input [x, y, z]
, this array looks like:
[y, x, 0, 0, ..., 0, 0, 0] [z, y, x, 0, ..., 0, 0, 0] [0, z, y, x, ..., 0, 0, 0] [0, 0, z, y, ..., 0, 0, 0] ... [0, 0, 0, 0, ..., y, x, 0] [0, 0, 0, 0, ..., z, y, x] [0, 0, 0, 0, ..., 0, z, y]
The 1-D array to convolve.
The number of columns in the resulting matrix. It gives the length of the input to be convolved with a
. This is analogous to the length of :None:None:`v`
in numpy.convolve(a, v)
.
This is analogous to :None:None:`mode`
in numpy.convolve(v, a, mode)
. It must be one of ('full', 'valid', 'same'). See below for how :None:None:`mode`
determines the shape of the result.
The convolution matrix whose row count :None:None:`k`
depends on :None:None:`mode`
:
======= ========================= mode k ======= ========================= 'full' m + n -1 'same' max(m, n) 'valid' max(m, n) - min(m, n) + 1 ======= =========================
Construct a convolution matrix.
toeplitz
Toeplitz matrix
>>> from scipy.linalg import convolution_matrix
... A = convolution_matrix([-1, 4, -2], 5, mode='same')
... A array([[ 4, -1, 0, 0, 0], [-2, 4, -1, 0, 0], [ 0, -2, 4, -1, 0], [ 0, 0, -2, 4, -1], [ 0, 0, 0, -2, 4]])
Compare multiplication by A
with the use of numpy.convolve
.
>>> x = np.array([1, 2, 0, -3, 0.5])
... A @ x array([ 2. , 6. , -1. , -12.5, 8. ])
Verify that A @ x
produced the same result as applying the convolution function.
>>> np.convolve([-1, 4, -2], x, mode='same') array([ 2. , 6. , -1. , -12.5, 8. ])
For comparison to the case mode='same'
shown above, here are the matrices produced by mode='full'
and mode='valid'
for the same coefficients and size.
>>> convolution_matrix([-1, 4, -2], 5, mode='full') array([[-1, 0, 0, 0, 0], [ 4, -1, 0, 0, 0], [-2, 4, -1, 0, 0], [ 0, -2, 4, -1, 0], [ 0, 0, -2, 4, -1], [ 0, 0, 0, -2, 4], [ 0, 0, 0, 0, -2]])
>>> convolution_matrix([-1, 4, -2], 5, mode='valid') array([[-2, 4, -1, 0, 0], [ 0, -2, 4, -1, 0], [ 0, 0, -2, 4, -1]])See :
The following pages refer to to this document either explicitly or contain code examples using this.
scipy.linalg._special_matrices.convolution_matrix
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them