# [SciPy-User] multidimensional signal.convolve semivalid

josef.pktd@gmai... josef.pktd@gmai...
Thu Jan 7 11:04:59 CST 2010

```simplest case I have two signals and I want to apply two linear
filters with convolve. As a result I want to get two signals given by
the convolution of the input signal with each of the filter arrays.

I can either loop over the filter arrays with valid mode which
produces the desired result

signal.convolve(x,a3f[:,:,0], mode='valid')
signal.convolve(x,a3f[:,:,1], mode='valid')

or a can do one 3 dimensional convolution, and throw away two thirds
of the calculation

signal.convolve(x[:,:,None],a3f)[:,1,:]

I didn't manage to get valid or same mode to return the results that I
wanted. Is there a way to do it without loop or redundant
calculations?

background: this will be the fastest way to filter and work with
vector autoregressive processes

example below

Thanks

Josef

>>> x = np.arange(40).reshape((2,20)).T
>>> a3f[:,:,0]
array([[ 0.5,  1. ],
[ 0.5,  1. ]])
>>> a3f[:,:,1]
array([[ 1. ,  0.5],
[ 1. ,  0.5]])

>>> signal.convolve(x[:,:,None],a3f)[:,1,:]
array([[ 10. ,  20. ],
[ 21.5,  41.5],
[ 24.5,  44.5],
[ 27.5,  47.5],
[ 30.5,  50.5],
[ 33.5,  53.5],
[ 36.5,  56.5],
[ 39.5,  59.5],
[ 42.5,  62.5],
[ 45.5,  65.5],
[ 48.5,  68.5],
[ 51.5,  71.5],
[ 54.5,  74.5],
[ 57.5,  77.5],
[ 60.5,  80.5],
[ 63.5,  83.5],
[ 66.5,  86.5],
[ 69.5,  89.5],
[ 72.5,  92.5],
[ 75.5,  95.5],
[ 38.5,  48.5]])
>>> signal.fftconvolve(x[:,:,None],a3f).shape
(21, 3, 2)
>>> signal.fftconvolve(x[:,:,None],a3f)[:,1,:]
array([[ 10. ,  20. ],
[ 21.5,  41.5],
[ 24.5,  44.5],
[ 27.5,  47.5],
[ 30.5,  50.5],
[ 33.5,  53.5],
[ 36.5,  56.5],
[ 39.5,  59.5],
[ 42.5,  62.5],
[ 45.5,  65.5],
[ 48.5,  68.5],
[ 51.5,  71.5],
[ 54.5,  74.5],
[ 57.5,  77.5],
[ 60.5,  80.5],
[ 63.5,  83.5],
[ 66.5,  86.5],
[ 69.5,  89.5],
[ 72.5,  92.5],
[ 75.5,  95.5],
[ 38.5,  48.5]])
>>> signal.fftconvolve(x[:,:],a3f[:,:,0]).shape
(21, 3)
>>> signal.fftconvolve(x[:,:],a3f[:,:,0], mode='valid')
array([[ 21.5],
[ 24.5],
[ 27.5],
[ 30.5],
[ 33.5],
[ 36.5],
[ 39.5],
[ 42.5],
[ 45.5],
[ 48.5],
[ 51.5],
[ 54.5],
[ 57.5],
[ 60.5],
[ 63.5],
[ 66.5],
[ 69.5],
[ 72.5],
[ 75.5]])
>>> signal.fftconvolve(x[:,:],a3f[:,:,1], mode='valid')
array([[ 41.5],
[ 44.5],
[ 47.5],
[ 50.5],
[ 53.5],
[ 56.5],
[ 59.5],
[ 62.5],
[ 65.5],
[ 68.5],
[ 71.5],
[ 74.5],
[ 77.5],
[ 80.5],
[ 83.5],
[ 86.5],
[ 89.5],
[ 92.5],
[ 95.5]])

>>>
```