summaryrefslogtreecommitdiff
path: root/pyspike/function.py
diff options
context:
space:
mode:
authorMario Mulansky <mario.mulansky@gmx.net>2014-09-15 17:01:13 +0200
committerMario Mulansky <mario.mulansky@gmx.net>2014-09-15 17:01:13 +0200
commitf9529c78538882879a07cb67e342eade8d2153ab (patch)
tree33f54461ff5c37c57d6006117ff3a02f21624053 /pyspike/function.py
parente08c05f9bf121e272aadce4fc79e9499e662111e (diff)
isi distance and basic example
Diffstat (limited to 'pyspike/function.py')
-rw-r--r--pyspike/function.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/pyspike/function.py b/pyspike/function.py
new file mode 100644
index 0000000..c1de9cb
--- /dev/null
+++ b/pyspike/function.py
@@ -0,0 +1,60 @@
+""" function.py
+
+Module containing classes representing piece-wise constant and piece-wise linear
+functions.
+
+Copyright 2014, Mario Mulansky <mario.mulansky@gmx.net>
+
+"""
+from __future__ import print_function
+
+import numpy as np
+
+class PieceWiseConstFunc:
+ """ A class representing a piece-wise constant function. """
+
+ def __init__(self, x, y):
+ """ Constructs the piece-wise const function.
+ Params:
+ - x: array of length N+1 defining the edges of the intervals of the pwc
+ function.
+ - y: array of length N defining the function values at the intervals.
+ """
+ self.x = x
+ self.y = y
+
+ def get_plottable_data(self):
+ """ Returns two arrays containing x- and y-coordinates for immeditate
+ plotting of the piece-wise function.
+ """
+
+ x_plot = np.empty(2*len(self.x)-2)
+ x_plot[0] = self.x[0]
+ x_plot[1::2] = self.x[1:]
+ x_plot[2::2] = self.x[1:-1]
+ y_plot = np.empty(2*len(self.y))
+ y_plot[::2] = self.y
+ y_plot[1::2] = self.y
+
+ return x_plot, y_plot
+
+ def avrg(self):
+ """ Computes the average of the piece-wise const function:
+ a = 1/T int f(x) dx where T is the length of the interval.
+ Returns:
+ - the average a.
+ """
+ print(self.x)
+ print(self.y)
+ return np.sum((self.x[1:]-self.x[:-1]) * self.y) / \
+ (self.x[-1]-self.x[0])
+
+ def abs_avrg(self):
+ """ Computes the average of the abs value of the piece-wise const
+ function:
+ a = 1/T int |f(x)| dx where T is the length of the interval.
+ Returns:
+ - the average a.
+ """
+ return np.sum((self.x[1:]-self.x[:-1]) * np.abs(self.y)) / \
+ (self.x[-1]-self.x[0])