-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLSL-stream
43 lines (38 loc) · 1.48 KB
/
LSL-stream
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Automatic acquisition of LSL stream
#In automatic mode, stream object acquires new chunks of samples at a regular interval
import uuid
from time import sleep
from mne_lsl.player import PlayerLSL
from mne_lsl.stream import StreamLSL
#Creating a LSL stream from loaded file
source_id = uuid.uuid4().hex
player = PlayerLSL(raw, chunk_size=200, annotations=True)
player.start()
player.info
#Acquiring information and creating interval
sfreq = player.info["sfreq"]
chunk_size = player.chunk_size
interval = chunk_size / sfreq # in seconds
print(f"Interval between 2 push operations: {interval} seconds.")
#Streaming the chunks
mne_lsl.lsl.resolve_streams()
stream = StreamLSL(bufsize=2, name=source_id).connect(acquisition_delay=0.1)
sleep(2) # waiting for new samples
print(f"New samples acquired: {stream.n_new_samples}")
stream.disconnect()
#Visualizing/Plotting the LSL stream created
#Run this code before stream.disconnect
picks = ('Ch1', 'Ch2', 'Ch3') # channel selection
f, ax = plt.subplots(3, 1, sharex=True, constrained_layout=True)
for _ in range(3): # acquire 3 separate window
# figure how many new samples are available, in seconds
winsize = stream.n_new_samples / stream.info["sfreq"]
# retrieve and plot data
data, ts = stream.get_data(winsize, picks=picks)
for k, data_channel in enumerate(data):
ax[k].plot(ts, data_channel)
time.sleep(0.5)
for k, ch in enumerate(picks):
ax[k].set_title(f"EEG {ch}")
ax[-1].set_xlabel("Timestamp (LSL time)")
plt.show()