Introduction top
"A visual, responsive neurofeedback EEG system to aid in relaxation."
Electroencephalograms (EEG) are at the conjunction of the fields of Electrical Engineering and Neuroscience; by measuring brain activity through the use of electrodes and an amplifier circuit, we can utilize signal processing techniques to explore the impacts of external stimulation such as music and meditation on brain waves. For our EEG, we focused mainly on looking at alpha and beta brain waves. Beta brainwaves are associated with being awake and using our brain for reasoning, while alpha brain waves are associated with meditation, and relaxation. We can then observe the resulting brain waves on a screen and attempt to control an external event, in this case, turning on a certain colored LED based on our alpha and beta brain waves. In this way, the neurofeedback from the LEDs provides a means of self-regulation of brain waves that can prove useful in applications such as cognitive-behavioral therapy.
High Level Design top
Rationale and Source of Our Project Idea
We chose to do this project because we were both interested in neural feedback systems, and were interested in doing a project that had a biological application. We were inspired by the Brain-Computer Interface project by Charles Moyes and Mengxiang Jiang from Spring 2012, where brain waves were used to control an EEG Pong game. We wanted to use our brain waves to see a representation of our mental state and whether we could use the biomedical impacts of Electrical and Computer Engineering to gain insight on how our brains react to stress and breathing techniques that can be used to relax us. We also found this project technically interesting because it involved a conjunction of analog circuit design, signal processing, and software design.
Background Math
The primary math behind this project involves understanding of Fast Fourier Transforms (FFTs) and fixed point arithmetic. 16:16 fixed-point arithmetic is preferable to floating point arithmetic primarily because it is much faster, and because we can represent these numbers as 32-bit integer types. In 16:16 fixed-point representation, we take the least significant 16 bits as the fractional portion of a number, and the most significant 16 bits as the non-fractional portion. The conversion from fixed-point to decimal is shown below.
FFTs are utilized in this EEG project to understand the frequency characteristic of a signal. In this case, it allows us to understand what the peak frequency of our brain waves, as well as how much our brain waves are dominated by alpha waves, and how much by beta waves. The equation for an FFT is shown below.
Logical Structure
Figure 1 below shows the high level block diagram of the EEG system. The hardware of this EEG involves 5 stages, with a combined gain of 1250. After placing adhesive electrodes on a user's forehead, we use jumper cables to connect to the negative and positive ports of an instrumentation amplifier. To ground the circuit properly, we take take another electrode measurement from the user's arm to the circuit ground rail. Because all the amplifiers used in this project are dual-rail, and because we cannot use an AC power source, we built a dual-rail battery circuit to get +4.5 and -4.5 V rails. From the electrodes themselves, we expect a signal on the order of microvolts to millivolts, so a high gain is necessary. After two gain stages, we have two filtering stages to filter out all frequencies above 31 and below 8 Hz, placing us within alpha and beta wave ranges. Our last hardware stage involves a voltage divider to ensure that the signal is between 0 and 3.3V so that the ADC on the PIC32 can read it.
Once we obtain 512 ADC samples from the hardware, we take an FFT of the data while simultaneously displaying the wave on the TFT screen. For safety reasons, we could not connect the circuit to any AC power source, so using an oscilloscope was not a possibility. After obtaining the peak frequency of the FFT spectrum and summing up certain bins, we can obtain a ratio of "calm" waves to "excited" waves. This ratio is then passed through a 3-point averaging filter over time, and the resulting average ratio is used to light up red, yellow, and green LEDs, depending on the user's state.
Hardware Software Tradeoffs
As a primarily analog project, this EEG system does not have too many hardware-software tradeoffs. One relevant one, however, deals with how we filter out brain wave frequencies that fall outside the alpha and beta ranges. In this case, a combination of hardware and software filtering worked best. Initially, we attempted to filter mainly through hardware, but due to the low amplitude of the brain waves themselves, the filter cut-offs were not very sharp. As a result, sometimes, frequencies below 6 Hz were still detected by the ADC, even though they were very attenuated. In this case, we introduce a software filter after performing the FFT on the brain waves, and only turn on LEDs with the information from frequencies within the alpha and beta wave band.
IEEE Standards
There are IEEE recommended practice for neurofeedback systems which we adhered to. We followed their definitions of EEG activity in terms of frequency bands, which are as follows: DC-EEG: < 0.1 Hz, delta: 0.1 to <4 Hz, theta: 4 to <8 Hz, alpha: 8 to 13 Hz, beta: >13 to 30 Hz, and gamma: >30 Hz (particularly 36 to 44 Hz). The regulatory requirements set forth by IEEE which apply to us are to document the system in terms of adherence to the IEC 60601-1 Safety and Essential Performance standard. We do not plan on selling this product so the remaining regulatory requirements do not apply. IEEE also sets system standards, such as providing thorough documentation about the components to the system, specifically the electrode/sensor, acquisition-which refers to the feedback mechanism, software, and computer and display hardware, which we do throughout this website. IEEE states that the electrode must be biocompatible and that the placement of them on the scalp should be repeatable taking into account variability between humans. We used adhesive electrodes, placing two on the user’s forehead, and one on the user’s arm as a virtual ground. This ground can be placed in other areas if there are any disabilities present.
Hardware Design top
In this section, we will discuss the specifics of each component in the high level block diagram. As mentioned, there are 5 hardware stages, each of which involves some amplification or filtering. In building the hardware, we also leveraged an incremental design approach, testing each stage only after we knew the previous stage worked properly.
Battery Circuit
In order to power the op-amps, we had to provide a dual rail voltage supply. We chose to make a +4.5V and -4.5V power rail. Since we were not able to directly connect our circuit to a power source, we created a battery circuit to provide the necessary voltage. We had a battery pack consisting of six AA batteries, which created a 9V power supply. We attached the battery across the 2 leads of a 2200 μF capacitor, with the ground of the battery pack going to the negative side of the capacitor and the other to the positive. Two resistors of values 100K, connected the battery ends into an LM358 op-amp. The VCC pin of the op-amp was used to produce a +4.5V rail, and the GND pin was used to produce a -4.5V rail. The output of the op-amp is tied to the same virtual ground as the rest of the circuit, which is discussed later in the webpage. The schematic of the battery circuit can be seen below in Figure 2.
Test Circuit
In order to test each stage without connecting electrodes to the user, we required a test circuit to emulate the level of attentuation that we expect from a brain wave reading. To do this, we built the test circuit below in Figure 3, and connected it to the instrumentation amplifier. All of the following stages remained unchanged while testing hardware. We passed in a 2Vpp sine wave from the function generator to a voltage divider that emulated a brain wave. We calculate the attenuation factor as follows: R1⁄2*R2. Using R1 = 100 and R2 = 100K from the diagram, we get an attentuation factor of .0005. As a result, a 2 Vpp signal becomes a 1 mV signal, which is within the range of brain waves that we may encounter.
Instrumentation Amplifier and First Order Gain stage
Figure 4 below shows the first few stage of our amplifier circuit. Powered by the dual-rails from the battery circuit, the INA121 Instrumentation Amplifier takes the two active electrode measurements from the user's forehead and amplifies brain wave signals with a gain of 50. We twisted the input cables that were connected to the electrodes together, so that we were better able to elimintate 60 Hz power line noise. The twisted pair helps reduce the normal mode noise since both pairs are now equally exposed to electromagnetic noise. An instrumentation amplifier is ideal for measurements because it has very low noise and very high input impedance. To achieve this gain of 50, we use a gain resistor Rg of 1kΩ. The equation for the gain of an INA121 amplifier is G = 49.4kΩ⁄Rg + 1.
Our next stage is a 1st order gain stage, with a gain of 100. This is shown in Figure 5. The gain of an LM358 is given as G = Rg⁄R1. In the circuit below, we have Rf = 100KΩ and R1 = 1KΩ, giving us a gain of 100 for this stage. We decided to amplify the circuit before filtering out all the noise so that the following filtering circuits will have a sharper cut-off response.
High Pass Filter
We created a High Pass Filter circuit using an LM358 op-amp, which was used to pass values above 6 Hz. Since alpha waves begin at 8 Hz, and because we wanted to give some leeway due to noise and interference, we set the cut-off in this way. This allows us to filter out lower frequency waves such as delta and theta waves. The equation to determine the cut-off frequency of a 2nd order filter is shown below.
By using resistor and capacitor values of 20KΩ and 1μF, we obtain a cut-off frequency of about 6 Hz. The schematic of the HPF can be in Figure 6. The gain of this stage can be calculated as G = Rf⁄R1. With Rf = 5KΩ and R1 = 10KΩ, the gain for this stage is ½.
Low Pass Filter and Voltage Divider
As discussed previously, we took special considerations to eliminate the 60 Hz power line noise. But in order to limit our range to alpha and beta waves, we required a Low Pass Filter, which we built with another LM358 op-amp. The schematic for this can be seen in Figure 7. This circuit filters frequencies above 31 Hz, because as per IEEE Standards, beta waves can reach frequencies of about 30Hz. The equation to determine the cut-off frequency for this low-pass filter is the same as that given in the previous section. Using R = 5KΩ and C = 1μF, we get this cut-off. As in the case of the high pass filter, we have a gain of ½ for this stage.
Finally, after the low pass filter, we need to ensure that the signal is within 0 and 3.3V so that the ADC on the PIC32 will be able to register it. This is achieved with the following voltage divider circuit, as seen in Figure 8.
PIC32 Circuit
The PIC32 circuit was powered by a USB connection through a laptop that was not plugged in so as to prevent a direct ground. It was grounded through the same virtual ground as the amplifier circuit. The output of the LPF from the amplifier circuit was passed through a voltage divider circuit into the ADC, so that only values between 0 and 3.3V are passed into the PIC32, in pin 24. We also use a TFT for brain wave, FFT, and frequency data display, which is wired to the PIC32 as can be seen in Figure 9. Finally, we had a red, yellow, and green LED wired to the PIC32. They were turned on depending on the brain activity levels we were reading; red for higher levels of activity and green for lower levels. The details as to how these activity levels were determined are discussed in the software section.
Software Design top
In this section, we will discuss the software running on the PIC32. We take the end of the amplifier circuit and pass it to the ADC input of the PIC (pin 24). After that, we simultaneously display the signal on the TFT, take the FFT of the signal after 512 points, determine the ratio of calm to excited states, and control the LEDs accordingly.
Using TFT as Oscilloscope to Display Brain Waves
Since we are unable to be directly connected to power, we used the TFT as an oscilloscope, which turned out to be a fairly straightforward task. We display the signal in two different ways: as a plain signal, and as an FFT spectrum, and we switch between these two with a hardware switch. If the switch was in oscilloscope mode we read the result of the ADC and shifted this value by two to fit it on the screen. We had an integer counter, which was used to increase the x value across the screen, and drew a pixel at the shifted ADC value. Once the screen was filled horizontally, which was when x reached 320, we reset our x position and started drawing at x=0 again. If the switch is in FFT mode, we refresh the screen after every FFT calculation and display the magnitude of the spectrum.
FFT of Brain Waves
Because an FFT is very computationally intensive, we utilized a fixed-point FFT algorithm on Bruce Land's Digital Signal Processing webpage. This FFT works on 16:16 fixed point arithmetic, which is explained in the background math section. After we run the fixed point FFT algorithm, we take the magnitude of this spectrum and determine the index of the peak frequency FFT bin. From this we do the following calculation to determine the peak frequency: max_freq = max_fr_index * FFT_SIZE⁄SAMPLING_RATE. In this case, we have set FFT_SIZE to be 512, and the sampling rate is determined by how fast the timer reads ADC samples, which is 333.3 times/second.
Determining Ratios to Control LEDs
As we loop over the FFT bins and find the peak frequency index, we also sum up the values of all the bins that correspond to either alpha wave activity ("calm" states) and beta wave activity ("excited" states). For alpha wave thresholds, we have summed up bins that correspond to frequencies between 5 and 16, and for beta waves, we have summed up bins that correspond to frequencies between 17 and 40. Even though these ranges are a bit wide, according to IEEE standards, after fine-tuning our system, we felt it was important to include more data points. After averaging these respective calm and excited bins, we determine a ratio of "calm" to "excited" states of mind. This is the ratio we use to determine the LED color. As of now, our algorithm has hard-coded ratio thresholds for each of the red, yellow, and green LEDs, but we will discuss the possibility of user calibration later on this page. For our current algorithm, we turn on the green LED, indicating a higher percentage of alpha or "calm" waves, for ratios greater than 1.2. We turn on the red LED, indicating a high percentage of beta or "excited" waves for ratios less than 0.9. Between these two ranges, we set the yellow LED, indicating that there is a near equal percentage of alpha and beta waves.
Results top
Quantitative Results
On our TFT screen, we display quantitative measures of our brain waves such as peak frequency, the sum of all the calm frequency bins, the sum of excited bins, and the average ratio between these two from three FFT measurements. Depending on the orientation of the switch, we also either display the brain wave signal or the FFT spectrum. Below is an image of the TFT screen with the brain wave signals.
Below is a an image of the TFT screen displaying the FFT spectrum.
Anecdotal Results
Individually, we were able to learn quite about our individual brain waves, and factors in the environment that caused our brain to become more active or more relaxed. One interesting thing was that Vasu definitely tended to be on the more relaxed side of brain waves, frequently turning on the LED, while Namita tended to be on the more active side, and generally turned on the Red LED. These opposite brain wave patterns were quite helpful when it came to making the threshold for our LEDs. As a result of our brain waves, we were able to find what stimulated or relaxed our minds. Vasu found that she had more active brain waves when there was conflict going on around her, or when Namita was purposely irritating her. Deep breathing helped Vasu relax again. Namita got more calm when listening to music or having light conversations with others, and got stressed again when thinking about schoolwork.
Safety
Safety was extremely important for our project, because we are connecting electrical components directly to a user's body. We had to ensure that a user was ground-isolated, so that they did not get shocked. This is why we ran our amplifiers off of the battery circuit that was discussed previously. Similarly, our PIC32 was also only powered by a USB to an unplugged laptop, so that the user was not connected to an AC power source in this way. Furthermore, purpose of connecting the virtual ground from the battery circuit to the PIC ground, and to the user's arm, was to ensure that all components in this project were ground-isolated. Safety was also an important concern while testing our analog circuit. We needed a way to test each stage of the amplifier circuit before connecting electrodes; this is the reason for the test mode discussed in the previous section. Below is a picture of the entire circuit setup; the only connection to a power source is to a battery-powered laptop through the PIC32.
Usability
Our project is easy for a person to use in that they have to attach three electrodes to themselves, and press a button when they are ready to begin taking measurements. However, there are definitely aspects to our project that could have been more user-friendly. For one, our circuit is not very portable. Since it consists of two whiteboards with many wires, and a connection to a laptop, it is not very easy to move the entire set up from one spot to another. This accuracy of this measurement is also very dependent on location; certain areas are conducive to much more noise than others. For instance, we got much more 60 Hz interference in the lab room, where there is a lot of connected electrical equipment, compared to the ECE lounge, which has much less electrical equipment. As we've stated before, to counteract this interference, we twisted the jumper cables; an image of this can be seen below in the input to the amplifier circuit.
Below is a picture of Namita using the EEG.
Another issue is to the extent we can actually control the LEDs. Since brain waves are something that differ from person to person, it can take a decent amount of time to change the LEDs to red or green. As we mentioned earlier, this was difficult for us as Vasu tended to more relaxed, and Namita to more active. In fact, not only does brain wave activity vary from person to person, it also varies within one person based on factors like the amount of sleep they had gotten and how much caffeine they had consumed. One potential solution to this problem is using calibration to determine base brain frequency, which is discussed further in the Conclusions section. Below is an image of the red LED being triggered by more excited brain waves.
Speed of Execution and Accuracy
The PIC32 is running at a clock speed of 40 MHz for this project. ADC measurements are taken 333.3 times a second; because the FFT runs after 512 ADC samples, the FFT runs approximately once every 1.5 seconds, which is also how often the TFT screen updates. In terms of execution time, because we utilize fixed point arithmetic, the FFT algorithm does not have a very large delay. Furthermore, in developing an EEG, one of our primary concerns is counteracting noise. We have done this in numerous ways: with filtering circuits and twisted-pair wires in hardware, and frequency and averaging filters in software. Therefore, we have attempted to get the best possible accuracy for our measurements. For example, in performing the FFT, we experimented with some sampling rates that would result in a very accurate frequency reading measurement; we tested this by feeding in a signal from the function generator and taking the FFT of that signal, which we know the frequency of. In this way, we were able to check our frequency accuracy and found that it was within 3%. One loss of accuracy, however, comes from the physical layout of the circuit itself. Because the EEG circuit is very sensitive to movements, we found that our signal flat-lines if we move too much in a certain direction. We have, however, accounted for this, by only taking ratios of frequencies above a certain threshold. In the future, we hope to look for better sources of connection from the electrodes to our circuit.
Breathe-Easy EEG in Action
Conclusions top
Expectations and Future Modifications
Overall, we were very pleased with the outcome of our project. We managed to build a functional EEG and were able to control LEDs with our brain waves. We were able to use the TFT to show that our EEG was functional since we could display FFT data, and use the TFT as an oscilloscope to read brain waves. There are a few things we would change if we could expand on this in the future. Firstly, we would definitely solder the full circuit instead of using whiteboards, to add extra noise elimination. Also, one problem with our method of determining excited versus relaxed brain frequencies, is that this can differ from person to person. We tried to implement a calibration mode that would gauge calm versus excited frequencies by having a user just sit versus having them play a game that would frustrate them, but we were not able to get meaningful results due to time limitations.
A potential means of calibrating the thresholds tailored to the specific user involves using the first few minutes while a user is attached to the EEG to take ADC data points. By taking 3-4 minutes worth of ADC values, and then sorting these values from lowest to highest, we can get a sense of the maximum and minimum values a specific user could potentially reach. If we then take the FFT of only the highest values, and of the lowest values, we can get a sense of the most excited and the most calm any user can be. Based on this, we can set thresholds that are much more accurate.
There are many consumer devices that have been released recently that are based on controlling things with your brain waves. There is great potential, then, for EEG-based therapy. Neurofeedback has been shown to be a very useful technique in aiding in relaxation; if a user sees that they are actively relaxing, they are more likely to continue doing so. While this device is limited in scope in that it only controls LEDs, the concept can be expanded to include a variety of powerful applications.
Standards
We met all the standards we discussed in the IEEE Standards section. How we did this was discussed further in the Safety section.
Intellectual Property Considerations
Our project was inspired by the EEG Pong and EEG Magic Cat Ears projects from previous years of this class. Their amplifier and filtering circuits inspired our final circuit design. Our FFT computation code was modified from code written by Bruce Land. We utilized the protothreads library provided by the ECE 4760 course staff.
There are not patent opportunities for our project, since there are many previously existing patents relating to EEG projects and neural feedback systems. There are potential publishing opportunities for our project if we are able to expand it to control other things, which we discussed in the Future Modifications section.
Ethical Considerations
We ensured we followed the IEEE Code of Ethics to the best of our ability. With regards to making decisions consistent with the safety of the public, and to disclose promptly factors that might endanger the public, we made sure to not use any direct power source to power the circuit. As stated in our safety section, we made the amplifier circuit battery powered, while powering the TFT through a USB plugged into a laptop that is not connected to its charger. The safety section says that a user can’t plug their laptop in while using this EEG. We are also to be honest and realistic in our claims based on the data we have gotten. We recognize that due to noise and interference, not all the frequencies we are reading are necessarily a fully accurate measurement of a user’s brain waves. We believe that our project allowed us to improve our technical competence since we were able to learn about neural feedback systems, and developed further experience with hardware and software design. Finally, we sought and accepted criticism of our work from Bruce and the TA’s of ECE 4760. We incorporated their ideas about how to test our circuit, as well as followed their advice when it came to debugging the circuit. We have credited those who have directly helped us, as well as any designs that were inspired or utilized in this project in the Special Thanks section and the above Intellectual Property Considerations section.
Legal Considerations
We followed all laws to the best of our knowledge when creating our project. We ensure for safety so that no one gets harmed, and our code is either our own, or modified from open source.
Appendices top
A. Program Listing
B. Schematics
C. Parts List and Costs
Part | Vendor | Cost/Unit | Quantity | Total Cost |
---|---|---|---|---|
PIC32MX250F128B | Microchip | $5 | 1 | $5 |
MicroStickII | Microchip | $10 | 1 | $10 |
White board | In Lab | $6 | 2 | $12 |
TFT LCD | Adafruit | $10 | 1 | $10 |
Electrodes | In Lab (Generic) | $0.92 | 20 | $18.50 |
Texas Instruments INA121 | Digikey | $6.17 | 1 | $6.17 |
Texas Instruments LM358 | Digikey | $0.38 | 3 | $1.14 |
Electrode Gel | MFI (Medical Equipment Inc.) | $1.86 | 1 | $1.86 |
AA Batteries | Rayovac | $0.75 | 6 | $4.50 |
Jumper Cables | In Lab | $0.20 | 3 | $0.60 |
Wires, Resistors, Capacitors, Switches, LEDs | In Lab | $0 | Many | $0 |
TOTAL: | $69.80 |
D. Division of Labor
For the majority of this project, we worked together. In the initial stages of designing the analog circuit, we both decided on the types and values for the amplifier and filtering circuit. We also both were in lab actively debugging the circuit. In terms of software, we decided on the components to use together and debugged it together as well. The only division of labor came with the website, putting all the code together, and cleaning up the wiring.
Vasu created the schematics and diagrams for the webpage, and contributed to half of the writing. She also put together the different pieces of code we used in the final design, such as the FFT and ADC code.
Namita cleaned up all the wiring throughout the project in order to aid in debugging the hardware. She also contributed to half the writing on the webpage.
F. Special Thanks
We would like to thank Bruce Land and all the ECE 4760 TAs for their time, advice, and patience in lab. We would not have been able to complete this project without you! We would also like to thank Richard Branciforte and Maxwell Dergosits for letting us use this website template.
G. Resources
-
Datasheets
- PIC32 Data Sheet
- LM358 Operational Amplifier Data Sheet
- INA121 Instrumentation Amplifier Data Sheet
-
Vendors
- Digikey
- Medical Equipment Inc.