# Optimal Heart Sound Segmentation Algorithm Based on K-Mean Clustering and Wavelet Transform
## 2 Methods
### Envelope Extraction Based on Viola Integral and Shannon Energy  
The envelope energy function of PCG signals was calculated using the Viola integral method and [PERSON]'s energy-based algorithm. This method mainly consists of two parts. The first part uses the Viola integral to extract the envelope of the pre-processed audio signal. The second part uses first-order Shannon energy to highlight the low amplitude peak. Compared with the envelope obtained using a Hilbert envelope, we obtained a smoother heart sound envelope using this method. The Viola integral method was first applied in face detection and recognition. [PERSON] et al. proposed a fast detection algorithm based on an adaptive method and successfully applied it to the fast detection of images [19]. The algorithm is simple, fast, and real-time. Based on this, [PERSON] et al. applied this method to heart sound analysis and achieved good results [20]. We used the Viola integral to extract the envelopes of heart sounds. Then we needed to determine a time scale \(\mathrm{L_{T}}\). \(\mathrm{L_{T}}\) is related to the duration of S1 and S2. Numerous studies claim that the duration of S1 is usually greater than 0.1 s, so \(\mathrm{L_{T}}\) can be estimated as  
\[\mathrm{L_{T}}\ =\ 0.5\times 0.1\times\mathrm{fs}\]  
Figure 2: The proposed method framework for heart sound segmentation. where fs is the sampling frequency of the PCG signal. The mean sequence of the signal \(\overline{\mathrm{X}}_{\mathrm{T}}(\mathrm{m})\) is expressed as  
\[\overline{\mathrm{X}}_{\mathrm{T}}(\mathrm{m})\ =\ \frac{1}{2\mathrm{L}_{\mathrm{T}}+1} \sum_{\mathrm{k}\ =\ \mathrm{m}-\mathrm{L}_{\mathrm{T}}}^{\mathrm{k}\ =\ \mathrm{m}-\mathrm{L}_{\mathrm{T}}}\mathrm{X}_{\mathrm{T}}( \mathrm{k}) \tag{1}\]  
The Viola integral envelope is calculated using  
\[\mathrm{E}_{\mathrm{T}}(\mathrm{m})\ =\ \frac{1}{2\mathrm{L}_{\mathrm{T}}+1} \sum_{\mathrm{k}\ =\ \mathrm{m}-\mathrm{L}_{\mathrm{T}}}^{\mathrm{k}\ =\ \mathrm{m}-\mathrm{L}_{\mathrm{T}}}\bigl{[}\mathrm{X}_{ \mathrm{T}}(\mathrm{k})-\overline{\mathrm{X}}_{\mathrm{T}}(\mathrm{m})\bigr{]} ^{2} \tag{2}\]  
where \(\mathrm{m}\ =\ \mathrm{L}_{\mathrm{T}}\), \(\mathrm{L}_{\mathrm{T}}+1\),..., \(\mathrm{M}-1-\mathrm{L}_{\mathrm{T}}\). The normalized sequence is the new envelope \(\mathrm{E}(\mathrm{m})\), which is defined by  
\[\mathrm{E}(\mathrm{m})\ =\ \frac{\mathrm{E}_{\mathrm{T}}(\mathrm{m})}{\mathrm{ max}(|\mathrm{E}_{\mathrm{T}}(\mathrm{m})|)} \tag{3}\]  
Shannon energy can suppress the interference of high-intensity signals, which can reflect the peaks of low-intensity amplitudes [21]. The [PERSON] energy formula of order \(\mathrm{n}\) can be written as Equation (4). Here we chose the first order. According to [PERSON]'s energy formula, when \(\mathrm{X}(\mathrm{t})\ =\ 1\), \(\mathrm{log}\mathrm{X}(\mathrm{t})\ =\ 0\). This results in a double-peak problem. The sequence \(\mathrm{X}(\mathrm{t})\) is obtained by multiplying the new envelope with the empirical parameter 0.7, which is to eliminate the double-peak problem after using [PERSON]'s energy formula. \[\mathrm{X}(\mathrm{t}) =\mathrm{c}\times\mathrm{E}(\mathrm{m}) \tag{4}\] \[\mathrm{E}(\mathrm{t}) =-\mathrm{X}(\mathrm{t})^{\mathrm{t}}\mathrm{log}\mathrm{X}( \mathrm{t})^{\mathrm{n}}\]  
To eliminate small spikes, Shannon energy is converted into the normalized average Shannon energy, which is called Shannon energy envelope (SEE) [22]. The width of the smoothing window \(\mathrm{N}\) is the sampling point of 20 ms.  
\[\mathrm{E}_{\mathrm{s}(\mathrm{t})}\ =\ \frac{1}{\mathrm{N}}\sum_{\mathrm{i}\ =\ 1}^{ \mathrm{N}}\mathrm{E}(\mathrm{t})\mathrm{log}\mathrm{E}(\mathrm{t}) \tag{5}\]  
After normalization, it is shown as  
\[\mathrm{P}(\mathrm{t})\ =\ \frac{\mathrm{E}_{\mathrm{s}(\mathrm{t})}-\mathrm{ Mean}\Bigl{(}\mathrm{E}_{\mathrm{s}(\mathrm{t})}\Bigr{)}}{\mathrm{Std}\Bigl{(} \mathrm{E}_{\mathrm{s}(\mathrm{t})}\Bigr{)}} \tag{6}\]  
The following Figure 3 compares the original signal and the extracted envelope.