## 2 Methods
### K-SVD Dictionary Learning Algorithm for EEG Feature Extraction  
After preprocessing, the next step in proposed method is to use the K-SVD dictionary-learning algorithm to construct a sparse dictionary of the preprocessed EEG signals during the training phase.  
K-SVD is a sparse dictionary-learning method for sparse representation developed by [PERSON] et al. [31]. K-SVD is a generalization of the k-means clustering method. It alternates between sparsely coded input data based on the current dictionary and updating the atoms in the dictionary to better fit the data. The solution model of the K-SVD algorithm is based on the \(l_{1}\) norm, and the sparse solution is achieved by restricting the sum of the absolute value of the reconstruction coefficient vector. In the context of EEG feature extraction, the constraint and objective function of the K-SVD algorithm are shown in (4), where \(\mathbf{Y}\) is the EEG signal frames, \(\mathbf{D}\) is the sparse dictionary, \(\mathbf{X}\) is the sparse coefficients matrix and \(T_{0}\) is the desired sparsity.  
\[\begin{array}{l}\min_{\mathbf{D},\mathbf{X}}\left\{\|\mathbf{Y}-\mathbf{D} \mathbf{X}\|_{F}^{2}\right\}\\ \text{s. }t.\forall i,\left\|x_{i}\right\|_{0}\leq T_{0}\end{array} \tag{4}\]  
In the field of EEG signal analysis, the sparse dictionary obtained by the K-SVD method performs better than the traditional Gabor-based sparse dictionary in terms of reduction error and computational complexity. The process of using the K-SVD method to obtain a sparse dictionary representing EEG signals is shown in Algorithm 1.  
```
0: Single-Channel EEG Singal Frames \(\mathbf{Y}\)
0: Sparse Dictionary \(\mathbf{D}\)
1:\(\mathbf{D}^{(0)}\in\mathbf{R}^{n\times k}\) as \(l_{2}\) normalized.
2: Sparse Coding Stage
3: Use Orthogonal Matching Pursuit(OMP) to obtain the Sparse Representation Vector of EEG samples, as in the following equation. \(T_{0}\) is the count of non zero elements in \(\mathbf{x}_{i}\), also the desired sparsity.
4:\(i=1,2,\dots,N,\min_{x_{i}}\left\{\ |\mathbf{y}_{i}-\mathbf{D}x_{i}|\ |_{2}^{2}\right\}\) subject to \(|x_{i}|_{0}\leq T_{0}\)
5: Dictionary Update Stage
6:for\(k=1,2,\dots,K\)do
7:\(d_{k}\) is the \(kth\) atom of \(\mathbf{D}^{(J)}\)
8: define \(\omega_{\mathbf{k}}=\left\{\mathbf{i}\ |\ 1\leq i\leq N,x_{\mathbf{t}}^{ \mathbf{k}}(i)\
eq 0\right\}\)
9: Calculate Sparse Coding Error matrix \(\mathbf{E}_{k}=\mathbf{Y}-\sum_{j\
eq k}d_{j}x_{T}^{j}\)
10: Select \(E_{K}^{R}\) according to \(\omega_{k}\)
11: perform SVD decomposition on \(\mathbf{E}_{K}^{R}\), and get \(\mathbf{E}_{K}^{R}=\mathbf{U}\triangle\mathbf{V}^{T}\)
12: update dictionary atom \(d_{k}\) : first column of \(\mathbf{U}\)
13: update \(x_{K}^{k}\) : first column of \(\mathbf{V}\) multiplied by \(\triangle\left(1,1\right)\)
14:\(J=J+1\)
15:endfor
```  
**Algorithm 1** K-SVD Dictionary-Learning Algorithm.  
Figure 1 illustrates the dictionary atom examples in the K-SVD algorithm. Those atoms have the highest cosine similarity (in abstract value) to the target ERP waveform. It can be seen that the top cosine similarity increased after iterations. This means that K-SVD algorithm extracted the ERP information into dictionary atoms.