In order to induce Lucid dreams, the program has to detect that the subject is dreaming (refer to the previous build log for more information). As dreaming occurs in Rapid Eye Movement (REM) sleep stage, the OpenLD detects this through EEG and EOG signals. Today, I will be talking about the EEG REM detection portion of the project.
The EEG REM Detection Algorithm Overview
The OpenLD software uses an algorithm developed by Imtiaz et al. to detect REM state from the EEG signal. Detailed flowchart for this algorithm is shown below:
The Main Program section of the flowchart is identical to that of the previous Project Log. As I've explained before, the program has 2 verification steps (EEG: REM Stage and EOG: REM Eye Movements). This log will talk about the first EEG step of the verification process.
I've divided the flowcharts in an exploded, side-by-side view so that it wouldn't get too long and unreadable. The flowchart of the EEG: REM Stage is shown in the middle of the above diagram, while the flowchart for the Process EEG Signal step is shown on the far right.
Here is how the program works: After the initialization process, the program acquires 30 seconds of data from the OpenLD. Then, it converts the 24 bit raw data to the required micro-volts (uV) scale. Afterwards, it filters the scaled data with a bandpass filter with a pass-band of 0.3hz to 35hz. This filter helps to remove the voltage offset from the electrodes and mains 50hz/60hz noise.
Finally, the data is Fast Fourier Transformed and its power spectrum is calculated. Using the spectral information, the values SEFd (Spectral Edge Frequency), AP (Absolute Power), and RP (Relative Power) are determined. This statistical information is used to determine the REM sleep state of the EEG. More information about these values are explained below.
Mathematics of the REM Detection Algorithm
Note: If you do not like to delve into the mathematical details, you can ignore this discussion and skip to the next section: Decision Tree for REM Detection
The algorithm proposed by Imtiaz et al. relies on these three values: SEFd, RP, and AP. Although I give the general gist here, please refer to the paper for more information.
SEFd (Spectral Edge Frequency difference)
SEF basically is the "frequency below which a certain fraction of the signal power is contained". For example, "SEF at 50% (SEF50) is the lowest frequency below which half of the signal power is present" (Imtiaz et al.).
Here is the formula that calculates the value of SEF50. Here, the SEF50 value (x) is the frequency below of which it contains 50% of the power spectrum (Xi2) that ranges from f start to f end. (Imtiaz et al.).
and here is one for SEF95:
Here is the formula that calculates the value of SEFd. It is simply the difference between SEF95 and SEF50.
AP (Absolute Power) and RP (Relative Power)
Absolute Power is the logarithmic power spectrum from frequency fstart to fend. Its formula is given below:
Relative Power is "the ratio of the absolute powers of the signal in the range of interest and the entire signal bandwidth." (Imtiaz. et al). Since we are band-pass filtering our signal at 0.3hz to 35hz, that is our frequency bandwidth. The formula is shown below:
This is equivalent to the formula below. Remember your rule of logarithm division rule!:
Decision Tree for REM Detection
After the above calculations, the SEFd, AP, and RP values are compared against a set of constants to determine if the current signal is a REM state. If we go back to the flowchart above, we can see that there is a decision tree involving these constants.
In order for it to be an REM state:
- its SEFd value has to be bigger than SEF min constant
- its AP has to be smaller than AP max constant
- its RP value should be in between RP min and RP max constants.
If that is the case, then the subject is currently in REM state.
Now that was a rather tedious Project Log... I promise that things will now get much more exciting. In the next post, I will be talking about the actual code implementation of this algorithm and testing it out.
Sources
Imtiaz, Syed Anas, and Esther Rodriguez-Villegas. "A low computational cost algorithm for rem sleep detection using single channel EEG." Annals of biomedical engineering 42.11 (2014): 2344-2359.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.