dcmri.conv#

dcmri.conv(f, h, t=None, dt=1.0, solver='step')[source]#

Convolve two 1D-arrays.

This function returns the convolution \(f(t)\otimes h(t)\), using piecewise linear integration to approximate the integrals in the convolution product.

Parameters:
  • f (array_like) – the first 1D array to be convolved.

  • h (array_like) – the second 1D array to be convolved.

  • t (array_like, optional) – the time points where the values of f are defined. If t=None, the time points are assumed to be uniformly spaced with spacing dt. Defaults to None.

  • dt (float, optional) – spacing between time points for uniformly spaced time points. This parameter is ignored if t is explicity provided. Defaults to 1.0.

Raises:

ValueError – if f and h have a different length.

Returns:

a 1D numpy array of the same length as f and h.

Return type:

numpy.ndarray

Notes

The convolution product \(f(t)\otimes h(t)\) implemented by conv is explicitly defined as:

\[g(t) = \int_0^t du\, f(u) h(t-u)\]

conv returns an approximation to this continuous convolution product, calculated by piecewise linear integration. This is not to be confused with other convolution functions, such as numpy.convolve which performs discrete convolution. Tracer-kinetic theory is defined by continuous equations and therefore should be performed with conv for maximal accuracy, though the difference may be small at high temporal resolution.

conv is generally applicable to any f(t) and h(t), but more accurate formulae for some special cases exists and should be used if available. An example is expconv, to be used when either f(t) or h(t) is an exponential.

Example

Import package and create vectors f and h:

>>> import dcmri as dc
>>> f = [5,4,3,6]
>>> h = [2,9,1,3]

Calculate \(g(t) = f(t) \otimes h(t)\) over a uniformly sampled grid of time points with spacing dt=1:

>>> dc.conv(f, h)
array([ 0.        , 25.33333333, 41.66666667, 49.        ])

Calculate the same convolution over a grid of time points with spacing dt=2:

>>> dc.conv(f, h, dt=2)
array([ 0.        , 50.66666667, 83.33333333, 98.        ])

Calculate the same convolution over a non-uniform grid of time points:

>>> t = [0,1,3,7]
>>> dc.conv(f, h, t)
array([  0.        ,  25.33333333,  57.41666667, 108.27083333])

Examples using dcmri.conv#

A comparison of convolution functions

A comparison of convolution functions