buffer v1

This commit is contained in:
2026-06-08 17:06:27 +08:00
parent 509fc5a1d7
commit d741e3548f
3 changed files with 21 additions and 20 deletions

View File

@@ -53,21 +53,21 @@ class FilterRingBuffer:
:param count: 读取点数
:return: np.ndarray, shape=(n_chan, count)
"""
with self.lock:
count = min(count, self.total_samples)
if count == 0:
return np.zeros((self.n_chan, 0))
# with self.lock:
count = min(count, self.total_samples)
if count == 0:
return np.zeros((self.n_chan, 0))
# 环形读取end是当前写入指针最新数据的下一位start是end - count
end = self.current_ptr
start = end - count
if start >= 0:
return self.buffer[:, start:end].copy()
else:
# 跨环形边界:前半部分从缓存末尾取,后半部分从开头取
part1 = self.buffer[:, start:] # start为负等价于n_points + start
part2 = self.buffer[:, :end]
return np.concatenate((part1, part2), axis=1)
# 环形读取end是当前写入指针最新数据的下一位start是end - count
end = self.current_ptr
start = end - count
if start >= 0:
return self.buffer[:, start:end].copy()
else:
# 跨环形边界:前半部分从缓存末尾取,后半部分从开头取
part1 = self.buffer[:, start:] # start为负等价于n_points + start
part2 = self.buffer[:, :end]
return np.concatenate((part1, part2), axis=1)
def get_latest_n_points(self, n):
"""
@@ -75,10 +75,9 @@ class FilterRingBuffer:
:param n: 点数
:return: np.ndarray, shape=(n_chan, n) | None数据不足时
"""
with self.lock:
if self.total_samples < n:
return None
return self.getData(n)
if self.total_samples < n:
return None
return self.getData(n)
def GetDataLenCount(self):
"""获取当前缓存总点数(兼容原有接口)"""
@@ -173,12 +172,14 @@ class SlidingFilter(threading.Thread):
try:
# 获取最新的3秒窗口数据
window_data = self.ring_buffer.get_latest_n_points(self.window_size)
algo_log(f"获取到{window_data.shape}数据", level='debug')
if window_data is None:
algo_log(f"缓存数据不足,当前缓存{self.ring_buffer.GetDataLenCount()}点,需{self.window_size}", level='debug')
continue
# 滤波并提取无边界效应的200ms数据
filtered_data = self._filter_window_data(window_data)
algo_log(f"滤波后{filtered_data.shape}数据", level='debug')
# 回调返回结果(外部可处理)
if self.filter_result_callback is not None:

View File

@@ -268,7 +268,7 @@ class zmqServer(threading.Thread):
# -------------------------- 数据端口消息处理 --------------------------
def _handle_data_message(self, frames):
"""处理8100端口二进制脑电数据消息"""
algo_log(f"收到数据帧,总帧数:{len(frames)}", level="DEBUG", record_once=False)
algo_log(f"收到数据帧,总帧数:{len(frames)}", level="DEBUG", record_once=True)
# 然后再进行解析
if len(frames) == 4:
# 你的上位机格式