Create a basic filtered file out of OpenBCI txt file

Hello codemates:

I have a txt file created by OpenBCI EEG software. Now I want to analyze it with MNE. Does anyone know a basic sample where I can understand the first steps to read it, filter the seconds I am interested in, filter the frequencies and record a new txt filtered file to analyze it ?

Thanks in advance !

You need to read data contained in the CSV file into a NumPy array. I recommend pd.read_csv(), then you can extract the data as a NumPy array with DataFrame.to_numpy(). Using this array, you can then create a Raw object from scratch (see here).

1 Like

thank you. I did it, but it shows me an error “ValueError: could not convert string ‘0.0,’ to int32 at row 0, column 1.”

Here is my code:

import mne
import numpy as np
import pandas as pd

# Define the file path and read the raw data
file_path = 'eeg01.txt'
data, times = pd.read_csv(file_path, unpack=True, delimiter=',', usecols=range(np.loadtxt(file_path, dtype=int).shape[1] - 1))

sfreq = 250  # Set the sampling frequency to 250 Hz, as per OpenBCI documentation

# Remove the extra comma from times
times = times[:-1]

raw = mne.io.RawArray(data, times, info={'sfreq': sfreq, 'ch_names': ['EEG_01', 'EEG_02', 'EEG_03', 'EEG_04', 'EEG_05']})

# Set up the filtering parameters
low_cutoff = 1  # Hz, low cutoff frequency
high_cutoff = 40  # Hz, high cutoff frequency
trans_bandwidth = 'butter'  # IIR filter type, can also be 'cheby2' or 'fir'
filter_order = 4  # order of the filter

# Apply the filter
raw.filter(l_freq=low_cutoff, h_freq=high_cutoff, filter_type=trans_bandwidth, fir_design='firwin',
           fir_window='hann', fir_phase='zero', iir_params=None, iir_btype=None, iir_q=None,
           verbose=None, picks=None, n_jobs=1, **kwargs)

# Define the time period of interest
time_start = 61  # seconds
time_end = 308  # seconds
tmin, tmax = time_start, time_end

# Extract the time period of interest
filtered_data = raw[tmin:tmax]

# Save the filtered data to a new text file
new_file_path = 'filtered_openbci_data.txt'
filtered_data.save(new_file_path, overwrite=True, format='raw_data_frame')

Any idea on what’s going on ? thanks in advance,

The line where you import the CSV does not look correct:

data, times = pd.read_csv(file_path, unpack=True, delimiter=',', usecols=range(np.loadtxt(file_path, dtype=int).shape[1] - 1))

Please read the documentation of this function; unpack does not exist and usecols=... doesn’t make a lot of sense. If you want to exclude the last column, I’d read the entire data frame and drop the column afterwards.

Thanks.

I changed that line into:

data, times = pd.read_csv(file_path, delimiter=‘,’)

But now the error message is: “ValueError: too many values to unpack (expected 2)”

I am really puzzled since yesterday…

The function only returns one value, not two.

sorry, don’t get you. Thanks anyway. Do you know if there is any basic example where I can study the connection ? I 't would be easier

You need to change this to

data = pd.read_csv(file_path, delimiter=‘,’)
1 Like