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( self.__buffer = np.zeros(
(channels, size), dtype=np.double (channels, size), dtype=np.double
) # or dtype of your choice ) # 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.__index = [0 for i in range(channels)]
self.__is_full = [False for i in range(channels)] self.__is_full = [False for i in range(channels)]
self.__totalcount = [0 for i in range(channels)] self.__totalcount = [0 for i in range(channels)]
self.__overflows = [0 for i in range(channels)] self.__overflows = [0 for i in range(channels)]
self.__read_increment = samplerate * 0.1
@property @property
def size(self): def size(self):
@ -42,8 +43,8 @@ class CircBuffer:
self.__buffer[channel, self.write_index(channel)] = item self.__buffer[channel, self.write_index(channel)] = item
self.__index[channel] = (self.write_index(channel) + 1) % self.__size self.__index[channel] = (self.write_index(channel) + 1) % self.__size
self.__totalcount[channel] += 1 self.__totalcount[channel] += 1
self.__time[self.write_index(channel=0)] = ( self.__time[channel, self.write_index(channel)] = (
self.__time[self.write_index(channel=0) - 1] + 1 / self.__samplereate self.__time[channel, self.write_index(channel) - 1] + 1 / self.__samplereate
) )
if self.__index[channel] == 0: if self.__index[channel] == 0:
self.__is_full[channel] = True self.__is_full[channel] = True
@ -128,14 +129,18 @@ class CircBuffer:
f"Invalid index {index} on ring buffer for channel{channel}" 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""" """Reads a numpy array from buffer"""
if start < 0 or count < 0: if extend < 0:
raise IndexError( raise IndexError(f"Invalid extend ({extend}) for channel {channel}")
f"Invalid start ({start}) or count ({count}) 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)) return np.array(self.get(start, channel))
vs, vc = self.valid_range(channel) vs, vc = self.valid_range(channel)
@ -147,17 +152,32 @@ class CircBuffer:
raise IndexError( raise IndexError(
f"Invalid start index {start} for buffer with size {self.size}" 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: if extend > vc:
return self.__buffer[channel, start : start + count] 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: else:
return np.concatenate( return (
( np.concatenate(
self.__buffer[channel, start:], (
self.__buffer[channel, : count - self.size + start], 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 AMPLITUDE = 2
FREQUENCY = 10 FREQUENCY = 10
self.stop = False
log.debug("producing Sin") log.debug("producing Sin")
start_time = time.time() start_time = time.time()
t = 0 t = 0
@ -39,9 +40,9 @@ class SinProducer:
end_time = time.time() end_time = time.time()
data = self.buffer.get_all()
log.debug(f"duration sinus {end_time-start_time}") log.debug(f"duration sinus {end_time-start_time}")
log.debug(f"Stimulation time {t}") log.debug(f"Stimulation time {t}")
# data = self.buffer.get_all()
# log.debug(data.shape[0]) # log.debug(data.shape[0])
# log.debug(data.shape[0] / self.buffer.samplerate) # log.debug(data.shape[0] / self.buffer.samplerate)
# plt.plot(np.arange(data.size) / self.buffer.samplerate, data) # plt.plot(np.arange(data.size) / self.buffer.samplerate, data)

View File

@ -13,10 +13,6 @@ from PyQt6.QtWidgets import (
QStatusBar, 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 uldaq
import nixio as nix import nixio as nix
import pyqtgraph as pg import pyqtgraph as pg
@ -78,8 +74,12 @@ class PyRelacs(QMainWindow):
widget.setLayout(layout) widget.setLayout(layout)
self.setCentralWidget(widget) self.setCentralWidget(widget)
SAMPLERATE = 1_000 SAMPLERATE = 40_000
BUFFERSIZE = 1_0000 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.buffer = CircBuffer(size=BUFFERSIZE, samplerate=SAMPLERATE)
# self.connect_dac() # self.connect_dac()
@ -222,7 +222,7 @@ class PyRelacs(QMainWindow):
def stop_recording(self): def stop_recording(self):
self.add_to_textfield("pressed") self.add_to_textfield("pressed")
self._stop_recording.setEnabled(False) # self._stop_recording.setEnabled(False)
self.sin_producer.stop_request() self.sin_producer.stop_request()
self.continously_plot.stop_plotting() self.continously_plot.stop_plotting()

View File

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