Deciphering secret signals from your brain (with BCIs)

Neha Adapala
6 min readDec 3, 2023

--

“Yeah”, “omg”, “mmhm”.

You know when you’re on a call with your friend and you don’t know if they’re listening or half listening or even awake at all. At times like that, I really just wish that I could read what was going on in their brain.

But that doesn’t seem as science fiction-y as you might think it is. Check out my last article to find out how anyone could theoretically use BCIs to detect people’s emotion.

This is not far out of reach though, as I figured out a way to check if someone is concentrated, in deep sleep or relaxing! I’m sure you’re curious now, so let me explain.

WHAT YOU NEED TO BUY:

  • Muse headband (I use the Muse 2 headband)

Yeah that’s it…

  1. Download Python

For this project, I decided to use Python. Go to https://www.python.org/downloads/ and download the latest version that works for whatever your device is. You can click on the yellow links to change the version that you’d like to download

2. Download Visual Studio Code

I chose Visual Studio Code because it’s free and easy to use, especially because so many other people use it as well, so they’ll come across similar problems to you and provide solutions online when they find them. Go to https://code.visualstudio.com/Download and download the latest version that works for your device.

If you’d like to find out more information about your Mac to check if the download will work. You can check it by clicking the small apple in the top left > About this Mac > More Info.

3. Learn how to use the Muse headband

To learn how to properly connect the Muse headband for the best connection, I would recommend downloading the Muse app and following all of those steps first. Once it’s connected properly there, disconnect it, because Muse can only connect to 1 device at any certain time.

4. Download an outlet

An outlet is necessary for your muse headband to display the signals on your computer. Therefore, I chose my outlet to be Petal Metrics. Simply download it and follow the instructions to make it an application on your device.

5. Navigating Petal Metrics

Now, just open Petal Metrics and under Type, click LSL and click the STREAM button. If you scroll down, it should show that it is connecting, or that it is streaming + the name of your Muse headband.

If it doesn’t show that it’s connecting, make sure that it is disconnected from ALL other devices.

6. Downloading Github code

Great! Now we want to actually see what the signals look like. Luckily, alexandrebarachant on Github has created a Python package to stream, visualise, and record EEG data from our Muse headband. We just need to have this in our own code.

First, go this link and click on the dropdown of the green button that says Code. Then, press the copy button that I’ve circled.

7. Working with VS code

Now go back to Visual Studio Code (VS Code) and click on the below items in the order of the numbers. (For step 3 you have to copy the link into the bar).

After this, click the terminal button at the top of your screen (circled below) and click New Terminal — the first option.

8. Installations

Now in the terminal, you have to install some python dependencies using Python. You should see a screen like the one in the picture.

Now in the terminal type the following commands each on one line and press the return button after each line:

pip install muselsl
pip install pygatt
pip install numpy
pip install matplotlib
pip install pylsl
pip install os-sys
pip install scikit-learn
pip install scipy

If pip doesn’t work, try replacing pip with pip3.

9. Viewing the signals

Perfect! Now just type in muselsl view into the terminal to view the signals. You should see something like the image below.

10. Detecting what state the brain is in

Great! Now we’re on the last stage.

a. Input

First, we’re gonna figure out what the user wants us to detect. You can do that by copying the below code into “neurofeedback.py” in the section shown below. You can find neurofeedback.py under the examples dropdown.

print(“1. concentration”)
print("2. deep sleep")
print("3. reduced anxiety")
mode = input("What wave would you like to detect? (please input a number 1–3)")

b. Figuring out signals

Then, in the section shown below you want to copy in this code:

if (mode == "1") :
beta_metric = smooth_band_powers[Band.Beta] / \
smooth_band_powers[Band.Theta]
print('Beta Concentration: ', beta_metric)
if smooth_band_powers[Band.Beta] >= 0.3:
print("Very concentrated!")
if (mode == "2") :
delta_metric = smooth_band_powers[Band.Delta] / \
smooth_band_powers[Band.Delta]
print("Delta: ", band_powers[Band.Delta])
if smooth_band_powers[Band.Delta] >= 2:
print("you're sleeping really deeply!")
if (mode == "3") :
alpha_metric = smooth_band_powers[Band.Alpha] / \
smooth_band_powers[Band.Delta]
theta_metric = smooth_band_powers[Band.Theta] / \
smooth_band_powers[Band.Alpha]
reduced_anxiety = False
if theta_metric > alpha_metric:
reduced_anxiety = True
if reduced_anxiety == True:
print("Yes! You're relaxing!!!")

We’ve used “if statements” here to allow us to show the right thing to the user.

This means that if the user chooses 1, the program will look at the values of the beta waves and see if the user is very concentrated, depending on if the value is above or equal to 0.3.

If the user chooses 2, the program will look at the delta waves to see if the user is in deep sleep, depending on if the value is above or equal to 2.

If the user chooses 3, it will then collect data from both the alpha and theta waves. If it detects that the theta waves have greater values than alpha waves, it will output that the user is feeling less anxious and therefore, relaxed.

c. Run the code

Now that you’ve done everything, just click the run button (top right) and try the code out!

Perfect!! You should now see something along the lines of this in the terminal.

WELL DONE!!! You did it. Now you can use this however you would like to (like seeing if your friend is really listening to what you’re actually saying)!

Thanks for reading. If you are interested or have any questions, please email me at neha.adapala@gmail.com

Feel free to follow me on LinkedIn: https://www.linkedin.com/in/neha-adapala-7b2a56231/

And X: https://twitter.com/neha_adapala

REFERENCES:

Thank you so much to Carol Rong for the tutorial for this! https://betterprogramming.pub/viewing-your-brain-activity-with-the-muse-headband-b03dde069ae4

--

--

Neha Adapala

Hi! Feel free to contact me about anything from natural sciences to arts. My interests mainly lie in technologies though!