Automatic Detection of the Cyclic Alternating Pattern of Sleep and Diagnosis of Sleep-Related Pathologies Based on Cardiopulmonary Resonance Indices

The cyclic alternating pattern is the periodic electroencephalogram activity occurring during non-rapid eye movement sleep. It is a marker of sleep instability and is correlated with several sleep-related pathologies. Considering the connection between the human heart and brain, our study explores the feasibility of using cardiopulmonary features to automatically detect the cyclic alternating pattern of sleep and hence diagnose sleep-related pathologies. By statistically analyzing and comparing the cardiopulmonary characteristics of a healthy group and groups with sleep-related diseases, an automatic recognition scheme of the cyclic alternating pattern is proposed based on the cardiopulmonary resonance indices. Using the Hidden Markov and Random Forest, the scheme combines the variation and stability of measurements of the coupling state of the cardiopulmonary system during sleep. In this research, the F1 score of the sleep-wake classification reaches 92.0%. In terms of the cyclic alternating pattern, the average recognition rate of A-phase reaches 84.7% on the CAP Sleep Database of 108 cases of people. The F1 score of disease diagnosis is 87.8% for insomnia and 90.0% for narcolepsy.


Introduction
Sleep, which accounts for nearly a third of human life, is an important function that helps the body to recover. It has been proven that sleep could help to reduce stress, regulate hormone balance, stabilize appetite and cardiovascular function [1][2][3]. At the same time, sleep is essential for the recovery of the brain function, which is closely related to brain development, learning, memory and mental health of human beings [4]. A lack of sleep will cause different degrees of harm to the body and mind [5]. The monitoring of sleep and the detection of sleep-related diseases are of great significance in people's daily life as well as in clinical treatment.
Sleep is known to rhythmically regulate autonomic nervous system activity. Quiet sleep is associated with increased parasympathetic arousal and activity, while rapid eye movement (REM) sleep is relevant to the increased sympathetic activity [6]. The sleep structure is based on the cyclical alternation of two main neurophysiological states: REM and NON-REM (NREM) sleep [7]. The alternations of non-REM and REM sleep constitute the sleep cycle, and its recurrence during the night determines the classical progressive sleep mode.
Many researchers suggest that both slow and fast electroencephalogram (EEG) activation complexes are involved in the structural organization of sleep [8]. Deep sleep is established and maintained through a process of periodic EEG instability accompanied by mild autonomic fluctuations in the wake state [9]. In contrast, the breakdown of slow-wave sleep and the introduction of REM sleep are mainly associated with EEG desynchronization and strong activation of muscle and autonomic nerve functions [10].
The Cyclic Alternating Pattern (CAP) is the periodic EEG activity occurring during NREM sleep. It is characterized by cyclic sequences of cerebral activation (phase A) followed by periods of deactivation (phase B) which separate two successive phase A periods with an interval <1 min [11]. Phase A period together with the following phase B period define a CAP cycle [12]. Detailed investigation has ascertained that the spontaneous EEG fluctuations of CAP are implicated in the subtle mechanisms that regulate the production and attenuation of slow-wave activities during sleep [13]. Different components of CAP have a sculpturing effect on the profile of the sleep cycle.
Phase A periods are subdivided into three subtypes. The abundance of A1 subtype in the descending branches and grooves may be an EEG expression of brain mechanisms involved in release activity, while the dominance of A2 and A3 subtypes in the ascending branches reflects the REM-on drive [14]. Therefore, in addition to a variety of EEG features, the activation complex also has non-random distribution characteristics at night, and has obvious periodicity in NREM sleep within the CAP framework, e.g., [15][16][17]. CAP is regarded as the main expression of sleep microstructure. CAP can be recognized in the sleep of both adults and children and it is a sensitive tool for studying sleep disorders throughout the life cycle [18,19].
Several efforts have been made to develop a reliable automatic CAP-scoring algorithm [20][21][22][23]. Most of these methods rely on the extraction of spectral features from the EEG and on the application of machine-learning algorithms, such as the k-nearest neighbor, support vector machine, artificial neural network, decision trees, and deep neural network [24,25]. However, EEG acquisition needs to be carried out under the guidance of experts, and the wearing of the equipment will also affect the sleep state of the subjects [26]. The preprocessing [27], recognition [28] and analysis [29] of EEG signal are complicated [30].
Some researchers and institutions have tried to classify sleep stages by physiological signals instead of EEG. As physiological signals that contain the physiological characteristics and autonomic nerve status information of the human body, Electrocardiograph (ECG), respiration and the three-axis acceleration signals on the chest have attracted much attention [31][32][33]. Wilhelm Daniel Scherz et al. used Heart Rate Variability (HRV) characteristics of three transformation domains to determine sleep stages [34]. Mourad Adnane et al. attempted to extract heart rate variability features from time domain, frequency domain, detrend fluctuation analysis and window detrend fluctuation analysis, and used a support vector machine to classify sleep and wake stage, with an average accuracy of 79.3% [35]. Martin Oswaldo Mendez et al. extracted HRV and body movement using a time-varying auto-regression model, and classified REM and NREM using hidden Markov model [36], and the time-varying relationship between EEG and HRV are studied [37]. Eline R.de Groot et al. researched on the value of cardiorespiratory parameters for sleep state classification in preterm infants [38].
Considering the connection between the heart and brain of people [39][40][41], the CAP, which derives from the transformation of EEG, affects the excitability of human nerves and then reflects not only in body movements and changes of heart rate, but also in respiration [42,43]. Cardiopulmonary characteristics of people show diverse manifestations in different stages of the sleep. Inspired by the causality analysis of the modulation of heart rate form respiration, cardiopulmonary resonance indices (CRI) are defined to measure the status of the cardiopulmonary resonance system by adopting the cardiopulmonary system circuit model based on the causality analysis in frequency domain [42].
In this research, we first expanded the index system of CRI, combined with the physiological significance of the cardiopulmonary coupling model. CRI in different sleep stages of different people were analyzed in detail in this paper. Inspired by the results of the statistical analysis of the cardiopulmonary characteristics of healthy people and patients with sleep-related diseases, we proposed an automatic recognition and disease diagnostic scheme of CAP based on CRI. The scheme combines the variation and stability of CRI using Hidden Markov [44] and Random Forest model [45]. In addition to the recognition of CAP, this scheme can also diagnose sleep-related diseases through the cardiopulmonary characteristics. Compared with the features of the directly acquired signals, such as ECG, three-axis acceleration signals, etc., the proposed method explores the deep rhythm in the cardiopulmonary system during human sleep, and is effective to conquer the weaknesses in signal acquisition and processing in clinical trials. At the same time, this study also proves the existence of the mind-brain connection in the human body.
In this paper, for materials and methods, Section 2 describes the database used in the study and the data preprocessing before calculating CRI. Section 3 describes the CRI and expands the index system, and statistical analysis of the cardiopulmonary characteristics of people with non-pathology and patients with sleep-related diseases, then gives the classification and recognition scheme. Sections 3 and 4 give the details and effects of the methods. The last section is the conclusion and future work.

Data
This section describes the data set and the data processing in this research.

Data Set
The experimental data used in this study is obtained from the CAP Sleep Database [46] in MIT-BIH database. The CAP Sleep Database is a collection of 108 polysomnographic recordings registered at the Sleep Disorders Center of the Ospedale Maggiore of Parma, Italy. The waveforms include at least three EEG channels, Electro-Oculogram (EOG) (two channels), Electromyography (EMG) of the submentalis muscle, bilateral anterior tibial EMG, respiration signals, the acceleration and gyroscope signals in the chest and ECG [47].
The database includes 108 cases of people with non-pathology and several sleeprelated pathologies. Expert neurologists who trained at the Sleep Center provided the scoring of the sleep macrostructure, according to the Rechtschaffen & Kales rules [48], including SLEEP−REM, SLEEP−S0, SLEEP−S1, SLEEP−S2, SLEEP−S3, SLEEP−S4, while the CAP was detected in agreement with Terzano's reference atlas of rules [49], including MCAP−A1, MCAP−A2 and MCAP−A3 in the NREM period every 30 s. Sleep stages S0, S1 and S2 are collectively called light sleep stage, while S3 and S4 are referred to as deep sleep stage.
The Figure 1 below is an example of the EEG in sleep stage 2. Phases A1, A2 and A3 are framed in the diagram according to the labels from experts. As shown in Figure 1, the Phase A periods are subdivided into three subtypes [50]: Subtype A1: synchronized events with low impact on autonomic and somatomotor activities; Subtype A2: mixed synchronized-desynchronized EEG events with an intermediate influence on the autonomic and somatomotor activities; Subtype A3: predominantly desynchronized EEG events with heavy effects on the autonomic and somatomotor activities.

Data Selection and Preprocessing
The ECG and respiration signals of people with non-pathology, insomnia and narcolepsy are used in this study. For the signals in the database, stationary signals are selected for analysis.
Wavelet analysis is used to remove the baseline drift of the signal and a Pasteur bandpass filter is used to remove the noise. Then, for respiratory signals, we define the signal quality function as follows: where res represents the respiratory signal, psd represents power spectral density function. Q value expresses the concentration degree of the power spectrum of respiratory signal, and the stability of respiratory frequency. Considering the respiratory signal is concentrated in the frequency domain from 0.03 Hz to 0.5 Hz [51], the signals which meet the requirement that Q > 0.85 are chosen [43,52]. This process ensures the quality of the respiratory signal to be representative of the breathing condition of the subject.

Data Selection and Preprocessing
The ECG and respiration signals of people with non-pathology, insomnia and narcolepsy are used in this study. For the signals in the database, stationary signals are selected for analysis.
Wavelet analysis is used to remove the baseline drift of the signal and a Pasteur band-pass filter is used to remove the noise. Then, for respiratory signals, we define the signal quality function as follows: where res represents the respiratory signal, psd represents power spectral density function. Q value expresses the concentration degree of the power spectrum of respiratory signal, and the stability of respiratory frequency. Considering the respiratory signal is concentrated in the frequency domain from 0.03 Hz to 0.5 Hz [51], the signals which meet the requirement that Q > 0.85 are chosen [43,52]. This process ensures the quality of the respiratory signal to be representative of the breathing condition of the subject. As for the ECG, to ensure RR intervals (intervals between adjacent R waves) in the normal range and without a mutation because of premature beat or other physiological phenomena [53], we use interpolation as a substitute for points that do not meet the following conditions based on the Gaussian model [54]: where RRI is the RR intervals, RRIi−1 and RRIi are adjacent intervals. After processing ECG and respiratory signals, we calculated CRI sequences taking the step size of 10 s with the window of 120 s.

Cardiopulmonary Resonance Indices (CRI)
Cardiopulmonary interaction is important in the circulation system to ensure efficient delivery of oxygen and nutrients, and that the efficiency is optimized at the state of cardiopulmonary resonance. Cardiopulmonary resonance indices (CRI) come from the bivariate autoregressive model of respiration series and RR intervals. The model could As for the ECG, to ensure RR intervals (intervals between adjacent R waves) in the normal range and without a mutation because of premature beat or other physiological phenomena [53], we use interpolation as a substitute for points that do not meet the following conditions based on the Gaussian model [54]: where RRI is the RR intervals, RRI i−1 and RRI i are adjacent intervals. After processing ECG and respiratory signals, we calculated CRI sequences taking the step size of 10 s with the window of 120 s.

Cardiopulmonary Resonance Indices (CRI)
Cardiopulmonary interaction is important in the circulation system to ensure efficient delivery of oxygen and nutrients, and that the efficiency is optimized at the state of cardiopulmonary resonance. Cardiopulmonary resonance indices (CRI) come from the bivariate autoregressive model of respiration series and RR intervals. The model could calculate respiratory along with the non-respiratory component effects on RR intervals in the frequency domain based on G-causality [42].
In the model, the change process of RRI was regarded as a Markov process, ignoring other factors affecting heart rate in the short term, the RRI and RSP (both of length T) were described by a bivariate auto-regressive model [43]: where p is the maximum number of lagged observations included in the model (the model order, p < T). A contains the coefficients of the model, and ε 1 , ε 2 are the residuals for each time series. By the definition of G-causality, the magnitude of RSA can be measured by the log ratio of the prediction error variances for the restricted (R) and unrestricted (U) models: where ε 1R (12) is derived from the model omitting the A 12,j (for all j) coefficients in the first equation and ε 1U is derived from the full model [21]. In order to describe the effect of respiration on heart rate better, and to compare the model with Heart Rate Variability (HRV), the model conducted the operation above in the frequency domain. Thus, through the model, we obtained a curve G(f) which measures the cardiopulmonary interaction in the frequency domain.
Based on G(f) in the frequency domain, CRI are defined to measure the status of the cardiopulmonary resonance system by adopting the cardiopulmonary system circuit model [42,55,56] (Figure 2).
where p is the maximum number of lagged observations included in the model (the model order, p < T). A contains the coefficients of the model, and ε1, ε2 are the residuals for each time series.
By the definition of G-causality, the magnitude of RSA can be measured by the log ratio of the prediction error variances for the restricted (R) and unrestricted (U) models: where ε1R (12) is derived from the model omitting the A12,j (for all j) coefficients in the first equation and ε1U is derived from the full model [21]. In order to describe the effect of respiration on heart rate better, and to compare the model with Heart Rate Variability (HRV), the model conducted the operation above in the frequency domain. Thus, through the model, we obtained a curve G(f) which measures the cardiopulmonary interaction in the frequency domain.
Based on G(f) in the frequency domain, CRI are defined to measure the status of the cardiopulmonary resonance system by adopting the cardiopulmonary system circuit model [42,55,56] (Figure 2). CRI include cardiopulmonary resonance amplitude (CRA), cardiopulmonary resonance bandwidth (CRB) and cardiopulmonary resonance quality factor (CRQ) shown in Figure 3. CRA represents the coupling depth of the cardiopulmonary system, CRB represents the width of the coupling frequency band, and CRQ represents the current quality of the cardiopulmonary system. CRI are the quantitative measurements for respiratory sinus arrhythmia (RSA) in the frequency domain [42]. Reflecting the modulation effect of breathing on heart rate changes, CRI well represent the degree of cardiopulmonary resonance and parasympathetic nerve activity level. They have been proved valid in the classification of NON-REM (NREM) and REM sleep [42] and in the antepartum autonomic nervous care in pregnant women [43]. CRI include cardiopulmonary resonance amplitude (CRA), cardiopulmonary resonance bandwidth (CRB) and cardiopulmonary resonance quality factor (CRQ) shown in Figure 3. CRA represents the coupling depth of the cardiopulmonary system, CRB represents the width of the coupling frequency band, and CRQ represents the current quality of the cardiopulmonary system. CRI are the quantitative measurements for respiratory sinus arrhythmia (RSA) in the frequency domain [42]. Reflecting the modulation effect of breathing on heart rate changes, CRI well represent the degree of cardiopulmonary resonance and parasympathetic nerve activity level. They have been proved valid in the classification of NON-REM (NREM) and REM sleep [42] and in the antepartum autonomic nervous care in pregnant women [43]. CRQ is defined to measure the merit of the cardiopulmonary resonance system by adopting the quality factor measure for inductor, capacitor, and resistor LCR oscillator. Combined with the physiological significance of the cardiopulmonary coupling model, we expanded the index system with non-respiration factors, which is an analogy with the energy consumption element in the circuit model, as cardiopulmonary resonance resistance (CRR). CRQ is defined to measure the merit of the cardiopulmonary resonance system by adopting the quality factor measure for inductor, capacitor, and resistor LCR oscillator. Combined with the physiological significance of the cardiopulmonary coupling model, we expanded the index system with non-respiration factors, which is an analogy with the energy consumption element in the circuit model, as cardiopulmonary resonance resistance (CRR).
In the oscillating system, the quality factor of a circuit is the ratio between the reactance power and the average power consumed by the resistor, namely the ratio of reactive power and active power. In the non-radiative system, the capacitive reactance is equal to the inductive reactance at resonance, then we can deduce [57]: where F A represents the cardiopulmonary resonance frequency. Then we define the cardiopulmonary resonance resistance (CRR) as follows [57]: Cardiopulmonary resonance resistance (CRR) represents the physiological phenomena of people that exist in the heart rate variability excluding the respiratory modulation, and is related to pathological state of the subject.
CRA, CRB, CRQ and CRR jointly constructed the evaluation index system of cardiopulmonary coupling. In the following analysis, we constructed the sleep analysis scheme by using the performance of the four indicators (including CRQ, CRR, CRB and CRA) in different groups. The significant difference of the CRI in A-phase and non-A phase (NA-phase) period were detected by the repeated one-way ANOVA test.

CAP Recognition and Disease Diagnostic Scheme
Inspired by the results of the statistical analysis of the cardiopulmonary characteristics of healthy people and patients with sleep-related diseases, we propose an automatic recognition and disease diagnostic scheme of CAP based on CRI. Using the Hidden Markov and Random Forest model, the scheme combines the values and stability of characteristics which measure the status of cardiopulmonary system of people during sleep. Precision and recall rates will be calculated in the next section.
The flow of the classification and diagnosis scheme is shown in the Figure 4 above. In our work, movement characteristics and cardiopulmonary characteristics are extracted every 30 s, along with improved Hidden Markov and Random Forest model, to classify the sleep stages. Support Vector Machine (SVM) is then used to diagnose diseases.
The classification algorithm in this paper uses a total of 21 features, consisting of three body movement (BM) features, 12 heart rate variability (HRV) features and six CRI features. Each kind of data is preprocessed.
Body movement (BM) features [58] include the variance, approximate entropy and sample entropy of three cycle acceleration signals. HRV features used in this paper can be split into three categories following existing research [59]. Three time-domain features: SDNN, RMSSD, pNN50. Four frequency-domain features: VLF power, LF power, HF power, LF/HF ratio. Five nonlinear features: SD1, SD2 and SD1/SD2 from Poincare plot, as well as sample entropy and approximate entropy. CRI features include follows: CRR, CRB, CRQ, CRA, the variance of CRA, the entropy of CRA.
During the training process, to improve the effectiveness of the extracted features, for the sleep stage WEAK, LIGHT SLEEP (stages S1 and S2) and DEEP SLEEP (stages S3 and S4), 20 consecutive tagged data is selected as the training data, that is, the data remaining unchanged in sleep status within 10 min is selected as the initial training data. For the identification of A1-phase, A2-phase and A3-phase, all the data are used. When the value of the calculated feature exceeds the normal range, the data fragment is preprocessed again, and the outliers are compensated until the eigenvalue is normal.  The classification algorithm in this paper uses a total of 21 features, consistin three body movement (BM) features, 12 heart rate variability (HRV) features and six features. Each kind of data is preprocessed.
Body movement (BM) features [58] include the variance, approximate entropy sample entropy of three cycle acceleration signals. HRV features used in this paper be split into three categories following existing research [59]. Three time-domain tures: SDNN, RMSSD, pNN50. Four frequency-domain features: VLF power, LF pow HF power, LF/HF ratio. Five nonlinear features: SD1, SD2 and SD1/SD2 from Poin plot, as well as sample entropy and approximate entropy. CRI features include follo CRR, CRB, CRQ, CRA, the variance of CRA, the entropy of CRA.
During the training process, to improve the effectiveness of the extracted featu for the sleep stage WEAK, LIGHT SLEEP (stages S1 and S2) and DEEP SLEEP (stage and S4), 20 consecutive tagged data is selected as the training data, that is, the data maining unchanged in sleep status within 10 min is selected as the initial training d For the identification of A1-phase, A2-phase and A3-phase, all the data are used. W the value of the calculated feature exceeds the normal range, the data fragment is processed again, and the outliers are compensated until the eigenvalue is normal.
The scheme adopts the combination of Hidden Markov and Random Forest mo to fully utilize the variation and stability of features.
The Hidden Markov Model is used to characterize both the stability and the va tion of CRA between different sleep stages, and the final recognition scheme is reali by the Random Forest Model.
The Hidden Markov Model is a probabilistic model of timing sequences. It learn the implicit characteristics of states by observing phenomena. The transforma process between sleep stage is a Markov process with implicit temporal informat The physiological signals and sleep states can be observed, while the numerical cha teristics of physiological significance of the state transitions are unclear. Therefore, choose the hidden Markov model as the initial model. In this study, for each subject, The Hidden Markov Model is used to characterize both the stability and the variation of CRA between different sleep stages, and the final recognition scheme is realized by the Random Forest Model.
The Hidden Markov Model is a probabilistic model of timing sequences. It can learn the implicit characteristics of states by observing phenomena. The transformation process between sleep stage is a Markov process with implicit temporal information. The physiological signals and sleep states can be observed, while the numerical characteristics of physiological significance of the state transitions are unclear. Therefore, we choose the hidden Markov model as the initial model. In this study, for each subject, the transformation between the sleep stages and phases is a Markov chain.
We take the CRA sequence as the observation sequence and the label of the sleep stages as the state sequence. There are 18 kinds of sleep stages in this study, including WEAK, S1 (A1, A2, A3 and NA), S2 (A1, A2, A3 and NA), S3 (A1, A2, A3 and NA), S4 (A1, A2, A3 and NA) and REM. I is the state sequence of 18 dimension, and O is the corresponding observation sequence of CRA.
A is the probability matrix of the observation transition: where a ij is the probability of the transformation to q j at time t + 1, given q i at time t.
In the CAP classification scheme, firstly, we train the HMM [44] for CRA and corresponding sleep stage tags. Then, the values along the diagonal of A and the sum of each column are calculated as the new eigenvalues of the states of the corresponding columns The Random Forest Model has natural advantages for classification tasks with many features and incomplete sample size, with high accuracy and fast training speed. It is suitable as our final classifier. The training and classification work are carried out in two layers, the first layer is carried out for identification stage WEAK, stages REM, S1, S2, S3 and stage S4, while the second layer is carried out for identifying A1-phase, A2-phase and A3-phase in S1/S2/S3/S4, respectively. The clarification of the two layers both used Random Forest Model.
SVM [60] is used in the scheme to diagnose insomnia and narcolepsy. The output of the Random Forest Model and the 23 characteristics above are used for training of the SVM. For subjects diagnosed with sleep-related disorders, their eigenvalues will be recalculated and retained in the training library.

Results of the Statistical Analysis of CRI in People with Non-Pathology, Insomnia and Narcolepsy
This section describes the statistical analysis of the cardiopulmonary characteristics of people with non-pathology and several sleep-related pathologies.
The deep sleep stage period (including stages S3 and S4) during the whole sleep, in which the human body is least affected by external noise and voluntary activities, shows more remarkable physiological significance in analyzing statistical results [61]. Previous studies have shown that CRI characteristics are stable and have statistical significance during deep sleep stage [43].
Therefore, CRI characteristics (including CRQ, CRR, CRB and CRA) in people with non-pathology, insomnia and narcolepsy during the DEEP SLEEP period were analyzed. The results are shown in the Figure 5  In the bar chart Figure 5, we can see that, compared to people with non-pathology, insomniacs have higher CRQ and lower CRR, while narcolepsy patients show the opposite manifestations. The CRB of the healthy group is significantly higher than the CRB of insomniacs and narcolepsy. At the same time, the CRA of insomniac patients is higher, while the CRA of narcolepsy patients is significantly lower than the CRA of the healthy group. The overall coupling of depth and width of narcolepsy patients are low. In the bar chart Figure 5, we can see that, compared to people with non-pathology, insomniacs have higher CRQ and lower CRR, while narcolepsy patients show the opposite manifestations. The CRB of the healthy group is significantly higher than the CRB of insomniacs and narcolepsy. At the same time, the CRA of insomniac patients is higher, while the CRA of narcolepsy patients is significantly lower than the CRA of the healthy group. The overall coupling of depth and width of narcolepsy patients are low.
For the analysis of CAP mode, the repeated one-way ANOVA, followed by Dunnett's post hoc test [62] is used to represent the significant difference of CRA in the A-phase and non-A phase (NA-phase) period during the sleep stages S3 and S4 in Table 1 below as an example. It is a method of comparing means in analysis of variance to judge whether the influence of CRA on the sleep stage is significant [62]. For repeated one-way ANOVA, if p < 0.05, there is significant difference between the groups. We calculated the p value of CRA in S3 and S4: p = 0.004. For Dunnett's post hoc test, if the value of difference of the mean > LSR, there is a significant difference between the groups being compared.
In the Table 1, it can be seen that CRA, as a measure of coupling depth, has significant differences between A-phase and NA-phase. For the different groups, measurements of people with non-pathology, insomnia and narcolepsy were analyzed and compared to each other below.
In the groups with non-pathology, insomnia and narcolepsy, by comparing the performance of CRA in period A1-phase, A2-phase and A3-phase in sleep stages S1, S2, S3 and S4 shown in Figure 6, the following results are obtained.  In the non-pathology group, in stage S4, there is a significant drop in CRA fro to A3; in stage S3, there is a huge drop between A1 and A2, whereas S1 and S2 are ently less coupled. CRA of A3 in stages S3 and S4 is lower than in stages S1 and S2 NA-phase. In insomniacs, CRA of A3-phase in stage S3 is very low, while it is ver in the NA-stage. We can see that CRA fluctuates greatly between phase A and Phase A. This may be a compensatory mechanism of the autonomic nervous sys insomniacs. In narcolepsy, the CRA in S3 and S4 is small regardless of whether th in an A-phase or NA-phase period.
To facilitate the analysis and comparison between three groups, Figure 7 is d From Figure 7, compared with the healthy group, the cardiopulmonary performa insomniacs fluctuates greatly with the generation of A-phase. The value of CRA i large in the non-A phase period and very small in the A-phase period. Howev In the non-pathology group, in stage S4, there is a significant drop in CRA from A2 to A3; in stage S3, there is a huge drop between A1 and A2, whereas S1 and S2 are inherently less coupled. CRA of A3 in stages S3 and S4 is lower than in stages S1 and S2 in the NA-phase. In insomniacs, CRA of A3-phase in stage S3 is very low, while it is very high in the NA-stage. We can see that CRA fluctuates greatly between phase A and non-Phase A. This may be a compensatory mechanism of the autonomic nervous system in insomniacs. In narcolepsy, the CRA in S3 and S4 is small regardless of whether they are in an A-phase or NA-phase period.
To facilitate the analysis and comparison between three groups, Figure 7 is drawn. From Figure 7, compared with the healthy group, the cardiopulmonary performance of insomniacs fluctuates greatly with the generation of A-phase. The value of CRA is very large in the non-A phase period and very small in the A-phase period. However, the value of CRA of narcolepsy patients is generally small. Combined with the overall analysis of CRB and CRR, it can be concluded that the cardiopulmonary coupling damping of narcolepsy patients is relatively high. to A3; in stage S3, there is a huge drop between A1 and A2, whereas S1 and S2 are inherently less coupled. CRA of A3 in stages S3 and S4 is lower than in stages S1 and S2 in the NA-phase. In insomniacs, CRA of A3-phase in stage S3 is very low, while it is very high in the NA-stage. We can see that CRA fluctuates greatly between phase A and non-Phase A. This may be a compensatory mechanism of the autonomic nervous system in insomniacs. In narcolepsy, the CRA in S3 and S4 is small regardless of whether they are in an A-phase or NA-phase period.
To facilitate the analysis and comparison between three groups, Figure 7 is drawn. From Figure 7, compared with the healthy group, the cardiopulmonary performance of insomniacs fluctuates greatly with the generation of A-phase. The value of CRA is very large in the non-A phase period and very small in the A-phase period. However, the value of CRA of narcolepsy patients is generally small. Combined with the overall analysis of CRB and CRR, it can be concluded that the cardiopulmonary coupling damping of narcolepsy patients is relatively high.  Overall, from the detailed analysis of the cardiopulmonary characteristics of different groups during the sleep period, we could see that CRI characteristics (including CRQ, CRR, CRB and CRA) in people with non-pathology, insomnia and narcolepsy are different during the whole sleep. The CRI could help diagnose insomnia and narcolepsy. In the CAP mode of different groups, both the variation and stability of CRA characteristics are significantly different. Constructing a classifier combining variation and stability can help us to recognize CAP more accurately.

Results of the Recognition and Disease Diagnostic Scheme
To illustrate the classification performance and generalization ability of the scheme, we conducted the 7-fold cross-validation [63]. We proved the validity of the sleep stage classification method by comparing our estimates with the expert diagnosis results.
The sleep stage classification results are shown in the confusion matrix Table 2. Each value in the table represents the number of samples. Each column represents the prediction category. Each row represents the actual category the data belong to. The results include 18 categories: Wake, REM, S1, S2 and S3, S4. In the S1, S2, S3 and S4, there are A1, A2, A3 and NA in every stage. According to Table 2, the accuracy and recall rates of the different classes could be calculated. The value of row i and column j represents the number of the sections whose truth value is class i and is predicted to be class j. Each column represents the prediction category; Each row represents the actual category of data belonging to. 'W' represents Wake, 'R' represents REM. 'Act' represents the number of actual sleep stage; 'Pre' represents the number of the predicted sleep stage.
In order to express classification accuracy and generalization performance form Table 2, the F1 score is defined as the average of accuracy and recall rates of all the eighteen classes [64].
In this formula, i stands for class i, including WEAK, REM and A1, A2, A3 and NA from stage 1 to stage 4. The precision refers to the specific gravity of the positive sample in the positive example determined by the classifier; recall refers to the proportion of the total positive cases that are predicted to be positive. Then the final F1 score in CAP experiments is [64]: According to the Table 2, we could calculate that F1 score of sleep-wake classification is 92.0%. F1 score in the sleep stage classification (WEAK, REM, S1, S2, S3 and S4) is 83.8%, while the F1 score in the CAP experiments is 80.4%.
In detail, about the identification of A-phase, the accuracy in stages S1, S2, S3 and S4 are 84.4%, 90.1%, 84.2% and 79.9% respectively. The average recognition rate of A-phase reaches 84.7%. Similarly, in terms of disease diagnosis, the F1 score of diagnosis and recognition is 87.8% for insomnia and 90.0% for narcolepsy. For real cases, this table could help to analysis differences between the results of sleep classification and the actual sleep structure easily.

Discussion
CRI characteristics (including CRQ, CRR, CRB and CRA) in people with non-pathology, insomnia and narcolepsy are different in all stages during the whole sleep.
Compared to people with non-pathology, insomniacs show higher CRQ and lower CRR, while narcolepsy patients have the opposite manifestations. The CRB of the healthy group is significantly higher than the CRB of insomniacs and narcolepsy. At the same time, the CRA of insomniac patients is higher than the healthy group, while the CRA of narcolepsy patients is significantly lower than that of the healthy group. This phenomenon can be considered as a compensation of autonomic nerve of insomniac patients. Meanwhile, the coupling depth CRA and coupling width CRB of narcolepsy patients are always low during the whole sleep.
In the CAP, in stage S4, there is a significant drop in CRA from A2 to A3; in stage S3, there is a sharp drop between A1 and A2, whereas S1 and S2 are inherently less coupled. CRA of A3 in stages S3 and S4 is lower than in stages S1 and S2 in the NA-phase. These performances support the conclusions including the presence of heart-brain connection [39] and the EEG performance of A2 and A3 type [50]. Compared to people with non-pathology, the cardiopulmonary performance of insomniacs fluctuates greatly with the generation of A-phase. The CRA is very large in the non-A phase period and very small in the A-phase period. However, the CRA of narcolepsy patients is generally low. Combined with the analysis of CRB and CRR, it can be concluded that the cardiopulmonary coupling damping of narcolepsy patients is relatively high.
In the CAP mode of different groups, both the variation and stability of CRA are significantly different. Our scheme using improved HMM and RF combines these two points well. The average recognition rate of A-phase reaches 84.7% and the F1 score in the CAP experiments reaches 80.4%.
In order to illustrate the role of CRI features in the scheme, as the control group, the features related to CRI were removed while the remaining features were used. After the training of the Random Forest Model and 7-fold cross-validation test, the F1 score of CAP classification is 73.8%. The F1 score of disease diagnosis is 71.6%. Besides, as comparison, features in references [34][35][36][37][38] are used in our scheme instead of CRI. Meanwhile, research on the feature extraction methods of ECG in recent ten years [65][66][67][68] are also considered. The results are shown in Table 3.

Disease Diagnosis
Heart rate spectrum analysis [34,65] 77.6% 72.6% 66.7% 70.5% detrended fluctuation analysis [35] 78 It can be seen that the performance of the CRI surpasses that of the features in other studies in sleep stage classification, especially in CAP pattern recognition. In the diagnosis of sleep-related disorders, CRI significantly outperforms other features.
The performances of 'removing CRI' and 'CRI' are better than the other benchmark papers, such as [34][35][36][37][38]. These papers start with the heart rate and body movement signals, try to find features that can characterize sleep stages. The HRV features in our classifying framework, include: three time-domain features: SDNN, RMSSD, pNN50; four frequencydomain features: VLF power, LF power, HF power, LF/HF ratio; five nonlinear features: SD1, SD2, SD1/SD2, sample entropy and approximate entropy. Body movement features include the variance, approximate entropy and sample entropy of three cycle acceleration signals. These are more comprehensive than those in these papers. 'CRI' performances better than [67,68], which uses wavelet filter bank to analysis the heart rate characteristics. The wavelet analysis could dig out heart rate characteristics in depth, which are closely related to the activities of cardiopulmonary system, to some extent. Compared with the features of the directly acquired signals, such as ECG, three-axis acceleration signals, etc., CRI deeply explores the degree of cardiopulmonary coupling in different stages of sleep, shows the physiological significance of the autonomic nerve regulation.
CRI characteristics quantify the coupling status in the cardiopulmonary system, and could express the activity characteristics of autonomic nerve regulation. These autonomic nerve regulation characteristics are determined by EEG, and ultimately manifest as tiny periodic changes in sleep stages. At the same time, using Hidden Markov and Random Forest model, the scheme proposed combines the values and stability of these characteristics during sleep. This makes the results of classification more accurate. For the groups with non-pathology, insomnia and narcolepsy, CRI show significant differences in various stages of sleep and express the pathological features of the disease. Thus CRI make the framework more effective in diagnosing diseases.
The results of the scheme, on the one hand, prove the existence of a heart-brain connection, and on the other hand, verify the validity of CRI in the expression of the cardiopulmonary coupling and autonomic nervous activity of human being. The scheme is also helpful to identify insomniac patients and narcolepsy patients.

Conclusions
Cyclic Alternating Pattern (CAP) is a sensitive tool for studying sleep disorders throughout the life cycle. Several efforts have been made to develop a reliable automatic CAP-scoring algorithm. Most of these methods rely on the extraction of spectral features from the EEG.
Considering the connection between the heart and brain of people, we analyze the cardiopulmonary characteristics of cardiopulmonary resonance indices (CRI) in people with non-pathology, insomnia and narcolepsy under the CAP mode. We analyze and identify key fields that contribute to insomnia and narcolepsy. The results show that CRI have different manifestations in different groups. The conclusion is drawn that CRI are capable of representing the cardiopulmonary coupling degree, autonomic nerve state and sensitivity of the subjects, and thus are able to measure the health status of human body.
Inspired by the results of the statistical analysis of the cardiopulmonary characteristics, our CAP Recognition and Disease Diagnostic scheme uses CRI in the frequency domain as the feature to recognize the A-phase stage during the whole sleep.
There are many trials in pattern recognition using Hidden Markov and Random Forest model. Different methods have shown superiority in different applications. The scheme in this article takes both variation and stability of measurements of coupling state of the cardiopulmonary system during sleep into account. The precision and recall show the good performance of the scheme.
The proposed method is effective to conquer the weakness about the signal acquisition and processing in clinical trials, particularly when we consider the stress of the examinee and the cost of measurements using the conventional Rechtschaffen & Kales rules [50]. Results show that the scheme could automatically recognize the Cyclic Alternating Pattern accurately, and hence help to diagnose insomnia and narcolepsy. Further, we would conduct more clinical trials to validate and improve our model.
To develop a classification and diagnosis scheme for a specific hospital, the clinical data and expert opinions should be added into the scheme. It could be interesting to see how these contribute to the prediction accuracy.
Furthermore, the meaning and application of CRI are also worth exploring, such as the psychological stress, inflammation, etc. Informed Consent Statement: Informed consent was obtained from all subjects involved in the study.

Data Availability Statement:
The data for this article is publicly available on the website https: //www.physionet.org/content/capslpdb (accessed on 1 January 2022).