Heart Rate Variability Measurement and Mental Model Building Based on Body Area Networks

: Heart rate variability (HRV) is a specific quantitative indicator of autonomic nerve regulation of the heart. The research of HRV can quantify the changes of human mental state. In this paper, an improved differential threshold method was proposed for R wave detection and recognition of ECG signals. The recognition rate was improved by improving the starting position of R wave and the time window function of the traditional differential threshold method. The experimental platform in this paper is a wearable sign monitoring system constructed based on body area networks (BAN) technology. Experimental results showed that the recognition rate of R wave of real-time 5min ECG data collected by this algorithm was more than 99%. Then, analytic hierarchy Process (AHP) was used to construct the mental stress assessment model, and the weight judgment matrix was constructed according to the influence degree of HRV analysis parameters on mental stress, and the consistency check was carried out to obtain the weight value of the corresponding HRV analysis parameters. Finally, comparative experiment proved that the model can describe the mental stress of the body quantitatively.

design [7,8], surgical risk assessment, and intelligent telemedicine [9]. At present, there are three methods to measure HRV: resting state HRV, task HRV and variable HRV. The measurement of HRV in resting state mainly refers to the way of collecting HRV in the quiet state. The measurement of HRV in the task is to investigate the differences of heart rate variability of subjects under different task states. The variable HRV is to investigate the change of HRV, such as the change of HRV before and after a task or operation, in order to infer the effect or effect of the task or operation. Luque-casado's research [10] showed that the cognitive workload under execution conditions changed with the change of tasks. Sustained attention is a key process affecting HRV, and there is a separation between subjective and objective cognitive workload.
There are two main methods to analyze HRV, namely time domain analysis and spectrum analysis. In time domain analysis, commonly used time domain parameters include standard deviation in N-N intervals (SDNN) and root mean square of the difference between adjacent NN intervals (RMSSD). SDNN is generally obtained by 24-hour dynamic electrocardiogram, which reflects the regulation ability of autonomic nervous system, and further reflects an individual's stress ability and resistance to pressure. RMSSD is used to assess the regulatory function and activity of the cardiac vagus nerve, or parasympathetic nerve. The spectral analysis of heart rate variability is to transform the time series of R-R interval to the frequency by mathematical transformation method, form the spectrum curve, and analyze the shape of the spectrum curve. Spectral analysis is usually performed at high frequency (HF; 0.15~ 0.40Hz), low frequency (LF; 0.04-0.15 Hz). HF describes parasympathetic nerve activity level, LH is sympathetic nerve activity characteristic indicator, and its ratio is negatively correlated with albumin level [11]. HRV index can be used to identify sympathetic and parasympathetic nerve activity, which can help cardiovascular patients to make accurate clinical warning and take a good treatment plan. Penttila [12] evaluated the applicability of the above four different parameters in cardiac vagus outflow and demonstrated that these parameters are more suitable for measuring cardiac vagal outflow during free breathing. La Rovere, M.T [13] quantized HRV (measured as standard deviation of normal to normal RR intervals [SDNN] by measuring a large number of data. The study may contribute to risk stratification after infarction. La Rovere, M.T [13] quantified HRV by measuring the standard deviation of the normal-to-normal RR interval with a large amount of data, which may contribute to post-infarction risk stratification. Rossi [14] applied ultra-short HRV to research the influence of data loss caused by motion artifacts, which plays a significant role in obtaining reliable HRV signals. After controlling for environmental and personal confounding factors, Tang [15] used a linear mixed effects model to analyze the above frequency domain and time domain parameters, and proved that temperature changes on the day of onset may significantly reduce cardiac autonomic nervous function. Yang [16] confirmed that lower HRV is associated with higher risk of all-cause and cardiovascular death in hemodialysis population, and that lower SDANN and LF/HF are predictors of both all-cause and cardiovascular mortality. HRV analysis is an important means to evaluate the state of autonomic nervous system regulating the cardiovascular system. Quantitative evaluation can directly reflect the mental state of patients, which is helpful for evaluation of psychiatric treatment programs and detection of specific drug effects. Sripanidkulchai [17] researched the effects of standard kaempferia parviflora (KP) extract on physical fitness and HRV parameters of adolescent sports school students in a randomized double-blind controlled trial. The experiment proved that KP extract has an anti-stress effect on HRV parameters, which can promote its application in sports training and exercise. Yoo [18] evaluated the relationship between stress measured by HRV and academic achievement of medical students during their internship.
In this paper, the recognition of R characteristic wave for ECG signal, HRV parameters calculation and mental stress evaluation are researched. The detection and recognition of the waveform is the prerequisite to judge the parameters of ECG signal. The detection and recognition of QRS waveform is the basis of ECG signal detection. If there is error detection and missing detection phenomenon of QRS waveform, it will certainly affect the judgment of P wave and T wave, and also affect the result of disease classification of ECG signal. To solve this problem, an improved difference threshold method was proposed to detect R-wave. The traditional difference threshold algorithm was improved in two aspects of starting point and sliding window width to improve the accuracy of R-wave recognition. Finally, the evaluation model of mental stress was constructed by AHP, and it was verified that the model can quantitatively describe the mental stress of the body through comparative experiments.

Differential threshold R wave detection algorithm
QRS wave group is the most unique and easily recognized characteristic wave in ECG signal. The characteristics of R wave in wave group are particularly obvious, and the change of rising slope and falling slope is most obvious. Differential threshold R wave detection algorithm is to use the slope characteristics of the rise and fall of R wave to recognize the R wave of ECG signal. In this paper, an improved differential threshold algorithm was proposed to improve the accuracy of R wave recognition. The whole detection process of proposed algorithm was shown in Fig.1. The selection of initial position is very important to accurately locate all R waves. As shown in Fig. 2, the initial position of R wave may occur in the following two situations. In the first case, the initial point is in the stationary part of ECG signal, which can quickly and accurately locate R wave. The second case is that the initial point is around the R wave, and when the maximum and minimum values are compared after the first-order difference, the maximum value will appear before the minimum value, which may increase the miss and fallout ratio of the R wave recognition. In order to prevent the second situation mentioned above, the initial detection time of R wave should be avoided near the R wave. Firstly, the ECG data during the period (1:t) was intercepted to find out the maximum ECG data A max . Then another period ECG data during (t+1:2*t) was intercepted to find out the maximum ECG data B max. Finally, the value of A max was required to compare with the value of 1.8* B max . If A max was greater, it indicated that the initial position was near the R wave. 2*t would be used as the initial position for R wave recognition. Otherwise, the initial position of R wave detection was set as 1. After the ECG signal detection point L was selected, a segment of ECG signal X wf was intercepted through the time window function W f , and the signal sequence was searched to locate the position point R 1 corresponding to the maximum value. At the same time, the first-order difference of X wf would be carried out, and the comparison between the differential signal and the original ECG signal was shown in Figure 3. After the differential signal dif(t) of signal segment X wf was calculated, the minimum and maximum values of the differential signal dif(t) were firstly located to correspond to the time points t min and t max of ECG signal. Whereafter, the position point R' corresponding to the maximum value of ECG signal in the time period was find out. Finally, a comparison was performed to judge whether R 1 and R' are the same point. If they were the same point, then that point was the R-wave position. Otherwise, the smaller one was. Once the position of the first R wave was determined, as shown in Fig.4, the initial point L was some distance away from that position, which mean that L = R + T 1 . The same process was then performed for the location of the second R wave. The calculated time interval between these two R waves was defined as the RR interval. Similarly, after the second R wave position was determined, the delayed T 1 position was set as the initial point. Then the ECG signal sequence was intercepted according to the dynamic time window function W f , and the position of R wave in this segment of ECG signal was located. In this process, if R 1 and R' were not at the same position, the absolute values of R 1 R and R' R were respectively subtracted from the previous RR interval. The position point with small difference was the position point of R wave.

Mental stress model construction based on HRV signal
HRV analysis is essentially a quantitative analysis of sinus heart rate. When premature beats or severe arrhythmias appear in the signals, HRV analysis will lose its significance. Therefore, the RR interval sequence will be processed to some extent in the actual analysis process as shown in Fig.5. Firstly, the absolute value of the difference between two adjacent RR intervals was set as , and the critical value was set as 0.12s. If there were 256 consecutive were all less than the critical value when scan the RR interval sequence, this data would be used as a HRV signal. Otherwise, it's necessary to estimate, in turn, to throw out any RR intervals in the sequence that don't satisfy the condition. Finally, the remaining RR intervals were pieced together to form HRV signals. Fig.5 The R wave recognition effect diagram was shown in Fig. 5(a), and the generated HRV signal diagram was shown in Fig. 5(b). When researchers evaluate body pressure, there is no clear judgment standard so far, and most of them determine the stress state of the subjects by observing too many indicators of HRV. In the process of HRV analysis, no matter the time domain parameters, frequency domain parameters or nonlinear parameters, any single parameter can't accurately represent the mental stress changes of the body. Therefore, the analytic hierarchy process (AHP) was proposed to conduct quantitative evaluation in this paper as shown in Fig.6. By obtaining the body pressure index, testers can intuitively understand their own pressure status. In this paper, some HRV analysis parameters were selected as pressure evaluation indexes by analyzing the experimental results. Among the time domain parameters, SDNN, PNN50 and HR were selected as pressure indicators. While TP, HF and LF/HF were frequency domain parameter, VAI, HRD and HLE were nonlinear parameter. The hierarchical structure of the pressure model constructed according to the pressure index was shown in Table 1. Where G represents the target layer of the model, represents the criterion layer of the model, and represents the scheme layer of the model. The body pressure value can be obtained by comprehensively weighting each level index of the hierarchical model. This process represents the mental stress in the form of mathematics, instead of the subjective judgment before.  The 1-9 scale method was used to construct the weight judgment matrix according to the influence degree of each parameter on mental stress. The weight judgment matrix of the constructed criterion layer was as follows.
The weight judgment matrices 1 ， 2 and 3 of frequency-domain parameters, time-domain parameters and nonlinear parameters of scheme layer G ij are respectively expressed as follows.
After the value of the secondary model was calculated, the body pressure value was calculated by combining the weight of the criterion layer . The pressure index calculation formula of the target layer model was shown in Formula (6).

EXPERIMENT AND DISCUSSION 3.1 Experimental test platform
The test platform in this paper is a wearable sign monitoring system constructed based on Body Area Networks (BAN) technology. The overall design structure block diagram of the system is shown in Figure 7 (a). It includes a three-layer structure of customer-premises Equipment (CPE), cloud computing services and doctor-premises Equipment (DPE). The ECG signal is collected by the client and sent to a remote data center to obtain a diagnostic report. On one hand, the remote data center receives the collected user data, calculates the HRV signal, and transmits the calculation results to the doctor. On the other hand, it receives the medical report transmitted by the doctor and feeds it back to the client. The doctor side will present the final HRV data calculation results, and the doctor can control the patient's status in real time for timely diagnosis and treatment advice. The hardware circuit diagram of the CPE in this experiment is shown in Fig.7(b). The device simultaneously collects ECG and pulse signals of human body. The ECG signal was collected by CM5 bipolar chest lead, with two lead wires as positive pole LA and negative pole RA. The electrode was attached to the human body, and the ECG signal was transmitted to the acquisition circuit board through the lead wire. The ADS1292R chip amplifies the analog ECG signal, converts A/D into digital i G Z i  signal, and sends it to the MCU processing circuit through SPI interface. The microcontroller assembles the collected ECG and pulse signals into frames and sends them to the USB-serial circuit, and then sends them to the computer. At this point, the synchronous collection of ECG and pulse is completed. The data collection interface of the upper computer is shown in Figure 7(c). The HRV time domain, frequency domain and nonlinear data can be presented by calculation. Figure 7 (d) is the doctor-side diagnosis and treatment interface, which can intuitively display the patient's current mental stress status and query the historical changes of mental stress, and provide the best diagnosis and treatment suggestions accordingly. The test data were measured in cooperation with Southwest Medical University. The subjects were aged between 20 and 40 years old, and the data were collected for 5min at a time. The Institution Research Ethics Board of the Chongqing University of Posts and Telecommunications approved this experiment, and all experiments were performed in accordance with relevant guidelines and regulations. In addition, all volunteers who provide data have agreed to use the data for publication and informed consent was obtained from all subjects and/or their legal guardian(s).

Verification of R wave detection algorithm
In order to verify the accuracy and anti-jamming ability of the R wave detection algorithm, the R wave location simulation of ECG signals under different conditions was carried out by using this algorithm on the test platform. The positioning effect diagram of ECG signal without noise interference was shown in Figure 8, and the one with noise interference was shown in Figure 9. Real-time collection of 5min ECG data using the algorithm presented in this paper was used to identify R wave. The simulation results showed that the algorithm recognition rate of R wave reached 100%. In order to further verify the accuracy of the algorithm, 30min ECG data from MIT/BIH ECG database was used to detect and identify R characteristic waves. Table 2 showed the statistics of the recognition rate of the R wave recognition algorithm for partial ECG data randomly selected from MIT/BIH database. As shown in table, the average R wave recognition rate of this algorithm for the electrical signals in the center of MIT/BIH database was about 99.69%, which was mainly caused by excessive noise interference in the signals.
In conclusion, the R wave recognition algorithm proposed in this paper has the following advantages over the traditional R wave recognition algorithm. On one hand, the algorithm improves the R wave recognition rate and avoids missing and wrong detection of short-range ECG signals. On the other hand, the algorithm is easy to implement and the computation is small, so it is suitable for the analysis equipment with high real-time requirement.

Contrastive experimental verification of mental states under different conditions
In this paper, a comparative experiment was carried out according to the difference of motion state and environmental factors.
It had found that muscle tension and mental activity would increase after exercise. In this experiment, HRV analysis parameters were used to calculate and compare mental stress values to verify the validity of the pressure model. Table 3 showed the comparison of experimental data of 5 randomly selected subjects in the database in two environments.
And figure 10 shows he calculated quantitative indexes of mental stress before and after exercise. It can be seen from the figure that the mental stress value obtained by comprehensive calculation after exercise is significantly increased compared with that before exercise, which is in line with the objective fact, indicating that this model can be used for stress assessment caused by exercise. VR videos tested included soothing landscapes and scary movie. Firstly, the subjects' mental stress was calculated and recorded while they were relaxed by watching the soothing landscapes. Each subject was then left alone in a dark room to watch horror movie clip. And the correspond mental stress in a state of anxiety and tension was calculated and recorded. Table 4 shows the comparison of experimental data of 5 randomly selected subjects in the database in two conditions. Furthermore, Fig.11 shows the comparison of mental stress values.
According to the results of the experiment, the subjects were relaxed when watching the scenery videos, with almost no psychological or physical stress. The value of corresponding mental stress was small, and the average value was about 30. After watching the scary video, the psychological activity of the subjects was greater, and even produced a sense of fear. As can be seen from Fig.11, the value of mental stress increases significantly.
Although there were individual differences, the increase was generally more than 60%, and the biggest change even reached 106.7%. This quantitative description was capable of reflecting the changes of mental stress effectively brought by different stimuli to the organism.

Error analysis of measurement data
Considering the influence of subjects, environment and measuring instruments, there will be some errors in the actual measurement. In order to reduce the error, we average the multiple measurements. Table 5 shows all error rate of HRV parameters. The results show that the errors of SDNN in time domain range from 0.16% to 17.27%, PNN50 range from 0.53% to 26.36%, HR range from 0.42% to 3.02%, and LF/HF range from 3.20% to 34.48%. TP (total power) error is between 1.92% and 36.35%. The errors of relative dispersion of nonlinear parameters (HRD), Lyapunov index (HLE) and vector Angle index (VAI) are 0.19%~14.96%, 0.42%~7.26%, and 0.57%~12.68% respectively. The error of the final calculated mental stress (MS) index is between 0.57% and 9.68%, which meets the requirements of application demonstration.

CONCLUSION
The quantitative analysis of HRV can reflect the regulation of the autonomic nervous system to the cardiovascular system. Not only can it help patients and doctors diagnose or predict cardiovascular disease, but it can also be used to assess psychological conditions such as stress. In this paper, an improved differential threshold method was used to detect and recognize R waves of ECG signals. In order to improve the recognition rate, the traditional difference threshold method was improved through a novel algorithm for locating initial position of R wave and improved time window function. The experimental results show that the recognition rate of R wave in 5min ECG data collected in real time is more than 99%. Compared with traditional characteristic wave recognition algorithms, the proposed algorithm has simplified process and high real-time performance, and is suitable for wearable analysis devices with low configuration requirements. The accurate recognition of ECG signal R characteristic wave goes far towards obtaining accurate HRV signal. The analytic hierarchy process was used to construct the mental stress evaluation model in this paper. According to the influence degree of HRV analysis parameters on mental stress, the weight judgment matrix was constructed and the consistency was verified. Then, the weight values of the corresponding HRV analysis parameters were calculated. In the end, according to the value range of the parameters selected by the model and the ratio relationship between the parameters and mental stress, the mental stress evaluation model was constructed. The validity of the mental stress model was verified by comparing the mental stress values of the subjects in different states and different environments. The experimental results show that the mental stress model can describe the mental stress of the body quantitatively.

5.Acknowledge
Dr. Luo reports grants from Science and Technology Research Program of Chongqing Municipal Education Commission (KJQN202000604), grants from Nature Science Foundation of Chongqing (cstc2021jcyj-bsh0221), grants from Central Nervous System Drug Key Laboratory of Sichuan Province (200020-01SZ), during the conduct of the study; Dr. Lu reports grants from Nature Science Foundation of Chongqing (cstc2019jcyj-msxmX0696), grants from project funded by China Postdoctoral Science Foundation (No.2021M693931), during the conduct of the study; Dr. Wang reports grants from Science and Technology