We will talk about how MATLAB can be useful for EEG signal analysis or EEG signal processing. So, initially I will tell you one particular, I will give you an introduction about a tool box named EEGLAB, how it is being useful and what are the functions which you can do for different recorded EEG files. Then, we will see that this EEG file is recorded in different formats across different system. So, we will see a couple of examples of how different files can be imported or loaded in MATLAB using scripting and as well as using a GUI of EEG lab. So, let us talk about EEGLAB a bit before I will demonstrate some of the very basic things about EEG lab.
So, EEGLAB is a prerequisite for any kind of EEG and ERP processing using MATLAB. There are some packages and functions which have been already made, so you can directly use that function and you can do your analysis, otherwise, it will take a lot of time again, to generate the same functions. As always, you can make use of different combinations of function and create a plugin for specific applications, but all the functions, documented functions or the included function in EEGLAB makes EEG signal processing very, very simpler. So, you can have a look. I will show you when I will demonstrate EEGLAB basic functions and plugins and all the things I will try to include. So, it is supposed both. Like in PsychoPy, we have seen that we were generated stimuli using GUI because at that time, it was like you have to generate stimuli mostly once for experimental twice. But whereas in this case, when you record across multiple subjects, instead of GUI a scripting is advisable because you do not have to repeat the same procedure again and again, you can act automate this stuff using scripting.
So it supports both basically. How you can generate a particular script for GUI operation which you have made? You know, and one particular scenario where the GUI is better or scripting, all this things I will try to cover. Further, we will see that scripting as I mentioned, it is advisable, one last important thing is plugins. So, in the demonstration, I will show you that there are many plugins which are already available with thousands of downloads and various applications, many of them are still in progress and getting updated with more and more features. So first, we will see the demonstration or introduction to EEGLAB. I have included from scratch. So how from downloading EEGLAB to what are the basic things you can do with a sample data set. All this thing I have tried to cover so let us see first and let us see how EEGLAB works or basic introduction of EEGLAB.
So, let us quickly see how to download EEGLAB. You can search it and it will redirect to Swartz Center for Computational Neuroscience site. Here you can add your basic details, your email id, your name, and your area of research and submit it. You should be able to download the latest version of EEGLAB. So, once you download EEGLAB, it should look like this. This is the latest 2020 version of EEGLAB. It consist of, functions which you are going to use for signal processing. There are various plugins, sample data and sample locations files which you are going to use. So, if I open this function folder, these are the mostly used function for signal processing analysis, most of them are used for scripting, all the pop functions are used for scripting. And further, this is for GUI. There are signal processing functions, statistical functions, and all the different functions available. This is very important thing that you see the different plugins here. So, Clean Raw data is basically a plugin to process or clear your whole EEG signals. ERPLAB generally is for event related potential extraction to create different beans for particular events and all. So, neuro-scan is required to import some file which has been recorded in tune called Euroscep. So, all these different plugins and all are there.
One interesting thing I would like to recommend all of you is that you can go through the list of plugins of MATLAB. Very important, all these plugins, if you install and if you can study, it will make life easier for EEG signal processing. So, I would request all of you to at least take a look at the plugins which is useful for their analysis or their applications. So, coming back to EEGLAB finally. EEGLAB is a toolbox which has been used specifically for EEG processing in MATLAB. So, I have already added this into my paths folder. So I will add this thing into path and see if I can run the EEGLAB. So once the downloaded EEGLAB package is in path, you have to write a command EEGLAB, it should open EEGLAB pursuit. In EEGLAB you can process to it in two manners, one is using the scripting, like typing a command and get the desired work done, whereas in other way, it is using GUI. So I will show you both the ways. In this newer version as I have downloaded ERPLAB, it is showing ERPLAB, ERP sets everything. But generally, it should start with, so the acquisition of your input data files generated from particular system should start with file menu. Let us import some of the data. Before importing, I would like to tell, I was telling two things, one is GUI, other one is scripting. So, we will start with GUI. We will go to scripting. We will see how there is a way from GUI to scripting and some tricks.
So first, let me import one of the sample data set which I have. Let us import EDF file. So, I will go to this from EDF file. This is a sample EDF file here. So I will view this file as an input, it will ask me a few questions, like if I want to see all the channels, if I want to see the entire data recorded throughout the complete data stream and all. I will for now, simply put, I would like to go ahead with default parameters, so I am not going to change anything. One thing to notice here is the function name is written. Like in GUI, whatever function you are executing is both BIOSIG. Based on my values here, a function with proper attributes and key value pairs can be used. So, for now, I will just simply say okay, further, it will process and it will ask me the name of the file. I can simplify let us keep it as it is an EDF file. So, that will be the name of my new data set. Here if you can see the data set menu, so this data set will appear there. Or I will just simply write EDF file one and I will say okay. So, this has kind of imported the data on dot EDF file EDF is European Data Format dot EDF file into a MATLAB.
Further, if I want to see the data, I can go to plot and scroll data. So, I am able to see the data, if you can see it properly this all this y axis is different channels. And in the experimentation module I told you about different kinds of montages. So, if you notice it clearly that FP 1 minus F3, F3 minus C3, C3 minus P3, P3 minus 01. So, this all this bipolar value bipolar channel representation is nothing but your double banana montage. Now, this is your data very difficult to generate an interpretation we can increase or decrease your amplitude scale using this function let us increase it. So, you know where we can see some channelise moment okay. So, now you can see we need activities which is being happening here. And like we can notice that it is predoom like prominent in F4 and F3. So, something related to font although maybe some moment also but let say let say before we analyse it further I will tell you a few basic things. Using this you can select a particular time range you want to see for one in one display now I have changed it to 10 also you can see change the number of channels or do you want to display one display currently at General is there if I change it to five then I can only see those 5 images.
So, based on your analysis based on your criticality of problem, you can analyse the number of channels and you can accordingly adjust the time axis. You can move in the time axis using this push button, this will, second by second you move. If you use this button, it will shift the entire time interval. Currently it is 5 to 15, so it will shift to 15 to 25, it will shift to 25 to 35 and so on. So, now if I talk about Let us say, I will first shift it to original number of channels. Now, I will check. I want to, Let us say I want to, I do not, this portion is artefact, we will move to initial time, let us say this 5 in between 4 to 5 that is an artefact. So, I will select this particular region and I will simply reject it. So, it will again ask, that the earlier EDF data set was 1, now I am going to change it as EDF data set 2. Once I do that, now I have a 2 data set here. So, I can process any of the data set by checking the data. Let us plot it together. Let us start it again like we plotted it before. It was for 10 seconds.
So, I will again change this time range to display. And if you can remember the time amplitude square was around 5,000, let us keep it 5000. So, in between there was a moment it will be in 4 to 5, here you can clearly see a boundary which gives an indication that some of the channel has been removed. So, this is kind of basic things which you can do. So, these are the basic operations using EEGLAB. One more important thing about EEGLAB is that whatever processing you have done on your EEGLAB GUI, you can recall it using EEG dot history, EEG dot history. So, from the beginning, whatever you have done in the session, you can see it here. Open the EEGLAB, imported the file, name that set file as a EDF file 1, then you plotted it, again you name another file as EEG file 2 in which you have rejected some of the nuance, some of the indices data. So, this is how you will come to know for one particular GUI operation, which kind of scripting function should be used.
Now, GUI looks very easy to use and user friendly, then why we are going for scripting? We have seen using the PsychoPy we can, PsychoPy EEG where we can generate a multiple stimuli, various stimuli and it is very easy instead of writing a Python script for that, which was complex. So why should we emphasize on more on scripting? So, the clear reason behind that is stimuli generation is mainly once for one, done once for an experiment, whereas, this is signal processing, so it should be done for multiple subjects very easily. So, every time you will not be there to perform GUI operation and see final data, it will be a time-consuming. So to avoid that for batch file processing or multiple subject signal processing for EEG what we use is basically a scripting. Now, I wrote here EEG dot, this rate is nothing but it is stored in the structure EEG.
So if you successfully import your file, you will be getting all these fields under EEG structure which is the most important for further signal processing, it has all the channel locations, where it has been placed, whereas, it has the data as well as the venues most important thing for all channels. If you can see that it is recorded for three lakh as almost 3.5 lakh examples at 125 rate, 125 sampling rates. So, if you want to know exactly how many minutes the recording has been done, you can use this data of number of samples and sampling rate, you should be able to find out that this is for which, how many number of, like what is the duration of the recording. So, these are some of the basics of EEGLAB. I hope all of you have understood this thing. Similar way for different acquisition systems, we have different formats of the final acquire data set, and how we can obtain it using different scripting options that we will see in upcoming videos. So for now, I think I hope all of you understood the basic introduction of EEGLAB. We will shift our focus to data acquisition using EEGLAB for various formats from different EEG acquisition system.
So all of you have seen our demonstration of EEGLAB, so now most of you have some ideas about which kind of software EEGLAB is and how you can convert your GUI operation into scripting using EEG dot history function or EEG dot history variable, because EEG is a data structure. We will see in the upcoming data acquisition videos demonstration we will see that. It is basically a structure in which history is one of the field, which stores whatever operation which you do equivalent command. So yeah, different systems stores your EEG information into different formats. Open BCI boards stores in dot txt file, the simplest form you know and you can see it in notepad. Neuro-scan stores it into dot cnt file, whereas Nicolet EEG Viewer mostly used for a long run EEG and many number of electrodes are placed. Let us say for epilepsy and more, many number of electrodes are placed on your skull. So, in such cases, that is EDF files. EDF is the standardized European Data Format for EEG recording. So, I have included a one sample data of each type, one see in txt file, one cnt and edf file and I have shown how you can acquire the data basically biopotentials and event using MATLAB.
So, and also, I have shown how you can see the events for triggers are like auditors stimuli, visual stimuli or whatever stimuli are generated that digital pins how you will fetch it from the acquired loaded EEG data. So, next would be three demonstration, quick short videos. So, in case in future any of you would come across such recorded files, will be able to fetch the data by your own. So, next would be these three demonstration videos of acquiring EEG and events from recorded files and that will conclude this submodule, and as well as this four-part lecture series on brainwave recording, acquiring and processing. We have only seen loading brainwaves into MATLAB. Further if time permits, we will see how you can process it, like pre-processing is also important step, further extracting a particular biopotential, any time locked event. So, all this thing epochs and all, like if time permits, we will cover this topics as well. But for now flow-wise Let us see how acquisition happens and how different formats can be loaded into MATLAB, I will see you in some other module you can enjoy these three different demonstrations and then will meet in some other module for some other topic.
So, in this short module, we will see how to import our your recorded files into MATLAB for further processing. So, we have file here. In this module, we will see file which has generated through OpenBCI ports, how to generate OpenBCI Cyton or OpenBCI Ganglion recorded files from, to, from text file to the MATLAB workspace. So, this is the sample file I will show it to you first. So, if you open any of the OpenBCI recorded file it looks like this, this is the raw data recorded for eight channels and it is recorded at 250 hertz. Data-wise it is an array, from here it is an array of 16 columns. This column number 2, 3 and 4 are recorded channels, rest 5 we have not used in the particular experimentation. Here there are three channels which has been used as well as further this five 0s and 1are nothing but your digital input outputs. So, we have inserted our trigger in pin number 11. So, while extracting events, I will consider or I will take the value of pin number 11. I will show it to you, if you can see here, here the 11th column is constantly getting toggled between 0 and 1. We will observe the same in MATLAB as well once we successfully import the file.
So, let us see how we can generate this file, how we can, also we will see how to write a script in case of multiple subjects scripting is advisable. So, how we can write the script in order to make life simpler. So, in order to import this test file, there is a function MATLAB which you can use, I will name this variable or whatever ones get imported as sample txt, and let us load the file. So, while you load the particular file you should write everything like the path and the filename. Now, instead of writing path and file name, I will use a trick here, if I place pwd, it shows me present working directory, the, currently in which folder I am while running into MATLAB. So, what I will do, I will use a strcat function using this pwd as one of the string if you do not see here. What I will do is, I will use the string concatenation function so it will merge all the inputs, after this output backslash once, and then I will write the filename, which is sample txt dot txt. Now, this also should be given as a string. So, should be something like this. So, ultimately it will merge all the things and it will load by text file into sample text variable. So let us see. So, if you can see here sample text variable has been created and saved. The same thing what we have seen in the notepad you can observe here, as well as if I go to the 11th column and once I go further down, I should be able to see the change in triggers, I hope you can see here. If I scroll it slowly, you should be able to see, since zeros are there in the 11th column, again 1 for some period of time. So, this is how you can analyse the trigger, which is a trigger. Though it is same in both 11 and 12th column here, you can take any of the column as an event trigger.
So, now how can we take the data out of this? As I already mentioned, this particular experiment is for three channels. So, column wise, it is column number 2 to column number 4. I will show you, show it to you one more time. It is column number 2, column number 3, column number 4. So, these are my biopotential values right now. So, what I can do is and make sure it is a row value, you might say, that it is in the 10th to the power here or 10 to the power 4, but yes, its row value which is generated by the addition of so many noise that we have to remove gradually using proper pre-processing algorithm. So, what we can do here is, I will store it into a variable biopotential, the first three potential I will save it as a biopotential. Now, I can take it from sample text and I will fetch first three, like second, third and fourth column. So, this is basic a MATLAB way to extract particular columns from any array, very simple. So, that three channels has been recorded here, you can check this thing here. Furthermore, Let us take a channel also event data or chan, which we say an event channel. So, we can store in the similar fashion from sample dot txt and I need now 11th column because we have already seen that 11th column digit, of digital input output, keep get, was keep getting toggled. So, now I, we have both the data event channel as well as our biopotential, so, let us see, let us see just for visualization how it looks like plot biopotential. For now, we will plot only one channel just to give you guys an idea.
So, it is something looks like this. So, the first channel was having some starting value around 8,000 hours, so it is looking like this. One more thing to note down here is, if you go to some sample text file, it is showing that it is sampled at 250 hertz. So, whatever number of samples are recorded it is recorded as 250 hertz. Currently this 17,409 is the number of samples which you have and it is recorded at 250 hertz. So, it will give you an idea that this recording is performed for duration 69 seconds. Now, if you want to visualize that how it has been recorded for 59 sorry, 69 second, you can again plot it with respect to time indices. In this case what you can do is, you can take an additional variable which is considered from the same length, which is of the same length of biopotential and you can divide it by 250, should give you the original timescale in seconds. Let us write that, so let us see.
So, this is nothing but I already mentioned so 16, 17 second video. So you can get it with respect to time, now you can analyse that and you can check it further. Let us see, Let us see the triggers as well. So, we can plot now event channel. I already mentioned that event channel is your digital input. So, it will be 0 and 1 in order to just look at, in order to just make it look fancy or, and good, I will just put a y limit to 0 to 1.1. Let say that x, and again, I will plot it with respect to a generated time. So, I will follow the same thing which we have done it, in the case of biopotential. I will give it indices, time indices of the same, you can use length of biopotential as well, that is fine, as long as you can plot the channel properly, that is completely okay. And I have to divide by the sampling rate as well. So, I guess now, we should be able to see the triggers as well. That is it. So these are the triggers in similar fashion you are getting from here. One thing to note down here is that experiment has started any after few seconds, the triggers have been inserted. So, you can further analyse it, you should consider the data corresponds to only the time when triggers have been given. If your experiment is audit like some evoked potential generated using this trigger, you can further check it, zoom it to verify whether the generated data is really accurate or popular, accurate or not.
So, I have shown you an image while explaining auditory stimuli, this is the output of the similar auditory stimuli, in which you can clearly see that it is getting repeated twice in a second, so two hertz auditory stimuli and the similar kind of stimuli has been presented here. So, I think this is a realisation of what I showed that we have made using PsychoPy. So, similarly, you can try out your own triggers and record your potentials with some of the biopotential acquisition device. In our case, we have used OpenBCI Cyton board, which I showed you in the previous module. So, I hope this MATLAB acquisition of your data and events is clear and you can now import the data and further process it. Let us see how we can load different EEG file formats recording from various devices. We have already seen one instance of that. In one of the videos I have shown you how we can import OpenBCI Cyton board recorded file which was the sample txt dot txt file, how you can record, how you can rather import that file into MATLAB for further processing. In a similar fashion, we will see we have two more instances, one is sample cnt dot cnt file, which is a file recorded for our Neuro-scan EEG system. So, how you can load the particular file.
In OpenBCI case, there was a text function, a lobe function for the text file. In this case, we have a specific function which is popular cnt. This is a function of our neuro scanner plugin, it is also a part of EEGLAB function. So, make sure that your path should view, you have added EEGLAB into your path and path is added, it is already added in this case. So now and one more thing Let us see what will be there in your pop load load cnt function. This is, with the help of the function it will tell you what are the inputs required for the function, what is the different ways or different uses which you can give, at the same time you can know what are the optional input and output. Very important thing here to note is, output is EEG data structure. Earlier we have seen, we saved up values generated from text file as an array of floating number, whereas in this case, the final output will be stored in the form of data structure. So, Let us implement this let us see if we can get this data out from this cnt file.
We will name the finally generated output structure as EEG. As I mentioned, we are going to use to upload cnt function. Here input to upload cnt function is the file name, but it should include the entire part. So, like I have shown you, you can use a strcat concatenation with file name as well as you can write the entire path as well. So I will use the same method which I used for OpenBCI data and channel acquisition. This will be my sample cnt file with extension and I have to check for brackets as well. Once I checked that, I can load this file and we can see where the data lies. So, it will store as EE data structure, you can go inside this and observe the field. So, here if you can see the total number of samples are 87,600 and the sampling rate is 1,000. So, one second has been recorded in 1,000 samples, so everyone millisecond it records a sample. So, in other words, if my samples are 87,600, the data is recorded for almost 88 or 87.6 minutes. That is what you can see here as maximum. Further, we will go and check the data, which is one of the most important field for any EEG data structure. So, Let us see what is the inside.
If you observe the data, it is very evident and clear that first three rows are, has the values which varies with respect to time. And if I just talk about the nature of the values, they are analog in nature, whereas the values in the fourth column you can observe here it is zero. Further if I move ahead, still it is 0. So, after a few seconds, it will get toggled. So, let us see, let us see and scroll it slowly. Yeah, here if you see it has changed to 4096. Now, why 4096? Because 4096 is 10 to the power, 2 to the power 13 or 14. So, a particular bit in the data stream will get toggled from 0 to 1. When a specific stimuli is generated. So other way, it is to one particular bit out of that stream is changed from 0 to 1. So, you can see that event is generated, I just want you people to realise that this three, this three rows are your biopotential, analog in nature, at the same time this last row is your data, sorry, your events, event data, which is instored. Now let us quickly visualise what I said. So, just clear it. We can plot one channel from EEG dot data. Let us plot second row and you can see the graph. Again, like you did in OpenBCI, if you want to see this, if you want to see in the time domain, if you are comfortable not in terms of samples, if you want to see it in seconds, what you can do is,
You can plot the same thing for one colon length of EEG dot data divide by 1000. This should be, why 1000, because we have seen that sampling rate is 1000. Instead, if you want to write you can write EEG dot s rate also, that is completely fine. It is just a basic way of addressing a particular variable from your structure. Structure for all those who are not aware about what is structure, it is a kind of composite data type in which you can store multiple variables. Like if you know variables, there are different type of variables, right, some of them are integer, float, string and all. Structure is composite data type, which can have more than one type of data. Like if this asset is let us say double whereas if I go to the sat name is character array, so like that, you can consider structure as a composite data type. And finally, once I am done with this, here it should be dot first.
So, if I run that, it is showing me this kind of graph, exactly in relation or in agreement with what we have seen the total number of time duration is around 89 seconds. And you can see this kind of EEG movement. Let us quickly have a look at the events. So, events was stored in the fourth column. So, we will see the events first. So, you need to zoom in to see exactly how this event looks like and what is the time duration between consecutive events. Before that, let us convert the samples into the actual time, like we did for biopotential. So, in this case, you should use the fourth number of the row. Similarly, you can plot it, it should show you now from 0 to 89 seconds, and if I further zoom in, Let us take only 4 to 5 iteration. By the way, this one stimulus from here to here interval is your one iteration. You should perform at least 100 or 120 iteration based on your experiment, because this brain response is highly variable. So, you need to average out some of the noise and so, it will also average out some of the common noise and all. But yeah, if you see here, if you focus on this, this stimulus onset is around 6 seconds and it lasts till 6.6, 6.5 seconds around 650, 600 milliseconds for one period. So yeah, this now you can analyse it, your events and your biopotential, further you can process it based on your requirement.
Let us quickly see how we can import EEG biopotentials and channel from EDF file. So, if you have seen the introduction to MATLAB, I have shown you how we can import EDF file using GUI. At that time I have mentioned that pop BIOSIG function is used to import a EDF file. Now what is BIOSIG function? So, Let us just take the help of EEGLAB. One more thing to mention that, I am using EEGLABs function. So, you must add EEGLAB to your path. I hope all of you know how to add EEGLAB to a path and basics of you know your default path and how you can restore it. These are the different ways in which you can implement your purpose and function for loading a data from dot EDF file these are different optional inputs, you can have a look.
So here, Let us quickly load the data using pop BIOSIG function, output will be obviously like it was there for 4 floor cnt would be your EEG structure data structure. So, input should be a string filename format of string. So, I will use strcat function I will add first input as a present working directory and finally sample underscore EDF dot EDF. Looks fine. Yeah, check for the brackets and Let us run this. So, it will store the value in this EEGs data structure. So similarly, also the sampling rate, I think now we are doing this of second time, you know what are the things to observe, this is the total number of sample, this the sampling rate. Here you can see the data. It is a bit longer duration, which you can see here. We can see how many sampling points are there and how many what is the sampling rate. So, let us just try to find out the duration.
So, you can use the same EEG dot points variable divided by EEG dot srate. This should give you the total duration in seconds. So, it is about 2836 seconds. So, if I convert it into around 2836 divided by 60. So, 47 minute recorded data. And one more thing if you observed it was recorded for 18 channels. So, it should like, channels would have co
Log in to save your progress and obtain a certificate in Alison’s free An Introduction to Brain-Computer Interfaces online course
Sign up to save your progress and obtain a certificate in Alison’s free An Introduction to Brain-Computer Interfaces online course
Please enter you email address and we will mail you a link to reset your password.