Data collection with OpenBci

I started to collect data with OpenBci through Jupyter Notebook

BoardShim.enable_dev_board_logger()
# use synthetic board for demo
params = BrainFlowInputParams()
params.serial_port = "COM5"
board = BoardShim(2, params)
board.prepare_session()
board.start_stream()
time.sleep(45)
data = board.get_board_data()
board.stop_stream()
board.release_session()
eeg_channels = BoardShim.get_eeg_channels(BoardIds.SYNTHETIC_BOARD.value)
eeg_data = data[eeg_channels, :]
eeg_data = eeg_data / 1000000  # BrainFlow returns uV, convert to V for MNE

# Creating MNE objects from brainflow data arrays
ch_types = ['eeg'] * len(eeg_channels)
ch_names = BoardShim.get_eeg_names(BoardIds.SYNTHETIC_BOARD.value)
sfreq = BoardShim.get_sampling_rate(BoardIds.SYNTHETIC_BOARD.value)
info = mne.create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_types)
raw = mne.io.RawArray(eeg_data, info)
raw.crop(tmin=3,tmax=18)
# raw.plot()
raw.export('SR04.edf', fmt='auto',overwrite=True)\

The problem is that the length of the data is half of the time.sleep. For example by following time sleep in my code

time.sleep(45)

I will get data of length 45/2=22.5, but the time that passed is actually 45 seconds.
Could you help me with where is the problem?

Good question but seems like more of an issue with OpenBCI and this is the MNE-Python forum, you might have better luck asking OpenBCI.

One thing that comes to mind is that your sampling rate is off (double what it should be) but you’re getting it from the OpenBCI API so that seems like the right way to do it. Otherwise, if you are only getting half the data, by process of elimination, it probably has to do with your BoardShim object. You might want to try running the script in a terminal to make sure it’s not something odd with Jupyter Notebook.

1 Like

Hi @alexrockhill,
Thanks for your reply.
In OpenBci documentation, it is mentioned that the Sampling Frequency is 125 Hz. I tried to load the data that I have captured through JupyterNotebook, both in Matlab and also in mne; the sampling frequency in Jupyternotebook is about 250 Hz, with a lowpass filter of 125 Hz.
Capture33

Also, in Matlab, in 1 second, it showed we have 250 NumSamples but with the Prefilter of LowPass 125 Hz, as you can see in the following snapshot.

Now, I am totally confused. What is my sampling frequency?

Thanks in advance,
Sincerely
Soroush

This is where you’d really need to talk to someone at OpenBCI to resolve the conflicting sampling frequencies. I don’t contribute to that project or know much about it so it’s hard to give you much advice on which one to go with other than my guess is that it’s 125 Hz since that matches the length of your data but if I were you I would confirm before possibly making a mistake in your analysis.

1 Like

To address this issue, you can modify your code to loop and continuously check for lead data enrichment until the desired duration is reached. Here’s an updated version of your code with a loop to wait for the specified duration:
import time
import mne
from brainflow.board_shim import BoardShim, BrainFlowInputParams, BoardIds

BoardShim.enable_dev_board_logger()

use synthetic board for demo

params = BrainFlowInputParams()
params.serial_port = “COM5”
board = BoardShim(2, params)
board.prepare_session()
board.start_stream()

desired_duration = 45 # seconds
start_time = time.time()

while (time.time() - start_time) < desired_duration:
time.sleep(1) # sleep for 1 second before checking again

data = board.get_board_data()
board.stop_stream()
board.release_session()

eeg_channels = BoardShim.get_eeg_channels(BoardIds.SYNTHETIC_BOARD.value)
eeg_data = data[eeg_channels, :]
eeg_data = eeg_data / 1000000 # BrainFlow returns uV, convert to V for MNE

Creating MNE objects from brainflow data arrays

ch_types = [‘eeg’] * len(eeg_channels)
ch_names = BoardShim.get_eeg_names(BoardIds.SYNTHETIC_BOARD.value)
sfreq = BoardShim.get_sampling_rate(BoardIds.SYNTHETIC_BOARD.value)
info = mne.create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_types)
raw = mne.io.RawArray(eeg_data, info)
raw.crop(tmin=3, tmax=18)

raw.plot()

raw.export(‘SR04.edf’, fmt=‘auto’, overwrite=True)

This modification ensures that you wait until the desired duration has passed by checking the elapsed time in a loop. It should give you data for the entire 45-second duration. Adjust the desired_duration variable as needed.