15 minutes · Browser simulation, optional Python script
Real-time BCI: streaming and neurofeedback
Follow a simulated live EEG signal, compute alpha power in a sliding window, and see why faster feedback is also noisier.
In offline analysis, the whole recording is available. In real time, the program only knows the recent past. It has to decide what to show before the next data arrive.
The closed loop
1. Acquisition
The amplifier digitizes the signal and sends samples in small chunks, for example a few tens of milliseconds at a time.
2. Buffer and window
The program keeps the last few seconds in memory. Each calculation uses a window, for example the last 2 seconds.
3. Feature
One number is extracted from the window. Here, the share of 4–30 Hz power that falls in the alpha band (8–12 Hz).
4. Feedback
The number becomes a bar, a sound, or an action. The person perceives it and may adjust their state, which closes the loop.
5. Latency
Every step adds delay. A window of W seconds mostly describes what happened about W/2 seconds ago.
Things to try
- Start the stream with a 2 s window. Switch between simulated eyes open and eyes closed. Note the response time shown.
- Switch to a 0.5 s window. The feedback reacts faster. How often does it cross the threshold while the eyes stay open?
- Try 4 s. The bar is steadier, but how long does it now take to cross the threshold?
- Change the update rate. Does updating more often make the estimate more accurate, or only smoother?
Simulated EEG stream and feedback
Simulated signalNo headset is used. The browser generates a simulated occipital channel at 128 Hz: a 10 Hz rhythm whose amplitude rises when the eyes are "closed", plus background activity and random noise. The values do not come from a real person.
Analyzed window: 2 s
FeedbackFilling the buffer…
Alpha share (8–12 Hz out of 4–30 Hz)–
- Expected delay
- ≈ 1.13 s
- W/2 for the window, plus on average half the update interval.
- Measured response time
- –
- Change the state to measure
- Variability
- –
- Standard deviation of the alpha share over the last 5 seconds in the same state. The larger it is, the more the bar jumps.
How the calculation works
At each update, the program takes the last W seconds from the buffer, removes the mean, applies a Hann window, and computes a periodogram. The alpha share is the power between 8 and 12 Hz divided by the power between 4 and 30 Hz. A short window gives coarse frequency resolution (1/W Hz) and a noisier estimate.
What Lab Streaming Layer (LSL) does
LSL is an open-source library that moves time series between programs on a local network. Research labs use it widely to connect acquisition devices, analysis software, and recorders.
- Streams
- A device or program publishes a stream (an "outlet") with a name, a type (EEG, markers, and so on), a channel count, and a sampling rate. Other programs find it on the network and open it as an "inlet".
- Timestamps
- Each sample or chunk gets a timestamp from a local monotonic clock. That clock counts seconds from an arbitrary starting point. It does not tell the time of day.
- Clock synchronization
- LSL measures the offset between the sender’s and receiver’s clocks by exchanging short UDP packets, similar to NTP. The receiver gets this offset with time_correction().
- XDF recording
- LabRecorder saves all selected streams to an XDF file, with the raw timestamps and every clock offset measurement. pyxdf later uses those measurements to align the streams.
The same loop in Python with LSL
The script creates an LSL outlet that streams simulated EEG, then an inlet that computes the alpha share in a sliding window and prints a bar in the terminal. It runs on one computer with no device. The pylsl package includes the liblsl library.
Download the Python scriptInstall, then run the demo (outlet and inlet in one process):
pip install pylsl numpy
python realtime-alpha-lsl.py demo --seconds 30Or in two terminals, to see two programs talk over LSL:
# terminal 1
python realtime-alpha-lsl.py outlet
# terminal 2
python realtime-alpha-lsl.py inlet --window 2 --update 0.25Connect a real device
The inlet accepts any LSL stream of type EEG. Pick a channel index with --channel.
- Muse ↗
muse-lsl (Muse 2, Muse S, Muse 2016): pip install muselsl, then muselsl stream. The Muse has no occipital electrode.
- OpenBCI ↗
In the OpenBCI GUI, the Networking widget can send data over LSL. You can also go through BrainFlow.
- PiEEG ↗
The project lists LSL output from PiEEG Server and a BrainFlow board ID. Follow its safety notes: battery power while electrodes are on a person.
- BrainFlow ↗
The script’s outlet mode can forward a BrainFlow board with --brainflow-board. ID -1 is BrainFlow’s synthetic board, for testing without a device.
To record a session, run LabRecorder next to the inlet, then open the XDF file with pyxdf. LabRecorder ↗ · pyxdf ↗ · pylsl ↗
What is known about neurofeedback
Neurofeedback is an active research area. It is used to study how people learn to modulate their brain activity and what that control changes in behavior.
Its effectiveness as a treatment is not established in general. For example, a meta-analysis of 38 randomized trials in ADHD found no significant improvement in symptoms when raters were probably unaware of the treatment received.
Researchers in the field published a checklist (CRED-nf) for designing and reporting studies better, including control groups and measures of learning.
This lab and script are for understanding the technique. They are not a medical device or a validated protocol.
Further reading
What to take away
A short window reacts quickly, but its estimate varies a lot. A long window is steady, but the feedback arrives late. Updating more often makes the display smoother without removing the delay built into the window. Every real-time system picks a trade-off for its task.