## 2 Algorithm Overview
The convolution of two rectangular windows is a triangular window with a width of \(2\text{N}-1\). Based on the above analysis, we propose to use triangular window coefficient filtering to extract the envelope. Generally, the N should be approximately the same as the duration of a possible wider QRS complex. The length of the filter is found empirically. For a sampling rate of 360 samples/s, the filter length of 75 samples is found in this study, and the corresponding coefficients are [1, 2, 3, \(\ldots\), 36, 37, 36, \(\ldots\), 3, 2, 1]. In addition, we also used MATLAB to compare the results of filtering with the rectangular window twice and the triangular window. It can be seen from Figure 3 that the two lines overlap, and they are equivalent in terms of filtering effect. Finally, the Hilbert transform was swapped with the approximate Hilbert filter. The Hilbert transform is theoretically an infinite-length transform, which can be obtained by the Fourier transform, but the Fourier transform has high computational complexity and is costly to implement. In hardware implementation, the Hilbert filter is often used. The filter coefficients of the Hilbert transform are shown as follows:  
\[h(n)=\frac{1-(-1)^{m}}{n\pi} \begin{cases}0&n=2m\\ \frac{2}{n\pi}&n=2m+1\end{cases}  \tag{14}\]  
It can be seen that the filter coefficients are symmetric [\(\cdots-1/5\pi\), 0, \(-1/3\pi\), 0, \(-1/\pi\), 0, \(1/\pi\), 0, \(3/\pi\), 0, \(5/\pi\), \(\cdots\)], and they can also be infinite in length. In practical use, obviously, a large order will not be used, as the coefficient values are smaller when n is large. In order to determine the optimal filter order for R-peak detection, we simulated the ability of the Hilbert filter to convert peaks to positive zero-crossing when different values of n are taken using MATLAB, as shown in Figure 4.  
As we can see from the above Figure 4, if the order (\(n\)) is small, the convolution operation interval is short, the peak sensitivity is high, and the noise tolerance is weak. If the order is large, the convolution operation interval is long, the peak sensitivity is low, and the noise tolerance is strong. As n increases from 180 to 360, the marginal benefit becomes very small, and due to the presence of noise in wearable device environments, we selected an order of \(n=180\) for the Hilbert filter. Figure 3: Comparison of the effects of positive and negative filtering of rectangular windows and triangular window filtering (the rectangular window filtering curve overlaps with the triangular filtering window curve). From a theoretical perspective, to convert peaks to positive zero-crossing, the coefficients need to be odd-symmetric about the coordinate axis. Since peaks are approximately even-symmetric about the peak point, as long as the coefficients are odd-symmetric in the first and third quadrants, the convolution calculation result of the peak point will definitely be 0, and it will become a positive zero-crossing. Therefore, we do not need to strictly implement the standard Hilbert filter. In addition, increasing the number of zeros in the filter coefficients can significantly reduce the computational load on hardware. To make the hardware implementation easier and reduce the computational load of this step, we modify the filter coefficient as follows: one zero between every two non-zero coefficients is changed to four zeros. And to avoid the coefficients from decreasing too quickly, the non-zero coefficients were taken as 1, 1/2, 1/3, 1/4, and so on. The Hilbert transform length was set to 0.5 s--a sampling rate of 360 Hz--which corresponds to a coefficient of 180. As a result, the coefficients are [\(-\)1/18, 0, 0, 0, 0, \(-\)1/17, 0, 0, 0, 0, \(\ldots\), \(-\)1/2, 0, 0, 0, 0, \(-\)1, 0, 0, 0, 0, 1, 0, 0, 0, 1/2, \(\ldots\), 1/17, 0, 0, 0, 0, 1/18]. According to the Matlab simulation shown in Figure 5, it can be seen that the Hilbert filter with this coefficient almost coincides with the original Hilbert transform in terms of the position of the positive zero-crossing point.