EEGLAB Tutorial: Import Data

EEGLAB can be used for the analysis and visualization of EEG datasets recorded using OpenBCI hardware and software. EEGLAB can work with a variety of different file types, including those that are exported from the OpenBCI GUI, as we saw in the previous post.

Get your data ready

EEG Data File

EEG data can be stored ASCII files (often saved with the “.txt” extension). The OpenBCI Processing GUI saves data in text or comma separated value (csv) files, which are output into the “SavedData” directory within the OpenBCI_Processing directory.

Import the CSV file as a MATLAB matrix using the following command:

> data = csvread('.../directory/yourfile.txt', row offset, column offset)

For example:

> data = csvread('.../OpenBCI-RAW-__.txt', 5, 1);

Row offset is the number of rows in your txt file before the start of your EEG data (in the current version of the OpenBCI GUI, there are 5 commented lines before the start of the data, so the offset should be 5 to make the matrix start on line 6). Column offset skips the sample number column.

You can also write a function to open files and avoid writing and changing the name of the file every time. To do so, you must write the code showed below and save it as a “.m” file.

function [data] = open_files()
 % Prompt user for filename
   [fname, fdir] = uigetfile( ...
   {'*.csv', 'CSV Files (*.csv)'; ...
   '*.xlsx', 'Excel Files (*.xlsx)'; ...
   '*.txt*', 'Text Files (*.txt*)'}, ...
   'Pick a file');

 % Create fully-formed filename as a string
   filename = fullfile(fdir, fname);

 % Check that file exists
   assert(exist(filename, 'file') == 2, '%s does not exist.', filename);

 % Read in the data, skipping the 5 first rows
   data = csvread(filename,5,0);
end

You can call the function from the Command Window with the following line (Remember to save both scripts in the same folder):

[data] = open_files();

If you are using the OpenBCI V3 board with accelerometer data and aux data, you might want to separate the last three channels of your data before you send it into EEGLAB to only work with the EEG data and the accelerometer data separately. To separate the different data with the respective variables, enter the command:

% Create general variables
EEGData = data(:,2:9);
AuxData = data(:,10:12);
time = (0:4:length(eegdata))';  % Time vector
1.5 - eegdata
Figure 1. Channels data should be saved in columns, not in rows.

Channel Location File

Channel locations are useful for plotting EEG scalp maps in 2-D or 3-D format. OpenBCI uses the standard 10-20 format for the 8 and 16 channel models, which can be found within these .ced files: 8 channel and 16 channel. You can then import channel data by click “Browse” next to “Channel location file or info” and locating the OpenBCI .ced file you downloaded.

1.5 - chlocs
Figure 2. Channel location file. All the elements are separated by tabs.

If you need to edit the channel locations, I would suggest editing either “eeglab_chan32.locs” located in “\EEGLAB\sample_data” or “Standard-10-20-Cap25.ced” located in “\EEGLAB\sample_locs”. You should use any text editors such as WordPad, NotePad++, Atom… Whichever works better for you and edit the files. Remember to save it as a “.ced” file, and not a “.txt” file.

Number labels theta radius X Y Z sph_theta sph_phi sph_radius 
1 Fp1 -18 0.511 0.95 0.309 -0.0349 18 -2 1 
2 Fp2 18 0.511 0.95 -0.309 -0.0349 -18 -2 1 
3 F9 -55 0.639 0.52 0.742 -0.423 55 -25 1 
4 F7 -54 0.511 0.587 0.809 -0.0349 54 -2 1 
5 F3 -39 0.333 0.673 0.545 0.5 39 30 1 
6 Fz 0 0.256 0.719 -0 0.695 -0 44 1 
7 F4 39 0.333 0.673 -0.545 0.5 -39 30 1 
8 F8 54 0.511 0.587 -0.809 -0.0349 -54 -2 1 
9 F10 55 0.639 0.52 -0.742 -0.423 -55 -25 1 
10 T9 -90 0.639 5.55e-17 0.906 -0.423 90 -25 1 
11 T7 -90 0.511 6.12e-17 0.999 -0.0349 90 -2 1 
12 C3 -90 0.256 4.4e-17 0.719 0.695 90 44 1 
13 Cz 90 0 3.75e-33 -6.12e-17 1 -90 90 1 
14 C4 90 0.256 4.4e-17 -0.719 0.695 -90 44 1 
15 T8 90 0.511 6.12e-17 -0.999 -0.0349 -90 -2 1 
16 T10 90 0.639 5.55e-17 -0.906 -0.423 -90 -25 1 
17 P9 -126 0.639 -0.533 0.733 -0.423 126 -25 1 
18 P7 -126 0.511 -0.587 0.809 -0.0349 126 -2 1 
19 P3 -141 0.333 -0.673 0.545 0.5 141 30 1 
20 Pz 180 0.256 -0.719 -8.81e-17 0.695 -180 44 1 
21 P4 141 0.333 -0.673 -0.545 0.5 -141 30 1 
22 P8 126 0.511 -0.587 -0.809 -0.0349 -126 -2 1 
23 P10 126 0.639 -0.533 -0.733 -0.423 -126 -25 1 
24 O1 -162 0.511 -0.95 0.309 -0.0349 162 -2 1 
25 O2 162 0.511 -0.95 -0.309 -0.0349 -162 -2 1

Start with EEGLAB

If EEGLAB isn’t already running, enter “eeglab” into the Matlab command line to start the program. Check that you are in the correct path before you run it.

1 - eeglab
Figure 4. MATLAB window with “eeglab” command executed.

The main EEGLAB window will pop-up on your screen:

0 - eeglab
Figure 5. The main window of EEGLAB.

Import your matrix into EEGLAB using the EEGLAB GUI: File -> Import Data -> Using EEGLAB functions and plugins -> From ASCII/float file or Matlab array.

In the pop-up window that appears, enter information about the data set. Select “Matlab variable”, and enter the name of the variable where your matrix is stored. Type the Data Sampling rate (it should be commented in at the top of the “txt” file – usually 250 Hz by default in the OpenBCI GUI). The other fields can be left at default, and EEGLAB will automatically fill in the information from the data set.

2 - data_eeglab
Figure 6. Import EEG data and channel locations.

Congratulations! The EGE data and Channel Locations are now imported into EEGLAB, and you can perform a variety of data analysis on the data and visualize it.

Plot your data into EEGLAB using the EEGLAB GUI: Plot -> Channel data (scroll). By default, EEGLAB has a 5 seconds window length (x-axis). If you want to change it, you must select Settings -> time range to display in the figure window and type the new window length (s).

plotdata3.png
Figure 7. Plot EEG data and changing window length(s).

Besides, you can also change the scale of the plot by playing a little bit with the “+” and “-” in the bottom of the plot. I would suggest a scale of 50 or 100. Thus, you will have better perspective and visualization of EEG data. This should be the result:

eegdata.png
Figure 8. EEG data visualization with a scale factor of 100.

References

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s