## 2 Materials and Methods
### State Annotation
#### 2.5.1 Limited Max Sequential Annotation (LMSA)
In terms of the performance of the whole algorithm, only a few dozen heartbeats with arrhythmia were present in the databases and they had little impact, but arrhythmia is an important indicator of disease, so the algorithm errors caused by arrhythmia could not be ignored. It has been observed that the diastolic duration usually changes mainly when arrhythmia occurs, and thus the model related to diastole should be considered. First, it is necessary to remove the limit on the diastolic duration because the open-source code for LR-HSMM limits the probability distributions for each state duration to a central range and the probabilities outside the limit range are infinitesimal. However, removing the restrictions was not sufficient to correct the errors. [PERSON] et al. [25] noted that using the Poisson distribution instead of the Gaussian distribution can improve the final annotation performance, so we only change the probability distribution for the diastolic duration to the Poisson distribution, as shown in Figure 9b, which can eliminate most of the arrhythmia errors, as shown in Figure 10 (TCN + Poisson 1.0). The Gaussian and Poisson distributions are defined as follows. 1. Gaussian distribution: \[f(x)=\frac{1}{\sqrt{2\pi\sigma}}exp\!\left(-\frac{\left(x-\mu\right)^{2}}{2 \sigma^{2}}\right),\] (14)  
Poisson distribution  
\[\mathrm{P}(X=k)=\frac{\lambda^{k}}{k!}e^{-\lambda},k=0,1,\ldots, \tag{15}\]  
Moreover, another method could eliminate the arrhythmia errors. We obtained the logarithm on both sides of Equation (10) using the following formula:  
\[\log\delta_{t}(j)=\max_{d}\left[\max_{i\
eq j}\left[\log(\delta_{t-d}(i))+\log \!\left(a_{ij}\right)\right]+\log\!\left(p_{j}(d)\right)+\sum\
olimits_{s=0} ^{d-1}\log\!\left(b_{j}(O_{t-s})\right)\right], \tag{16}\]  
With \(1\leq i,j\leq H\), \(1\leq t\leq N\), \(1\leq d\leq d_{max}\). As mentioned above, when an arrhythmia error occurred in the optimal path, the impact of the duration probability exceeded the impact of the classification probability, so the weight of \(\log\!\left(p_{j}(d)\right)\) could be adjusted to change the influence of the duration probability, and the equation became:  
\[\log\delta_{t}(j)=\max_{d}\left[\max_{i\
eq j}\left[\log(\delta_{t-d}(i))+\log \!\left(a_{ij}\right)\right]+\mathbf{\alpha}\log\!\left(p_{j}(d)\right)+\sum \
olimits_{s=0}^{d-1}\log\!\left(b_{j}(O_{t-s})\right)\right], \tag{17}\]  
With \(0\leq\mathbf{\alpha}\). When \(\mathbf{\alpha}\) was reduced to 0.2 from 1, most of the errors caused by arrhythmia could be corrected, as shown in Figure 10 (TCN + Gauss 0.2). In terms of the complexity and real-time feasibility of the Viterbi algorithm, the main calculation was focused on obtaining the HSMM model parameters and calculating the matrix \(\delta_{t}\)_(j)_ based on the feature classification results. The HSMM model parameters that needed to be obtained were mainly related to the heart rate, and the heart rate calculations were based on the autocorrelation with the original signal in the method proposed by [PERSON] et al. [13], which required a large amount of calculations when the signal was excessively long, but the method for determining the heart rate could be simplified by intercepting the signal length or changing the estimation algorithm. Calculating the matrix \(\delta_{t}\)_(j)_ required the same amount of calculations at each time point. Thus, after improving the method for estimating the heart rate, the average amount of calculations may not exceed 20,000 at each time point. Thus, compared with the hundreds of thousands of calculations involved in the classification step, the impacts of calculating the heart rate and \(\delta_{t}\)_(j)_ were small. In addition, the amount of track-back calculations will be very low and the real-time feasibility will not be affected if the length of the track-back is limited. Therefore, the Viterbi algorithm could also achieve real-time calculations by dynamically refreshing the track-back results.