Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate a Toeplitz matrix in the correct form for performing discrete convolution?

Discrete convolution can be performed via the Toeplitz matrix, as shown below (Wiki article):

enter image description here

Note that this is not the exact same form as as the general Toeplitz matrix, but it has experienced various shifts and zero-paddings.

Is there a way to achieve this in numpy purely based on roll, hstack etc., i.e. without using any for loops? I have tried all sorts of shifts but I can't really get it in to the form shown above.

like image 810
Francisco Vargas Avatar asked Dec 30 '15 19:12

Francisco Vargas


People also ask

How do you make a Toeplitz matrix?

t = toeplitz( a , b ) returns a nonsymmetric Toeplitz matrix with a as its first column and b as its first row. b is cast to the numerictype of a . If one of the arguments of toeplitz is a built-in data type, it is cast to the data type of the fi object.

What is toeplitz command in Matlab?

toeplitz( c , r ) generates a nonsymmetric Toeplitz matrix having c as its first column and r as its first row. If the first elements of c and r are different, toeplitz issues a warning and uses the first element of the column. example. toeplitz( r ) generates a symmetric Toeplitz matrix if r is real.

What is a Toeplitz matrix used for?

Toeplitz matrices are used to model systems that posses shift invariant properties. The property of shift invariance is evident from the matrix structure itself. Since we are modelling a Linear Time Invariant system[1], Toeplitz matrices are our natural choice.


1 Answers

Yes, you can use scipy.linalg.toeplitz:

import numpy as np
from scipy import linalg

h = np.arange(1, 6)

padding = np.zeros(h.shape[0] - 1, h.dtype)
first_col = np.r_[h, padding]
first_row = np.r_[h[0], padding]

H = linalg.toeplitz(first_col, first_row)

print(repr(H))
# array([[1, 0, 0, 0, 0],
#        [2, 1, 0, 0, 0],
#        [3, 2, 1, 0, 0],
#        [4, 3, 2, 1, 0],
#        [5, 4, 3, 2, 1],
#        [0, 5, 4, 3, 2],
#        [0, 0, 5, 4, 3],
#        [0, 0, 0, 5, 4],
#        [0, 0, 0, 0, 5]])
like image 162
ali_m Avatar answered Oct 31 '22 16:10

ali_m