## 3 Methods
### EEG-Based Concentration Level Detection
}\)
18: where \(\mathbf{B}=\text{logm}(\mathbf{S})\)
19:Step 4: Compute Spectral Features
20: Fast Fourier Transform (FFT):
21: FFT\((w_{t})=\sum_{n=0}^{N-1}y_{n}e^{-\beta\pi\mathbf{b}t/N}\)
22: Extract the top 10 most energetic frequency components:
23: getFFT\((w_{t},10)=\arg\max_{f}|\text{FFT}(w_{t})|\)
24:Step 5: Construct Final Feature Vector: \(F=\{\hat{y},g_{1},g_{2},s_{k\ell},\lambda,\text{logm}(\mathbf{S}),\text{FFT}( w_{t})\}\)
25:Step 6: EEG Classification using CNN each EEG frequency band \(b\in\{\alpha,\beta,\theta,\gamma,\delta\}\)
26: Apply CNN with layers:
27: Convolution: \(Z^{(l)}=\text{ReLU}(W^{(l)}X+b^{(l)})\)
28: Pooling: \(P^{(l)}=\max(Z^{(l)})\)
29: Flattening: \(F^{*}=\text{Flatten}(P^{(l)})\)
30: Fully Connected Dense Layer: \(D^{(l)}=\text{ReLU}(W_{d}F^{*}+b_{d})\)
31: Dropout: \(D^{(l)}=\text{Dropout}(0.5)\)
32: Concatenate CNN outputs into a feature vector \(F_{\text{CNN}}\)
33: Apply final SoftMax classifier:
34:\(C=\arg\max(\text{SoftMax}(Z))\)
35:Return C
```  
**Algorithm 1** EEG Feature Extraction and Classification  
EEG Data Augmentation Using WGAN-GP  
Acquiring large-scale EEG datasets is inherently challenging due to privacy concerns, ethical considerations, and the complexity of long experimental setups. These limitations often result in small and unbalanced datasets, making it difficult to train deep models effectively. To address this challenge, we employed a Wasserstein Generative Adversarial Network with Gradient Penalty (WGAN-GP) [19] to generate realistic synthetic EEG data, mitigating data scarcity while enhancing classification. The WGAN-GP framework comprises a generator \(G(z)\) that learns the underlying distribution of real EEG samples and a discriminator \(D(x)\) that distinguishes between real and synthetic signals. By optimizing the Wasserstein distance with gradient penalty, a WGAN-GP ensures improved convergence stability and better quality of synthetic EEG signals. Algorithm 2 presents all the steps of a WGAN-GP. ```
1:Input: Training data distribution \(P_{data}\), noise distribution \(P_{z}\)
2:Output: Trained generator \(G_{\theta}\)
3:Step 1: Define Generator and Discriminator Models
4:Generator:
5:\(\mathbf{G}_{\theta}(z)=\mathrm{ReLU}(\mathbf{W}_{4}\cdot\mathrm{ReLU}( \mathbf{W}_{3}\cdot\mathrm{ReLU}(\mathbf{W}_{2}\cdot\mathrm{ReLU}(\mathbf{W}_ {1}\cdot z)))))\)
6:Discriminator:
7:\(\mathbf{D}_{\omega}(x)=\mathrm{LeakyReLU}(\mathbf{W}_{4}\cdot\mathrm{Dropout}( \mathrm{LeakyReLU}(\mathbf{W}_{3}\cdot\mathrm{Dropout}(\mathrm{LeakyReLU}( \mathbf{W}_{2}\cdot\mathrm{Dropout}(\mathrm{LeakyReLU}(\mathbf{W}_{1}\cdot x))))))\)
8:Step 2: Initialize Models and Optimizers
9:Initialize generator \(G_{\theta}\) and discriminator \(D_{\omega}\)
10:Initialize Adam optimizers with learning rate \(lr\) and \(\beta\)
11:Step 3: Training Loop epoch = \(1,\dots,num\_epochs\)
12:Randomly shuffle training data indices \(i=1,\dots,m\)
13:Sample a batch of \(m\) real data: \(x^{(i)}\sim P_{data}\)
14:Sample a batch of \(m\) noise vectors: \(z^{(i)}\sim P_{z}\)
15:Generate fake data: \(\tilde{x}=G_{\theta}(z)\)
16:Interpolate data: \(\hat{x}=\epsilon x+(1-\epsilon)\tilde{x}\), where \(\epsilon\sim U(0,1)\)
17:Compute gradient penalty:
18:\(\
abla_{\tilde{x}}D_{\omega}(\hat{x})\),
19:\(L_{GP}=\lambda\left(\|\
abla_{\tilde{x}}D_{\omega}(\hat{x})\|_{2}-1\right)^{2}\)
20:Compute discriminator loss:
21:\(L_{D}=D_{\omega}(\tilde{x})-D_{\omega}(x)+L_{GP}\)
22:Update discriminator:
23:\(\omega\leftarrow\omega-\eta\
abla_{\omega}L_{D}\)
24:Every \(k\) iterations, update the generator: \(i\mod(5\times m)==0\)
25:Generate fake data: \(\tilde{x}=G_{\theta}(z)\)
26:Compute generator loss: \(L_{G}=-D_{\omega}(\tilde{x})\)
27:Update generator:
28:\(\theta\leftarrow\theta-\eta\
abla_{\theta}L_{G}\)
29:Return: Trained generator \(G_{\theta}\)
```  
**Algorithm 2** WGAN-GP Training Algorithm