# Optimal Heart Sound Segmentation Algorithm Based on K-Mean Clustering and Wavelet Transform
## 2 Methods
### Adaptive Optimal-Peak Finding Based on Dynamic Spacing and Dynamic Threshold Segmentation  
Adaptive optimal-peak searching based on dynamic threshold segmentation included three parts. The first part was determining the initial step length, i.e., heart rate. It used the Fourier transform after similar empirical mode decomposition (which detected the peak point and then used third-order spline interpolation) of the envelope. The second part was to get the dynamic threshold and extract the upper and lower envelopes. The third part was to determine the optimal peak point iteratively. According to the initial step length, the optimal peak point is obtained using the dynamic distance and dynamic threshold. On the one hand, the extracted envelope was processed using a similar empirical mode decomposition method to obtain the processed signal \(\mathrm{X(n)}\). In the time domain, the interval between S1 and next S1 is an instantaneous period in a PCG signal. Using fast Fourier transform (FFT), the fundamental frequency of a PCG signal envelope was obtained and the initial step length \(\mathrm{T}_{\mathrm{ini}}\) was determined. On the other hand, for the extracted envelope, the upper envelope and lower envelope were extracted using a similar empirical mode decomposition. The upper and lower envelopes were averaged to obtain the dynamic threshold, as shown in Figure 4. The purple line represents the dynamic threshold. Our research set the minimum distance between two peaks as \(\mathrm{T}_{\mathrm{min}}\). This detected the points where the peak points were greater than the dynamic threshold. The points with relatively small envelope wave peaks (i.e., false positive points) were eliminated. \[\mathrm{J(P(i))}\ =\ \left\{ \begin{matrix}\mathrm{T}_{\mathrm{min}}\mathrm{=T}_{ \mathrm{ini}}\times 0.6\\ \mathrm{P}_{\mathrm{x}}(\mathrm{i+1})-\mathrm{P}_{\mathrm{x}}(\mathrm{i}) \mathrm{>T}_{\mathrm{min}}\\ \mathrm{P}_{\mathrm{y}}(\mathrm{i})>\mathrm{Thre}\end{matrix} \right. \tag{7}\]  
In the formula, \(\mathrm{P}_{\mathrm{x}}\) is the abscissa of the envelope peak point and \(\mathrm{P}_{\mathrm{y}}\) is the ordinate of the peak envelope point. \(\mathrm{P}_{\mathrm{y}}(\mathrm{i})\) is the local maximum. The method stated above is based on the selection of dynamic amplitude threshold. Next, time dynamic segmentation can be  
Figure 4: The dynamic threshold. Blue line is the improved Viola integral envelope. The red and yellow line represent above and following envelopes. The purple line represents the dynamic threshold. Figure 3: The original signal and the extracted envelope. (**a**) The original PCG signal waveform. (**b**) Viola integral waveform. (**c**) Improved Viola integral envelope. (**d**) Hilbert envelope. (**c**,**d**) Comparison of two envelope details. used to seek peaks. Based on the previous peak point, the time interval was obtained using the abscissa difference of the peak point T\({}_{\text{off}}\), which can get the dynamic time offset T\({}_{\text{dy}}\). Finally, the dynamic time offset was obtained to find the maximum and second maximum peaks in the quasi-periodic segmentation interval. The peak points are the initial S1 and S2. \[\text{J}(\text{P}(\text{i}))\ =\ \begin{cases}\text{T}_{\text{dy}}\text{= diff}(\text{P}_{\text{x}}(\text{i}))\\ \text{T}_{\text{off}}\text{= Para.}\times\text{T}_{\text{dy}}\\ \text{P}_{\text{y}}(\text{i})>\text{Thre}\end{cases}  \tag{8}\]  
The parameters in the formula were set to 0.3 and 0.5 to obtain different dynamic spacing in peak seeking. Then the resulting peak points were merged. This had the advantage of preventing omissions and improving accuracy. For a PCG signal, it is easy to miss the detection of the envelope peak point due to artificial jitter or low-frequency noise interference. Here we propose an algorithm for missing points. The average spacing between the last obtained peak points can be calculated using \(\overline{\text{T}}\). The upper threshold is defined as T\({}_{\text{u}}\ =\ 1.3\times\overline{\text{T}}\).