SynGesture — Using MATLAB to Predict Hand Movements With EEG Data

Aaryan Harshith
Nov 16, 2019 · 6 min read

Ian Burkhart was your average college freshman. He was studying Video Production at Ohio State University, landed himself a job, and even coached a lacrosse team. As far as everyone could tell, Ian had a bright future ahead of him.

That was until his freshman year of college, where Ian went on a summer vacation with his friends to an abandoned beach in North Carolina. As he threw himself into the crashing waves, it was already too late to realize that he threw away the use of his limbs as well.

Ian was rendered a quadriplegic and was told he could never hope to move a muscle below his neck. In 2017, when all hope was lost, Ian was chosen to participate in a mysterious trial from a company called NeuroLife.

Ian Burkhart narrating his struggles as a quadriplegic, as well as his experience with NeuroLife

NeuroLife turned out to be a company that aimed to revolutionize the treatment of people with lost connections between their brains and their bodies. NeuroLife’s straightforward system was able to bypass Ian’s broken nervous connections to his hand. Ian, a quadriplegic, was now able to pick up blocks, open jars, and even play guitar.

This article is going to cover how NeuroLife, and other future-forward companies, are developing their systems, as well as how you can make one in less than ten minutes.

Using EEG Data to Predict Hand Movements

Did you know that every movement you make, your brain had to prepare a second in advance?

When someone has an intention to perform a motion, clusters of neurons generate specific levels of electrical potential to be sent to the desired location of movement.

For people like Ian, these signals are blocked from reaching anywhere under the C5 vertebra. In the field of Brain-Computer Interfaces (BCIs), this issue is solved by reading potentials directly from the source (the brain) and recreating that signal in the region of interest. Using technology such as electrocorticography (ECoG), and electroencephalography (EEG), we can analyze someone’s brain signals to detect their intentions to move.

In my previous article, I mentioned how these measurement systems are highly sensitive. Actions as innocent as a blink can result in a nuclear blast’s worth of brain signals, and can completely obstruct important data from being analyzed.

EEG signal charts showing the effects of various artifacts on the readings of electrodes

Now, rather than aiming to remove unnecessary data, we’re going to be developing a machine learning program to “read someone’s thoughts” — based solely on their EEG signals.

As we all know, in machine learning, data is king. To develop a model that can accurately classify a person’s thoughts, we need a large dataset of labelled EEG recordings. For the purposes of this tutorial, we will be using the EEG Hand Movement Dataset available on Kaggle. It is a zipped file containing roughly 23MB of EEG recordings from a total of four patients. Each patient’s data is recorded in a separate .CSV file, and is subdivided into three classes of motion. Using a 14-channel Emotiv EEG headset, data was labelled with either a zero (right hand motion), a one (left hand motion), or a two (no motion).

Now that we have our data, we can start by uploading it to MATLAB Online. MATLAB is an accessible interface that you can use to create robust machine learning models without a single line of code.

After unzipping your file, start by opening the MATLAB Online workspace. In the Home tab, click the upload button ad choose the CSV file for ‘user_a’. This is the file that we will be analyzing in this tutorial.

Open MATLAB Online and click on the Upload Button, highlighted in green

If done correctly, the file should show on your Current Folder tab. Double click the icon to reveal a visualization of your data. The first column of your data, labelled Class, is our label column. Select the dropdown menu for this column and set the Column Type to Categorical. Setting this column to categorical shows MATLAB’s automated system that this is the variable that needs to be predicted.

Next, in the Import section of the CSV, set the data range to A2:DI2881. Doing this will exclude the column names from being registered as data points in the model. Once the data points have been selected using the range, click the Import Selection icon to create a table in your MATLAB workspace.

By viewing the data as a CSV in MATLAB, we can select a data range and import our file as a usable table

Now that our data is fully prepared, we can now start building a classification model using MATLAB’s integrated ML development system. In essence, we’ve completed the hardest part of this project — everything from here is a cakewalk.

Switching to the Apps section of your workspace, select MATLAB’s built-in Classification Learning program. This should open a new window, prompting you to start a new session. Simply click ‘New Session’ to begin on your machine learning adventure.

Doing this will open yet another tab, describing the dataset’s 113 predictor variables. Since you set the label column to categorical, MATLAB already knows that Class is our response variable.

Before you start training different models, be sure to choose holdout validation and set it to 20%. For datasets of this size, holdout validation seems to yield the best results, but you can try making this project your own by experimenting with different settings.

In this example, holdout variation is selected and set to 20%; however, try out as many settings as possible

You can now proceed to click the Start Session button. Clicking this button will lead to a window with an option to choose one of the dozens of machine learning models available. After rigorous rounds of testing, an optimizable KNN model was shown to perform best for this dataset; however, feel free to play around with any model you like. In the optimization settings for the KNN, set the number of iterations to 50, leaving the remaining settings at their defaults.

For the purposes of this experiment, the model is set to optimize itself for 50 iterations, rather than the default of 30 iterations. All other parameters are left at their default values.

Our model is finally complete — you’ve created a machine learning model on par with companies such as NeuroLife — all in less than ten minutes! Finally, click the Train button to evaluate your model’s accuracy.

It’s spectacular how a simple KNN model can achieve an accuracy of 94.1%! Our model succeeded in optimizing and converging extraordinarily quickly as well. This metric is on par with the systems of some of the best neuro-prosthetic companies, currently at around 96% accuracy, and best of all, making this model didn’t require a single line of code.

Now that you’ve created this highly robust and efficient model, imagine what else you can do with EEG Data. Systems such as these are what’s helping people like Ian hope for a brighter future. A brighter future doesn’t come on its own, we’re supposed to create it. By reading and understanding the message of this article, you’re one step future to creating a brighter future, and I hope that you’re excited about what’s next.

Thank you for reading!

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem

Aaryan Harshith

Written by

I write about things every week(ish).

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem