Using Neurofeedback to Enhance Meditation Part 2: The Technical

This is a two-part article and to understand a lot of what we talk about here, read this article.

This article is meant for people who have a basic understanding of programming, especially the language python. If you don’t have it, this article will still make sense, but it is recommended to understand the project better. If you would like to get a basic understanding of python, I recommend this quick and free course that helps you learn python if you are a beginner.

Hardware

OpenBCI boards are basically the Arduino of the BCI world and are built mainly for data scientists and developers who want to build out projects with them.

  • OpenBCI Ganglion Board
  • OpenBCI dry electrodes

Since the OpenBCI headset, I borrowed didn’t fit me, I made my own headset using an old headband I had. If you want to see more info on the hardware process and how to re-create it, check out this article.

The Software

Brainflow is a library that integrates with most BCI boards, and it’s the library I used. It can obtain, parse and analyze the data from many biosensors and works with up to 7 languages and I used the language Python.

Brainflow has three parts:

  • BoardShim — read and output data from the board as well as preparing the board for use
  • DataFilter — performing signal processing and helps with handling and working with your data
  • MLModel — ability to use different machine learning models and calculate derivative metrics

Side Notes:

  • most of the data is returned as a 2D Numpy Array
  • refer to the documentation and API

My Code

To see my full code, head on to this GitHub repository.

The board has specific parameters specified in this link. Each board has a board-id to tell brainflow how to work with it. The Ganglion ID is 1 and the parameters it needed was the serial port. I also initialized some variables that we will need for later.

This code uses get_band_power to get the band power of each type of wave. The parameters include the range of the frequencies you would like to use. Then I made a dictionary assigning the type of brainwave with the state.

This code plays a sound that lets you know if you are in deep meditation or if you need to clear your mind. This is the audio neurofeedback that is being provided with the program. There’s a timer set up, so it doesn’t play every 2 seconds.

This if loop shows up at the end of the while loop which checks if they are still meditating. I used the keyboard package so see if the spacebar is being pressed and making that break the while loop.

The main while loop takes 5 seconds to run as I used time.sleep(5) which makes the program stop for 5 seconds. It was needed as the program froze a lot before I did that. I added a counter to count the total time and timers for how much time in each state. In the end, I converted it from seconds to minutes.

I put all the minutes in a list and used a for loop to go through the values and divide them by the total time. This gives us the percent of how much time they were in each state. Next, I used matplotlib to plot these values in a bar graph.

After we print the results, the program ends, but the data would still be streaming from the board. These final lines end the stream and that is the end of the code.

Found this article useful? Feel free to tell me by reaching out to me on LinkedIn or emailing me. Want to see other things I’ve done? Go to my website.

A student interested in emerging tech