diff options
author | Mario Mulansky <mario.mulansky@gmx.net> | 2015-05-12 18:45:03 +0200 |
---|---|---|
committer | Mario Mulansky <mario.mulansky@gmx.net> | 2015-05-12 18:45:03 +0200 |
commit | 6f418a5a837939b132967bcdb3ff0ede6d899bd2 (patch) | |
tree | 6a90d51c1b091903c0a32c6f745ffd0be1a02534 /pyspike/PieceWiseConstFunc.py | |
parent | a35402c208bd0ad31e5e60b6ddc55a3470e7bdde (diff) |
+functions to obtain values of the pwc/pwl profile
Added __call__ operators to PieceWiseConst and PieceWiseLin class for
obtaining function values at certain points in time.
Diffstat (limited to 'pyspike/PieceWiseConstFunc.py')
-rw-r--r-- | pyspike/PieceWiseConstFunc.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/pyspike/PieceWiseConstFunc.py b/pyspike/PieceWiseConstFunc.py index 41998ef..cf64e58 100644 --- a/pyspike/PieceWiseConstFunc.py +++ b/pyspike/PieceWiseConstFunc.py @@ -26,6 +26,28 @@ class PieceWiseConstFunc(object): self.x = np.array(x) self.y = np.array(y) + def __call__(self, t): + """ Returns the function value for the given time t. If t is a list of + times, the corresponding list of values is returned. + + :param: time t, or list of times + :returns: function value(s) at that time(s). + """ + assert np.all(t >= self.x[0]) and np.all(t <= self.x[-1]), \ + "Invalid time: " + str(t) + + ind = np.searchsorted(self.x, t, side='right') + # correct the cases t == x[0], t == x[-1] + try: + ind[ind == 0] = 1 + ind[ind == len(self.x)] = len(self.x)-1 + except TypeError: + if ind == 0: + ind = 1 + if ind == len(self.x): + ind = len(self.x)-1 + return self.y[ind-1] + def copy(self): """ Returns a copy of itself |