1 """Routines to pad convolution to control the centering of the kernel.
2
3 """
4 import numpy
5 -def pad(x, klength, origin=0, value=0.):
6 """Pad an array that is going to be convolved.
7
8 Set origin to zero to have only positive delays. Set origin to
9 klength-1 to have entirely negative delays. Set origin to the
10 center of the kernel for centering on zero delay.
11
12 Parameters
13 ----------
14 klength: scalar
15 Length of the kernel.
16 origin: scalar
17 The index of the kernel value you want at the origin (default 0).
18 value: scalar
19 Value to pad the array with (default 0).
20
21 """
22 if (origin > klength-1):
23 raise ValueError("Origin can't be > klength-1.")
24 elif (origin < 0):
25 raise ValueError("Origin can't be < 0.")
26 return numpy.hstack((numpy.zeros(klength-1-origin) + value,
27 x,
28 numpy.zeros(origin) + value))
29
31 """Convolve an array with padding.
32
33 See docstring for pad for more info.
34 """
35 return numpy.convolve(pad(x, len(kernel), origin, value),
36 kernel,
37 mode='valid')
38