Compare commits

...

4 Commits

4 changed files with 64 additions and 71 deletions

View File

@ -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],
)
),
)

View File

@ -28,6 +28,7 @@ class SinProducer:
AMPLITUDE = 2
FREQUENCY = 10
self.stop = False
log.debug("producing Sin")
start_time = time.time()
t = 0
@ -39,9 +40,9 @@ class SinProducer:
end_time = time.time()
data = self.buffer.get_all()
log.debug(f"duration sinus {end_time-start_time}")
log.debug(f"Stimulation time {t}")
# data = self.buffer.get_all()
# log.debug(data.shape[0])
# log.debug(data.shape[0] / self.buffer.samplerate)
# plt.plot(np.arange(data.size) / self.buffer.samplerate, data)

View File

@ -13,10 +13,6 @@ from PyQt6.QtWidgets import (
QStatusBar,
)
from pglive.sources.data_connector import DataConnector
from pglive.sources.live_plot import LiveLinePlot
from pglive.sources.live_plot_widget import LivePlotWidget
import uldaq
import nixio as nix
import pyqtgraph as pg
@ -78,8 +74,12 @@ class PyRelacs(QMainWindow):
widget.setLayout(layout)
self.setCentralWidget(widget)
SAMPLERATE = 1_000
BUFFERSIZE = 1_0000
SAMPLERATE = 40_000
start = time.time()
BUFFERSIZE = SAMPLERATE * 10 * 60
end = time.time()
log.debug(f"Buffer allocation took {end - start}")
self.buffer = CircBuffer(size=BUFFERSIZE, samplerate=SAMPLERATE)
# self.connect_dac()
@ -222,7 +222,7 @@ class PyRelacs(QMainWindow):
def stop_recording(self):
self.add_to_textfield("pressed")
self._stop_recording.setEnabled(False)
# self._stop_recording.setEnabled(False)
self.sin_producer.stop_request()
self.continously_plot.stop_plotting()

View File

@ -19,76 +19,48 @@ class Continously:
self.figure.setBackground("w")
self.daq_plot = self.figure.addPlot(row=0, col=0)
pen = pg.mkPen("red")
# self.time = np.zeros(self.buffer.size)
# self.data = np.zeros(self.buffer.size)
self.time = np.zeros(self.buffer.size)
self.data = np.zeros(self.buffer.size)
self.line = self.daq_plot.plot(
# self.time,
[0],
self.time,
self.data,
pen=pen,
setCliptoView=True,
symbol="o",
# symbol="o",
)
# self.item = 0
# while self.buffer.totalcount() < 3_000_000:
# log.debug(self.buffer.totalcount())
# self.time = np.roll(self.time, -1)
# self.data = np.roll(self.data, -1)
# try:
# item, time_index = self.buffer.get(self.item)
# except IndexError:
# item = 0
# time_index = 0
# self.time[-1] = time_index
# self.data[-1] = item
# self.line.setData(self.time, self.data)
# self.item += 1
self.plot_index = 0
self.CHUNK_PLOT = 1000
# self.plot_index = 0
self.CHUNK_PLOT = 1_000
self.timer = QTimer()
self.timer.setInterval(200)
self.timer.timeout.connect(self.update_plot)
self.timer.start()
# self.update_plot()
def update_plot(self):
log.debug(self.buffer.totalcount())
if self.buffer.totalcount() > self.CHUNK_PLOT:
log.debug(self.buffer.totalcount())
try:
# item, time_index = self.buffer.get(self.buffer.write_index() - 1)
items = self.buffer.read(
self.buffer.write_index() - self.CHUNK_PLOT, count=self.CHUNK_PLOT
times, items = self.buffer.read(
self.buffer.write_index() - self.CHUNK_PLOT, extend=self.CHUNK_PLOT
)
# times = (np.arange(len(items)) / self.buffer.samplerate) + self.time[-1]
except IndexError:
item = 0
except IndexError as e:
items = np.zeros(self.CHUNK_PLOT)
times = np.zeros(self.CHUNK_PLOT)
# log.debug("Stopping the timer")
# self.timer.stop()
time_index = 0
log.debug("No Data Available")
log.debug(f"Index Error {e}")
# self.time = np.roll(self.time, -len(items))
# self.data = np.roll(self.data, -len(items))
#
# # self.time[-len(times) :] = times
# self.time[-len(times) :] = times
# self.data[-len(items) :] = items
self.line.setData(
# self.time,
times,
items,
)
self.plot_index += len(items)
# self.plot_index += len(items)
else:
# self.timer.stop()
# embed()
# exit()
pass
def stop_plotting(self):