diff --git a/pyrelacs/dataio/circbuffer.py b/pyrelacs/dataio/circbuffer.py index d24b8df..2433543 100644 --- a/pyrelacs/dataio/circbuffer.py +++ b/pyrelacs/dataio/circbuffer.py @@ -11,11 +11,12 @@ class CircBuffer: self.__buffer = np.zeros( (channels, size), dtype=np.double ) # or dtype of your choice - self.__time = np.zeros(size, dtype=np.double) + self.__time = np.zeros((channels, size), dtype=np.double) self.__index = [0 for i in range(channels)] self.__is_full = [False for i in range(channels)] self.__totalcount = [0 for i in range(channels)] self.__overflows = [0 for i in range(channels)] + self.__read_increment = samplerate * 0.1 @property def size(self): @@ -42,8 +43,8 @@ class CircBuffer: self.__buffer[channel, self.write_index(channel)] = item self.__index[channel] = (self.write_index(channel) + 1) % self.__size self.__totalcount[channel] += 1 - self.__time[self.write_index(channel=0)] = ( - self.__time[self.write_index(channel=0) - 1] + 1 / self.__samplereate + self.__time[channel, self.write_index(channel)] = ( + self.__time[channel, self.write_index(channel) - 1] + 1 / self.__samplereate ) if self.__index[channel] == 0: self.__is_full[channel] = True @@ -128,14 +129,18 @@ class CircBuffer: f"Invalid index {index} on ring buffer for channel{channel}" ) - def read(self, start, count=1, channel=0): + def read(self, start, extend=1, channel=0): """Reads a numpy array from buffer""" - if start < 0 or count < 0: - raise IndexError( - f"Invalid start ({start}) or count ({count}) for channel{channel}" - ) + if extend < 0: + raise IndexError(f"Invalid extend ({extend}) for channel {channel}") + if not self.is_full(channel): + if start < 0: + raise IndexError(f"Invalid start ({start}) for channel {channel}") + else: + if start < 0: + start = start + self.size - if count == 1: + if extend == 1: return np.array(self.get(start, channel)) vs, vc = self.valid_range(channel) @@ -147,17 +152,32 @@ class CircBuffer: raise IndexError( f"Invalid start index {start} for buffer with size {self.size}" ) - if count > self.size: - count = self.size - if count > vc: - count = vc - if (start + count) < self.size: - return self.__buffer[channel, start : start + count] + if extend > vc: + extend = vc + + if (start + extend) > self.__totalcount[channel]: + raise IndexError( + f" Invalid range, extended over the totalcount of the buffer {self.__totalcount[channel]}" + ) + + if (start + extend) < self.size: + return ( + self.__time[channel, start : start + extend], + self.__buffer[channel, start : start + extend], + ) else: - return np.concatenate( - ( - self.__buffer[channel, start:], - self.__buffer[channel, : count - self.size + start], - ) + return ( + np.concatenate( + ( + self.__time[channel, start:], + self.__time[channel, : extend - self.size + start], + ) + ), + np.concatenate( + ( + self.__buffer[channel, start:], + self.__buffer[channel, : extend - self.size + start], + ) + ), )