Code: Select all
# we use numpy to compute the mean of an array of values
import numpy
class MyOVBox(OVBox):
def __init__(self):
OVBox.__init__(self)
self.signalHeader = None
# The process method will be called by openvibe on every clock tick
def initialize(self):
def process(self):
for chunkIndex in range( len(self.input[0])):
# if it's a header we save it and send the output header (same as input, except it has only one channel named 'Mean'
if(type(self.input[0][chunkIndex]) == OVSignalHeader):
self.signalHeader = self.input[0].pop()
outputHeader = OVSignalHeader(
self.signalHeader.startTime,
self.signalHeader.endTime,
[1, self.signalHeader.dimensionSizes[1]],
['Mean']+self.signalHeader.dimensionSizes[1]*[''],
self.signalHeader.samplingRate)
self.output[0].append(outputHeader)
# if it's a buffer we pop it and put it in a numpy array at the right dimensions
# We compute the mean and add the buffer in the box output buffer
elif(type(self.input[0][chunkIndex]) == OVSignalBuffer):
chunk1 = self.input[0].pop()
numpyBuffer = numpy.array(chunk1).reshape(tuple(self.signalHeader.dimensionSizes))
f = open ( '{path_to_file}' , 'r')
l = numpy.array([ map(float,line.split(',')) for line in f ])
temp = l.dot(numpyBuffer)
temp = numpy.array(temp)
chunk = OVSignalBuffer(chunk1.startTime, chunk1.endTime, temp.tolist())
print "chunk = " ,chunk
#chunk is not equal to zero
self.output[0].append(chunk)
# if it's a end-of-stream we just forward that information to the output
elif(type(self.input[0][chunkIndex]) == OVSignalEnd):
self.output[0].append(self.input[0].pop())
# Finally, we notify openvibe that the box instance 'box' is now an instance of MyOVBox.
# Don't forget that step !!
box = MyOVBox()