# Detection of Movement and Lead-Popping Artifacts in Polysomnography EEG Data
## 2 Methods
### Algorithm
#### 2.1.2 Step-by-Step Description  
Preprocessing  
Our data preprocessing procedure is designed to make the input time series of potentials amenable to subsequent analyses. The algorithm uses EEG signals from six channels (F3, F4, C3, C4, O1, and O2) and preprocesses each one separately. First, it identifies 30 s epochs labeled as \"Nan\" or \"inf\" and recodes them as \"NaN\", thereby making them irrelevant for downstream analysis. Each channel is re-referenced with respect to its opposite mastoid channel (M1 or M2) by subtraction, e.g., F3-M2 and F4-M1. The algorithm applies a zero-phase notch filter to remove power-line noise (60 Hz). Then the algorithm applies a band-pass filter to keep signals within the frequency range of 0.5-32.5 Hz. It removes 30 s epochs with excessive flat signals, that is, epochs with amplitudes one standard deviation less than 1 \(\upmu\)V or less than 0.2 \(\upmu\)V for five consecutive second. To minimize movement artifacts, it also removes epochs with signal amplitudes exceeding 500 \(\upmu\)V.  
Spectral Analysis  
Next, we carried out multitaper spectrogram (MT-spectrum) analysis [18] on the preprocessed EEG signals. For each channel, we extracted the EEG power spectrum with seven Slepian tapers using a moving window of 4 s with 3 s overlap; we indexed the period corresponding to each 4 s spectrum, termed a \"segment,\" to its initial second. Consequently, a segment is 1 s in duration. The spectrum for each segment consists of absolute EEG power in 128 bands, each with a 0.25 Hz resolution. Thus, we regard each segment as having a vector of power values with 128 elements. Segment-Specific Correlation between Pairs of Channels  
For a given pair of channels, we have two vectors of EEG power for each segment, as described above. Using those vectors, we compute the Spearman correlation for each segment of the sleep period, thereby creating a time series of correlations for the pair of channels. We create a corresponding pairwise correlation time series for all 15 possible pairs from the six channels. Each of the six channels is paired with five other channels. We average the five correlations, segment by segment, to create a time series of average correlations for each  
Figure 1: Overall workflow of the proposed method. For raw EEG data, the algorithm first performs the preprocessing step. Next, the algorithm computes pairwise correlation coefficients in EEG power between every pair of channels. For each channel, the correlations with the other channels are averaged. Based on the correlation coefficients, the algorithm iteratively identifies channels with excessive outliers and recomputes the average correlation. Finally, outlier segments are identified and annotated. index channel. For example, for index channel C3, we averaged all correlations involving C3, i.e., correlations between C3-C4, C3-F3, C3-F4, C3-O1, and C3-O2. In subsequent iterations, pairwise correlations that involve a channel that subsequently recognized as having an excessive number of outlying segments (see below for definition of artifact channel) are excluded from the calculation of average correlations. Thus, after the first iteration, the average correlation for a given channel may involve fewer than five pairwise correlations. Identification of \"good\" and \"bad\" segments. For a given channel, the algorithm characterizes each segment as \"good\" or \"bad\" using a local moving window-based approach applied to the channel's average correlation time series. We chose a window size of 60 s (two 30 s epochs) and a sliding time of 1 s. Following [PERSON] et al. [9], we employed a locally adaptive threshold. We designate a segment in the moving window as a \"bad\" segment when its average correlation is less than half of the median of the average correlation for all segments in the window. Each segment declared \"bad\" is placed in the \"bad\" segment pool. All remaining segments, considered \"good\" segments, are placed in the \"good\" segment pool.