/EEG

We build an EEG headset which allows the user to send morse code to a computer using their brain wave activity.

Primary LanguagePython

Project Description

This project builds an EEG device that measures brain waves to determine if the user is concentrating or relaxed. An EEG uses electrodes placed on the user's scalp to measure the voltage difference between brain regions. These voltage differences oscillate and represent synchronized activity over a network of neurons. Depending on the electrode placements, brain waves have different characteristic frequency, magnitude, and are related to different brain activities. In our project, we measure alpha waves originating from the occipital lobe because they are one of the strongest EEG signals. Alpha waves have a signature frequency in the range of 8-12 HZ. It reflects activities of the visual cortex: their magnitude is increased with closed eyes and relaxation, and decreased with open eyes and concentration.

In our project, we use a circuit to amplify the electrode signals from the brain, and uses a number of high pass, low pass, and notch filters to filter out frequencies outside of 8-12 HZ. Then, we take data with a raspberry pi 4 and post-process the data using digital filters and analyze it with statistical methods. In the end, we successfully captured alpha waves and observed that its magnitude varies significantly between a concentrated and relaxed state. Furthermore, we came up with three applications that utilize our EEG device:

1st application: use EEG to play "EEG bird" (modeled after flappy bird).

2nd application: use EEG to monitor child attention level. We had fun making this video!!

3rd application: brain-to-text communication, in hope to help paralyzed people communicate.

We based our project on the guidance of an amazing article instructables.com/DIY-EEG-and-ECG-Circuit/. However, we created our own circuit design, wrote our own code for data-taking and analysis, and included unique and fun applications. We hope this Github Repository can help people implement and debug their own EEG circuit. Make sure to check out the circuit debugging tips section if you are working on a similar project and your circuit doesn't work!

Our project presentation: https://docs.google.com/presentation/d/12gxBqD7eJtzKXW-NE2dzhQD0BcE0SRJ6LncpbzUi6I8/edit?usp=sharing

Methods

List of Components

Wiring

The above diagram describes the complete EEG setup. The user tapes 3 electrodes to their scalp at various locations to measure the brain's electrical signals. These electrodes are attached to the circuit, which serves to amplify alpha waves and filter out other signals. The voltage output of the circuit is measured with the ADC and the data is sent to the Rpi. The Rpi applies further digital filtering and outputs the voltage of the user's alpha waves.

Note that we have two different grounds; the ADC and Rpi ground (black) at 0V, and the circuit ground (pink) at 3.3V. This design choice is due to ADC input voltage limitations and is explained in detail later. To get the -9V to 9V of power with respect to the 3.3V ground, connect one 9V battery the correct way, and the other one backwards.

Here is a picture of the physical setup:

Electrode Placement for Alpha Waves Measurements

There are many possible electrode placements depending on what wave one chooses to measure. In our project, we measure alpha waves. We need to use three electrodes: one at the left mastoid (the bone at the back of the left ear) which is connected to the ground of the circuit. This electrode helps with noise cancelling; one located one inch above and one inch to the right of the nasion (the midline bony depression between the eyes where the frontal and two nasal bones meet); the last one located one inch above and one inch right of the inion (the projecting part of the occipital bone at the base of the skull). The 2nd and 3rd electrode placements are approximately in O2 and Fp2 regions in the below diagram:

The voltage difference oscillations between the 2nd and 3rd electrodes are the target alpha waves, which is then fed to the circuit to be amplified and filtered.

Circuit Schematic

The circuit consist of the following sections:

  • Instrumental Amplifier (gain ~91)
  • Notch Filter (60 Hz, gain = 1)
  • High Pass Filter (Fc = 7.2 Hz, gain = 1)
  • Low Pass Filter (Fc = 32.9 Hz, gain = 1)
  • Instrumental Amplifier with variable gain (gain ~90-460)
  • Notch Filter (60 Hz, gain = 1)

The total filter responses without gain are shown below:

Individual Section are discussed further below.

Instrumental Amplifier (gain ~91)

Alpha wave signals are 15-50 uV so we need a lot of amplification in the circuit. An instrumentation amplifier takes as its inputs 2 voltages, and outputs the difference between the two multiplied by some gain given by: G = 1 + (50.5 kOhm)/R, where R is the total resistance between pin 1 and 8. Note it is possible to make a home-made instrumentation amplifier with 3 op-amps. However, it suffers from a low CMRR unless precision resitors are used (info from Instructable).

1st Notch Filter (60 HZ, gain = 1)

The biggest source of noise in our system is centered at 60 Hz due to power line interference. This noise is present even though we use batteries to power the circuit. Thus we have 2 notch filters in the circuit (filters that have a severe reduction of gain around 1 particular frequency). The first notch filter intends to filter out interference before more gains are applied.

The notch frequency is given by f = 1/(2 PI R C) where R = R3 = R5. The other two resistor values are related to the quality factor of the filter, which determines how sharp the attenuation is.

High Pass Filter (Fc = 7.2 Hz)

The high pass filter intends to filter out frequencies corresponding to galvanic skin response across our head. This interference is primarily low frequency. A second order active filter design is used here and is shown to be necessary for noise reduction.

Low Pass Filter (Fc = 32.9 Hz)

The EEG waves of interest to our project are alpha (8-12 HZ) and beta waves (12-30 HZ). Thus, we are not interested in frequency > 30HZ and filter them out. A second order filter design is used.

Instrumental Amplifier with variable gain (gain ~ 90-460)

This 90-460 gain is on top of the 90x gain from the first instrumentation amplifier. Alpha wave amplitude varies from person to person, from about 15 to 50 uV. Using a middle value of 30 uV, this means the ending voltage reading could range from 909030e-6 = 0.243V to 4609030e-6 = 1.242V. The variable gain is achieved by putting resitors in series and in parallel. The gain is roughly in the range of 90-460, which corresponds to potentiometer value 1k (maximum) to 0 (minimum).

To adjust the potentiometer, start taking readings and make sure one is not moving at all. Make sure voltages don't fluctuate offscreen, but avoid making it too small because then the errors incurred from digitally reading the data into the Rpi would be relatively increased.

2nd Notch Filter (60 HZ, gain = 1)

Another 60 HZ is necessary at the end of the circuit since the power line interference seeps into the circuit through prior steps. This is the exact copy of the 1st notch filter

Connecting ADC to Rpi

Supply the ADC chip with 5V from Rpi to ensure the maximum input voltage range possible. The pin configuration and connection in the image above is correct, but it is recommended to double check the connection, because wiring mistakes can lead to damaging the chip and/or the Rpi.

Circuit Debugging Tips

If the circuit doesn't work:

If you don't see brain waves but the circuit works:

  • Most likely it is because the electrode placement is wrong. To solve this issue: look up what is the systematic and best practices in setting up electrodes, look up 10-20 EEG Measuring System, test it on a friend, wash your hair, etc
  • Try switching the electrode wires feeding into the instrumental amplifier, you could be amplifying negative voltages
  • Switch out Op Amps, which are easy to break.

Post-processing

Data Taking Methods

The output from the circuit into the ADC is the amplified voltage difference between the nasion and inion. The ADC repeatedly reads this voltage value over time and sends the data to the RPI for digital processing.

Digital Filtering

At this point our data will still contain some noise which has a frequency outside the 8-12 Hz region. To eliminate this noise, we take the fourier transform of the voltage data over a 5 second interval and set all frequency components outside 8-12 Hz to zero, and the take the inverse fourier transform to reconstruct the signal.

Example of using digital filter to keep 8-12 HZ components and inverse fourier transform back to time series (before / after):

Gaussian Analysis and Voltage Threshold

This code is created to 1) find the optimal voltage threshold which separates relaxed and concentrated data and 2) evaluate how distinct the relaxed and concentrated datasets using statistical analysis.

We approximate concentrated and relaxed brain wave data sets each as normal Gaussian distributions. The cross point of the two gaussians give the best threshold voltage V0 which separates relaxed and concentrated data. This voltage threhold would minimize wrong classification rates. Specifically, the area overlap on the left is the probability of misclassifying the relaxed state, and the area overlap on the right is the probability of misclassifying the concentrated state.

The following plots are data from Hak and Ryan, applied with Gaussian analysis. Note that the y axis (frequency of occurence for each RMS voltage bin) is scaled so that the histogram distribution integrates to 1.

The above figure contains 30 samples of 5 seconds of concentrated brain wave data and 30 samples of relaxed data for each person. One can see that the distribution is indeed different for differrent people and that the relaxed and concentrated states are very distinct. In fact, the fail rate according to the Gaussian distribution is - Ryan: 0.01%, Hak: 0.0001%. However, we take this with a grain of salt because we do see some data right next to the threshold. This is likely due to the fact we have not accounted for the artifacts. We also don't know if we can assume the distribution is normal. Also notice that the concentrated distribution is much narrower than the relaxed one. This shows that it is much harder to relax than to concentrate: it is easy to make sure one is concentrate by looking at colorful images and doing mental calculations. However, even when one is relaxed, it could still be hard to control involuntary eye movements and thoughts, which could possibly have an effect on alpha wave magnitude. Also, the diagram does not indicate that there is no mid-level between the concentration and relaxation gaussians. The sharp distinction between concentrated and relaxed population is simply due to when taking the data, the person is trying their hardest to concentrate/relax.

Alpha Wave Data Visualized

The following are examples of post-processed concentrated and relaxed data from the same person. It is clear that the magnitude of the waves are very different:

Example of a fourier transform from raw data shows a clear spike in the alpha wave 8-12 Hz range:

Click here to access all the data we took

Applications

All of the following applications rely on the singular fact that alpha wave magnitude increases in the relaxed state and decreases in the concentrated state. Calibration is also very necessary for all of the applications since alpha wave magnitude varies from person to person, and since the gain of the second amplifier is adjustable.

EEG bird is a game modeled after the popular flappy bird game, but adjusted to take EEG brain waves as input. In general, the more concentrated the higher bird flies, the more relaxed/eye closed the lower bird flies. To make the bird motion smoother, we make the concentration level correspond not directly to the height of the bird, but the velocity of the bird (velocity is positive when concentrated, negative when relaxed).

Even though the original flappy bird game is hard, using EEG signal as input makes the game even harder. There are many parameters one can change to adjust the difficulty level and make the game playable, such as: interval of the pipes, gap between top and buttom pipe, floor rate, pipe rate, relaxed/concentration level cutoff during game play, etc.

See code for more detailed documentations

Child Attention Monitor https://youtu.be/HQ8krHOXocc

The child attention monitor is a comical application of the EEG device. A parent could place the EEG on their child's head while doing homework, and if the child is relaxed for over a certain period of time (here 5 seconds), the device will set off an alarm to notify the parent. Below is the measured brain wave voltage over time in the youtube video.

See code for more detailed documentations

Brain-to-text Communication https://youtu.be/74iM_w6vFuU

The brain-to-text communication allows the user to display words by only changing their mental state between concentrated and relaxed. This could have applications to help paralyzed people communicate. Over 5 second intervals, it measures whether the user is on average concentrating or relaxed, effectively communicating one bit of information every 5 seconds. This binary data is then converted into letters and words. The graph from the youtube video is shown below.

See code for more detailed documentations

Future improvement

Artifact Removal Using Independent Component Analysis

The circuit has already shown success in filtering out noise in a wide frequency range (caused by skin, power line, etc). However, it is still subject to artifact signals unrelated to the brain waves of interest. The method we would like to experiment with in the future is independent component analysis (ICA). It has shown to be a robust method used for EEG in the field as well as in research to separate mixtures of brain activities, as well as to eliminate contamination of signals by eye movements, blinks, muscle, heart, and line noise.

ICA is a signal processing method to separate independent sources linearly mixed in several sensors. ICA recovers a version of the original sources, by multiplying the data with an unmixing matrix: U = WX, where X is the data with dimension (channel * time), U is the ICA source activties (components * time), and W is the ICA unmixing matrix. ICA separates out the independent components by finding W such to minimize the gaussianicity of each data set.

To apply ICA to EEG data, we assume the following

  • Mixing is linear at electrodes
  • Propagation delays are negligible
  • Component time courses are independent
  • Number of components are equal or less than the number of channels

We cannot apply ICA to our current circuit because we are limited by the number of electrodes. As mentioned above, we need to increase the number of electrodes our circuit can accomodate in order to separate out enough artifact components.

Improve Precision of Electrode Placements

This is arguably the biggest unresolved challenge in our project. The electrode placements need to be rather precise to capture the signals. The fact that skull varies from person to person poses additional difficulty. A clear scalp free of oil and dendrites is also important. We need to find a systematic method to find electrode placement, and potentially use more robust tools other than tape. We have our eyes on the following link as a start point: https://www.ers-education.org/lrmedia/2016/pdf/298830.pdf

Beta Waves

This circuit should also be capable of measuring beta waves, which are in the frequency range 12-30 HZ. Beta wave is essentially the opposite of alpha waves in the sense that it is increased with concentration and decreased with relaxation. We would like to try this next.

Acknowledgement

Many thanks to Professor Andrew Jayich, our lovely TAs Beaks and Mingyu for supporting us and guiding us along the way.

Special thanks to Rei for giving us a huge bread board and starring in the "naughty naughty movie".

References