[time] adding time to the buffer and retruning it with the item

This commit is contained in:
wendtalexander 2024-10-09 07:58:41 +02:00
parent aaa42db2ae
commit 8378add874

View File

@ -11,10 +11,10 @@ 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.__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.__time = [0.0 for i in range(channels)]
self.__overflows = [0 for i in range(channels)]
@property
@ -42,7 +42,9 @@ 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[channel] += 1 / self.__samplereate
self.__time[self.write_index(channel=0)] = (
self.__time[self.write_index(channel=0) - 1] + 1 / self.__samplereate
)
if self.__index[channel] == 0:
self.__is_full[channel] = True
self.__overflows[channel] += 1
@ -107,11 +109,11 @@ class CircBuffer:
extend = self.size
return start, extend
def get(self, index: int = -1, channel: int = 0):
def get(self, index: int = -1, channel: int = 0) -> Tuple[np.double, float]:
# easy case first, we can spare the effort of further checking
if index >= 0 and index <= self.write_index(channel):
if self.has_value(index, channel):
return self.__buffer[channel, index]
return (self.__buffer[channel, index], self.__time[index])
else:
raise IndexError(
f"Invalid index {index} on ring buffer for channel{channel}"
@ -120,7 +122,7 @@ class CircBuffer:
if index < 0:
index = self.write_index() - 1
if self.has_value(index, channel):
return self.__buffer[channel, index]
return (self.__buffer[channel, index], self.__time[index])
else:
raise IndexError(
f"Invalid index {index} on ring buffer for channel{channel}"