UDP epoching LSL stream
I'm running LSL with the OpenBCI GUI, and the stimulation software will send a UDP signal whenever a trial starts. The LSL is outputting a filtered signal between 8-30Hz for 8 EEG channels
I want to receive the UDP signal and then start the while loop to collect data, however, it doesn't work.
Below I have shown the graph from doing 3 trials, and you can see that:

- The spike clearly doesn't occur in the same place (plot starts from t=0), I am expecting the spike to occur shortly after zero because I am manually tapping an electrode at the beginning of each trial
- The magnitude is totally wrong, on the GUI it says my signal should be between +-6uv, and the peak should be around 150uv when I tap it
Below is also the code I am using, would someone be able to help with this UDP synchronisation?
import serial, scipy
import numpy as np
from pylsl import StreamInlet, resolve_stream
import socket
import os
def process_eeg_block(time_data, eeg_data, title, trial_n):
print(time_data.shape, eeg_data.shape)
np.savetxt("MI_trials/"+title+"_"+str(trial_n)+".csv", eeg_data, delimiter=",")
return
def read_eeg_data(window_size, Fs, Nch, trial_n):
try:
# first resolve an EEG stream on the lab network
print("looking for an EEG stream...")
streams = resolve_stream('type', 'EEG')
# create a new inlet to read from the stream
inlet = StreamInlet(streams[0])
#store batch data
samples_per_window = Fs*window_size//1000
eeg_data = np.zeros((samples_per_window, Nch))
time_data = np.zeros(samples_per_window)
eeg_sample, start_time = inlet.pull_sample()
eeg_data[0,:] = eeg_sample
time_data[0] = start_time
n_samples = 1
#record
while True:
if UDPClientSocket.recv is not None:
msgFromServer, addr = UDPClientSocket.recvfrom(bufferSize)
msg = msgFromServer.decode(encoding = 'UTF-8',errors = 'strict')
# print("Direction: ", msg)
while True:
# get a new sample
eeg_sample, timestamp = inlet.pull_sample()
#store samples
eeg_data[n_samples,:] = eeg_sample
time_data[n_samples] = timestamp
n_samples += 1
if n_samples >= samples_per_window:
print((timestamp-start_time)*1000)
time_window = time_data[:]
eeg_window = eeg_data[:,:]
process_eeg_block(time_window, eeg_window, msg, trial_n)
start_time = timestamp
trial_n += 1
print("Stop")
n_samples = 0
break
except KeyboardInterrupt as e:
print("Ending program")
raise e
WINDOW_SIZE_MS = 5500
SAMPLE_RATE_HZ = 250
N_CH = 8
trial_n = 1
serverAddressPort = ("127.0.0.1", 12345)
bufferSize = 1024
# Create a UDP socket at client side
UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
UDPClientSocket.bind(serverAddressPort)
if __name__ == "__main__":
read_eeg_data(WINDOW_SIZE_MS, SAMPLE_RATE_HZ, N_CH, trial_n)
I know manual synchronisation is better but there must be a way to do it with software like often OpenVibe does?
Thank you!!!