Example 1
 figure 1 This is a schematic flowchart of the sleep state detection method based on the EEG acquisition headband provided in the first embodiment of the present invention. This embodiment can be applied to the sleep state detection using the portable EEG acquisition headband. The sleep state detection device is implemented, which specifically includes the following steps:
 S110. Obtain the EEG signals of the FP1 electrode and the FP2 electrode in the 10-20 international standard lead system;
 Exemplarily, a portable EEG signal acquisition headband can be used, and electrode pads can be placed with reference to the 10-20 system electrode placement method to obtain the EEG EEG of the user's FP1 and FP2 electrodes corresponding to the 10-20 system electrode placement method. Signal,
 figure 2 For a schematic diagram of the structure of the EEG signal acquisition head ring in the sleep state detection method based on the EEG acquisition head ring provided by the first embodiment of the present invention, see figure 2 The EEG signal acquisition head ring includes: electrode sheets and connecting devices connected in sequence for collecting EEG signals, a bioelectric signal acquisition module for EEG signal amplification and transformation, and a bioelectric signal acquisition module for controlling bioelectric signal acquisition. The ESP32 processor of the module and the WIFI wireless transmission circuit for transmitting EEG signals, the EEG signal acquisition head ring also includes a power circuit respectively connecting the bioelectric signal acquisition module and the ESP32 processor, and a retractable head for carrying content chips and circuits Ring housing. Wherein, the electrode sheet and the connecting device collect the EEG signal of the user's prefrontal lobe region, and are connected to the bioelectrical signal acquisition module through a flexible cable for acquisition and transmission of the bioelectrical signal.
 The above-mentioned bioelectric signal acquisition module is composed of an integrated high common mode rejection ratio analog input module for receiving the voltage signal collected by the EEG cap, a low-noise programmable gain amplifier (PGA) for amplifying the brain voltage signal, and a It is composed of a bioelectric signal acquisition chip of a high-resolution synchronous sampling analog-to-digital converter (ADC) that converts analog signals into digital signals.
 The above ESP32 processor is used to control the acquisition mode and parameters of the bioelectric signal acquisition module and control the WiFi wireless transmission mode and transmission speed.
 The input of the above WiFi wireless transmission circuit is the output of the bioelectric signal acquisition module, which transmits the EEG signal data to the EEG signal analysis software.
 The above-mentioned power supply circuit has an input voltage of 5V, and is powered by a lithium battery. The voltage conversion module provides different working voltages required by the system. It is also possible to directly use a USB data cable for power supply without using a lithium battery, but this will affect its portability.
 The above-mentioned retractable headgear shell includes a post-ear fixing device, a retractable device, an elastic lining, a shell and an electrode opening, which can be worn on a human head and fixed behind the ear through the fixing device. For people with different head shapes, the retractable device can be used. Adjust the tightness of the headband. There are four electrode openings in the shell for placing four electrodes to contact the human body. The four electrodes are located on the forehead and behind the ears.
 S120, processing the EEG signal to obtain an EEG epoch signal of 30s;
 First, the collected EEG signals need to be preprocessed, by intercepting the 30s EEG epoch signal to reduce the difficulty of data processing and improve the analysis efficiency of the algorithm; exemplarily, a sixth-order Butterworth with a cutoff frequency of 0.1-50Hz can be used. The Sterling bandpass filter processes the complete EEG vector to filter out noise to improve the accuracy of subsequent signal processing. Then the collected EEG signal is down-sampled from 500Hz to 100Hz. Then, the EEG vector is divided into time series segments of 30s, and the 30s EEG epoch signal can be obtained.
 image 3 A schematic flowchart of the method for acquiring the EEGepoch signal in 30s in the sleep state detection method based on the EEG acquisition headband provided in Embodiment 1 of the present invention, as shown in image 3 As shown, the EEG EEG signal can be processed by a sixth-order Butterworth band-pass filter with a cutoff frequency of 0.1-50Hz; then the processed EEG EEG signal is down-sampled from 500Hz to 100Hz, and then the EEG signal is downsampled. The complete EEG signal is divided into time series segments of 30s, and several 30s EEGepoch signals are obtained.
 S130, input the EEG epoch signal of the 30s into the trained EEG staged attention mechanism model, and obtain the approximate Hilbert change of the EEG epoch signal of the 30s, that is, the EEG signal epoch;
The Hilbert transform is an important tool in signal analysis and is very useful in signal processing systems and communication systems. The main functions are as follows: First, it is used to construct an analytical signal, so that the signal spectrum contains only positive frequency components, thereby reducing the sampling rate of the signal; A method is proposed; the third is that it can be combined with other transformations and decompositions to perform spectrum analysis of non-stationary signals.
 Exemplarily, the EEG epoch of 30s may be input to the first convolutional layer, the convolutional kernel size of the convolutional layer is Fs=N/100, 64 convolutional kernels, and the moving step size is Sd=N/1000. N is the number of sampling points included in each EEG epoch, that is, N=30*100=3000. The output of the convolutional layer is then fed into the two max-pooling layers, respectively. The size of the first pooling layer is N/1000, and its output is the frequency feature of the input EEG epoch, and the second output is of size π*N/1000, and its output is the amplitude feature of the input EEG epoch. Finally, the outputs of the two pooling layers will be summed point by point to obtain the approximate Hilbert variation of the input signal. Note that the input EEG epoch of 30s is s(t), then the Hilbert change is:
 s a (t)=s(t)+i*H(s(t))
 Among them, the real part contains frequency information, and the imaginary part contains amplitude information;
 The * here represents the convolution operation.
 Figure 4 It is a schematic structural diagram of a neural network unit of Hilbert transform in the sleep state detection method based on EEG acquisition headband provided in Embodiment 1 of the present invention; the neural network unit includes a first convolution layer and two maximum pooling layers ; Input the 30s EEG epoch signal into the first convolution layer, the convolution kernel size of the first convolution layer is Fs=N/100, 64 convolution kernels, and the moving step size is Sd=N/1000; among them, N is The number of sampling points contained in the EEG epoch signal of each 30s, that is, N=30*100=3000; the output of the first convolutional layer is input to the two maximum pooling layers respectively; among them, the first maximum pooling The size of the layer output is N/1000, and its output is the frequency characteristic of the 30s EEG epoch signal. The output size of the second maximum pooling layer is π*N/1000, and its output is the input 30s EEG epoch signal. Amplitude characteristics.
 S140. Use the encoder in the EEG staged attention mechanism model to encode the EEG signal epoch to obtain a vector mapping sequence; the encoder includes a first residual block, a second residual block, and a global average pool that are sequentially connected chemical layer.
 Figure 5 It is a schematic structural diagram of the first residual block in the sleep state detection method based on the EEG acquisition headband provided in Embodiment 1 of the present invention; such as Figure 5 As shown, the first residual block includes: the second convolution layer, the number of convolution kernels in the second convolution layer is 128, the convolution kernel size is 1*1, and the stride is 2; the third convolution layer, the first The number of convolution kernels in the third convolution layer is 64, the size of the convolution kernel is 3*1, and the stride is 1; the fourth convolution layer, the number of convolution kernels in the fourth convolution layer is 128, and the size of the convolution kernel is 1 *1, the step size is 1; the shortcut connection of the residual network of the first residual block uses the average pooling layer, the size of the average pooling layer is 2, and the step size is 2, so as to realize the conversion of 64-dimensional input into 128-dimensional output ; Sum the output of the fourth convolutional layer and the output of the average pooling layer, and then pass through the RELU function to obtain the output of the first residual block.
 Image 6 A schematic structural diagram of the second residual block in the sleep state detection method based on the EEG acquisition headband provided in Embodiment 1 of the present invention, as shown in Image 6 As shown, the second residual block includes: the fifth convolution layer, the number of convolution kernels in the fifth convolution layer is 128, the size of the convolution kernel is 1*1, and the stride is 2; the sixth convolution layer, the first The number of convolution kernels of the six convolutional layers is 64, the size of the convolution kernels is 3*1, and the stride is 1; for the seventh convolutional layer, the number of convolution kernels of the seventh convolutional layer is 128, and the size of the convolution kernels is 1*1, the step size is 1; among them, the direct connection is used for the shortcut connection of the residual network of the second residual block; the output of the seventh convolutional layer and the direct connection are summed, and then after the RELU function, the second residual is obtained. The output of the difference block.
 Compared with the traditional CNN, the last layer is a fully connected layer, and the number of parameters is very large, which is easy to cause overfitting (such as Alexnet). In a CNN model, most of the parameters are occupied by the fully connected layer, so this The embodiment proposes to use global mean pooling to replace the fully connected layer. Unlike traditional fully connected layers, we Figure 1 The entire image is globally mean pooled so that each feature map can get an output. In this way, the use of mean pooling can omit parameters, which can greatly reduce network parameters and avoid overfitting. On the other hand, it has a feature that each feature map is equivalent to an output feature, and then this feature represents our output class. feature.
 Specifically, the advantages of global average pooling include: by enhancing the consistency between feature maps and categories, the convolution structure is simpler; no parameter optimization is required, so this layer can avoid overfitting; The summation is thus more stable to the spatial transformation of the input, so the processing efficiency of the EEG staged attention mechanism model can be effectively improved by using a global average pooling layer.
 S150. Use the decoder based on the multi-head attention mechanism in the EEG staged attention mechanism model to decode the vector map sequence; the decoder includes a multi-head attention sub-module connected in sequence, a first fully connected layer, and a second full connection layer;
 The resulting sequence of vector maps after encoding is decoded by a multi-head attention submodule and two fully connected layers.
 Exemplarily, the output of the encoder and the position encoding PE can be summed and normalized, and then input to the multi-head attention mechanism sub-module of the decoder; wherein, the calculation method of the position encoding PE is as follows:
 where p represents the position of the current EEG signal epoch in the input EEG signal epoch queue; d represents the number of EEG signal epochs contained in the input queue input to the multi-head attention mechanism sub-module; dim represents the global average pooling layer The number of output data; the position mapping of each EEG signal epoch can be directly calculated by the above equation without training, thus speeding up the training and helping the algorithm to learn transition rules from two directions.
 After the input and output of the decoder are summed and normalized by residual connection, they are input to the second DropOut layer; wherein, the DropOut probability of the second DropOut layer is 0.5;
 The output of the second DropOut layer is input into the SoftMax layer, and the SoftMax layer outputs the sleep stage staging results.
 Compared with the traditional method, this embodiment introduces a multi-head attention mechanism. Traditional EEG analysis methods either do not pay attention to cross-time analysis, that is, when analyzing the current epoch, do not pay attention to the impact of the epoch before and after the epoch, or often use LSTM for analysis, but LSTM cannot perform parallel computing and will prolong the training process. The multi-head attention mechanism enables parallel computation and shortens the training process while performing cross-temporal analysis.
 S160. Obtain the sleep stage staging result output by the EEG staging attention mechanism model.
 The sleep state of the user can be obtained by collecting and analyzing the EEG signals, which can then provide a reference for the user to understand their own sleep health state; the sleep state based on the EEG acquisition headband provided in this embodiment The detection method can realize the accurate collection, accurate identification and correct classification of EEG signals, and classify the user's sleep state by identifying the user's EEG signal, so as to provide the user with a reference to understand his own sleep health state.