Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
feat(misc): add buffer-class for realtime applications
Browse files Browse the repository at this point in the history
  • Loading branch information
mgrub committed Mar 2, 2020
1 parent 53ca6f5 commit d105de2
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions PyDynamic/misc/buffer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import numpy as np
from collections import deque

# define some buffer to fill, view and consume
class Buffer():

def __init__(self, maxlen=1000):
self.timestamps = deque(maxlen=maxlen)
self.values = deque(maxlen=maxlen)
self.uncertainties = deque(maxlen=maxlen)

def append(self, time=0.0, value=0.0, uncertainty=0.0):
self.timestamps.append(time)
self.values.append(value)
self.uncertainties.append(uncertainty)

def append_multi(self, timestamps, values, uncertainties):

for t, v, u in zip(timestamps, values, uncertainties):
self.append(t, v, u)

def popleft(self):
t = self.timestamps.popleft()
v = self.values.popleft()
u = self.uncertainties.popleft()
return t, v, u

def view_last(self, n):
r = range(-n,0)

t = np.array([self.timestamps[i] for i in r])
v = np.array([self.values[i] for i in r])
u = np.array([self.uncertainties[i] for i in r])

return t, v, u

0 comments on commit d105de2

Please sign in to comment.