## 2 Materials and Methods
### Pain Detection Explainability Through Frequency Class Activation Maps (FCAM)  
Gradient-weighted Class Activation Mapping++ (Grad-CAM++) is an advanced deep learning interpretability technique that generates high-resolution heatmaps to localize important regions within an input image more accurately than traditional CAM approaches [43]. By leveraging higher-order gradients, Grad-CAM++ provides refined localization and a more detailed understanding of model decisions. In our approach, we integrate Grad-CAM++ into an EEG-based pain detection framework, representing  
Figure 3: Kernel Cross-Spectral Functional Connectivity network (KCS-FCnet) backbone for EEG-based pain detection. Our proposal aims to support non-verbal patient pain detection through brain activity analysis (no-pain vs. high-pain).  
each trial \(\mathbf{X}\) as an image with \(C\) rows (channels) and \(T\) columns (time steps) to enhance interpretability and feature relevance analysis.  
Here, an upsampled EEG-CAM, denoted as \(\mathbf{\Lambda}_{k}^{\varphi}(\mathbf{X})\in\mathbb{R}^{C\times T}\), is computed for an input trial \(\mathbf{X}\) corresponding to the \(k\)-th class (\(k\in\bar{K}\)). This CAM is derived from the \(\varphi(\cdot)\) layer in Equation (5) to highlight salient spatio-temporal patterns encoded by KCS-FCnet:  
\[\mathbf{\Lambda}_{k}^{\varphi}(\mathbf{X})=\text{ReLU}\Bigg{(}\zeta\Bigg{(}\sum_{f=1 }^{\bar{f}}\mathbf{\theta}_{kf}^{\varphi}\odot\mathbf{\mathcal{Z}}_{kf}^{\varphi} \Bigg{)}\Bigg{)}, \tag{9}\]  
\(\zeta(\cdot)\) denotes an upsampling function, \(\text{ReLU}(\cdot)\) is the Rectified Linear Unit, \(\tilde{\mathbf{Z}}_{kf}^{\varphi}\in\mathbb{R}^{C\times\Delta_{T}}\) represents the activation map for the \(f\)-th 1-D convolutional filter regarding the \(k\)-th class, and \(\odot\) stands for the Hadamard product. The Grad-CAM++ weights \(\mathbf{\beta}_{kf}^{\varphi}\) are computed as follows:  
\[\mathbf{\beta}_{kf}^{\varphi}=\sum_{c=1}^{C}\sum_{t=1}^{\Delta_{T}}\alpha_{kf}[c, t]\cdot\text{ReLU}\Bigg{(}\frac{\partial\mathbf{\vartheta}_{k}}{\partial\tilde{ \mathbf{Z}}_{kf}^{\varphi}[c,t]}\Bigg{)}, \tag{10}\]  
where \(\bar{y}_{k}\in\mathbb{R}^{+}\) is the \(k\)-th class score, and \(\alpha_{kf}[c,t]\) are the Grad-CAM++ coefficients calculated using higher-order partial derivatives of \(\bar{y}_{k}\) with respect to \(\tilde{\mathbf{Z}}_{kf}^{\varphi}[c,t]\).  
We normalize the EEG-CAM to focus on important spatial and temporal EEG inputs and reduce unwanted CAM artifacts between classes, as follows:  
\[\mathbf{\Lambda}_{k}^{\varphi}(\mathbf{X})=\frac{\mathbf{\Lambda}_{k}^{\varphi}(\mathbf{X})}{ \max\limits_{k^{\prime}\in\bar{K}}\mathbf{\Lambda}_{k^{\prime}}^{\varphi}(\mathbf{X})}. \tag{11}\]  
with \(\tilde{\mathbf{\Lambda}}_{k}^{\varphi}(\mathbf{X})\in[0,1]^{C\times T}\).  
Lastly, to reveal explainable spatio-frequency patterns, we propose to compute the Frequency CAM (FCAM) vector \(\mathbf{\gamma}_{k}\in\mathbb{R}^{C}\) as follows:  
\[\mathbf{\gamma}_{k}(\bar{\Omega})=\mathcal{F}^{-1}\{\mathcal{F}\{\mathbf{\Lambda}_{k }^{\varphi}(\mathbf{X})\};\bar{\Omega}\}\mathbf{1}_{T}, \tag{12}\]  
where \(\bar{\Omega}\) rules the frequency band of interest and \(\mathbf{1}_{T}\) is an all-ones vector of size \(T\).