def pwl(pairs, t):
"""
PWL describes a piecewise linear waveform
:param pairs: a 2D list. Each rom is a pair [time, value]
:param t: array with times where the function has to be evaluated
:return: the function values at times defined in t
"""
# convert pairs to np.array is necessary
if isinstance(pairs, list):
pairs = np.array(pairs)
# check pairs format
if pairs.ndim == 1:
pairs.shape = (-1,2)
# get pairs
x = []
y = []
for xk, yk in pairs:
x.append(xk)
y.append(yk)
# create linear interpolator
fun = interp1d(x, y, bounds_error=False, fill_value=(y[0], y[-1]), assume_sorted=True)
# interpolation
out = fun(t)
return out
评论列表
文章目录