**3. Application of morphological filtering to physiological signals**

#### **3.1 ECG signal filtering algorithm**

The opening and closing operators proposed in this work for ECG signal processing, is shown in **Figure 6** below.

It consists of different phases. A first phase which consists in detecting variations in the baseline from the original noisy ECG signal. A second phase which consists in correcting these variations (correction of the baseline), and a third phase which consists in suppressing the remaining noises to finally generate the filtered ECG signal.

These different phases are realized through the development of a sequence of opening and closing operators.

This sequence is based on exploiting the different characteristics of baseline drift and noise contamination in ECG signals, different structuring elements and different morphological operators. For baseline correction, an opening operator followed by a closing operator is defined; as well as noise cancelation. They are described in detail in the following subsections.

#### *3.1.1 Baseline correction algorithm*

To correct for baseline variations, the variations of that line ( *f <sup>b</sup>* signal) detected from the original signal ( *f <sup>o</sup>*) are subtracted from that signal to generate the corresponding signal *f cb* (i.e., an isoelectric line).

The signal *f <sup>b</sup>* is obtained through a sequence of opening and closing operators with structuring elements *Bo* and *Bf* suitably chosen as the one described below.

$$f\_b = f\_o \odot B\_o \bullet B\_f \tag{10}$$

$$= \mathbf{f}\_b \Theta \mathbf{B}\_o \oplus \mathbf{B}\_o \oplus \mathbf{B}\_f \Theta \ \mathbf{B}\_f$$

**Figure 6.** *Steps of the morphological filter algorithm.*

*Mathematical Morphology and the Heart Signals DOI: http://dx.doi.org/10.5772/intechopen.104113*

$$f\_{cb} = f - f\_b \tag{11}$$

#### *3.1.2 Noise suppression algorithm*

The approach adopted for noise suppression appeal of Top-hat transformations, which are translated by the equations below.

$$f = f\_{cb} \bullet B - f\_{cb} \bullet B$$

$$= (\mathbf{f}\_{cb} \bullet \mathbf{B} - \mathbf{f}\_{cb}) + (\mathbf{f}\_{cb} - \mathbf{f}\_{cb} \odot \mathbf{B})$$

$$= (\mathbf{f}\_{cb} \oplus \mathbf{B}\_1 \Theta \bullet \mathbf{B}\_2 - \mathbf{f}\_{cb}) + (\mathbf{f}\_{cb} - \mathbf{f}\_{cb} \Theta \bullet \mathbf{B}\_1 \oplus \mathbf{B}\_2)$$

*<sup>f</sup> cb* • *<sup>B</sup>* � *<sup>f</sup> cb* and *<sup>f</sup> cb* � *<sup>f</sup> cb* ○ *<sup>B</sup>* are of the type of morphological transformation by Top Hat. This transformation is a high pass filter.

*<sup>f</sup> cb* • *<sup>B</sup>* � *<sup>f</sup> cb* and is called the black top hat transformation, which is used to extract negative pulses (or negative peaks); is called the white top hat transformation, which is used to extract positive pulses (or positive peaks). Thus the filter can be used to extract positive and negative pulses simultaneously [46].

**Figure 7** represents the block diagram describing the structure of the top-hat transformation for noise suppression.

It consists of three steps: The first concerns the acquisition of ECG signals ( *fcb*: ECG signal after baseline correction).

This step is followed by another step that allows noise removal. This step uses the morphology operators defined in Eq. (12).

*B1* and *B2* are structuring elements for opening and closing. These operators are employed simultaneously on the signal. The next step is the subtraction between the two closing and opening operators to generate a filtered ECG signal.

The proposed algorithm is implemented under MATLABR14 environment. It is tested on a set of ECG signals (noisy) from the MIT-BIH database [47].

#### **3.2 PCG signal filtering algorithm**

**Figure 7** below illustrates the stages of noise suppression and heart murmurs.

**Figure 7.** *Diagram of PCG filtering.*

The first step is to normalize the PCG input signal. A second step is to remove noises, and a third step is to remove heart murmurs.

## *3.2.1 PCG signal normalization*

The original signal has been normalized before performing any operation. The PCG signal is re-sampled at 8000 Hz with 16 bits of precision and converted to WAVE format and normalized by:

$$\varkappa\_{\text{Nor}}(t) = \frac{\varkappa(t)}{m \mathfrak{a} \mathfrak{x}\left(|\varkappa(t)|\right)}\tag{13}$$

The aim of normalization is to reduce the amplitudes of the PCG signal.

#### *3.2.2 Noise suppression algorithm*

Noise suppression uses the morphological filter, which is presented by the following equations:

$$f\_{\,\,p} = f \bullet B \bigcirc B \tag{14}$$

$$f\_c = f - f\_p \tag{15}$$

*f <sup>p</sup>* represent the detection of the positive and negative peaks by the closing of the element.

*B*<sup>1</sup> followed by the opening of *B*<sup>2</sup> to finally generate the filtered signal *fc*. So the signal *fc* is the subtraction of the original signal and the signal *f <sup>p</sup>*.

#### *3.2.3 Heart murmur algorithm*

The filter applied for the suppression of murmurs is the average filter; it is described in the equation below.

$$f\_{\,\,p} = f \bullet B \odot B \tag{16}$$

$$f\_{\mathfrak{e}} = f - f\_{\mathfrak{p}} \tag{17}$$

The detection of murmurs is based on two morphological operators, namely closing and opening, to suppress peaks. The average between the closing and opening operator gives the result *f <sup>p</sup>*. After the subtraction between the *f* signal and *f <sup>p</sup>* to generate a filtered signal *f <sup>c</sup>*.

#### **3.3 ECG signal wave detection algorithm**

The detection of the RR interval can be accomplished only after a good detection of the R peak. This detection goes through three stages. A step which allows locating the positive peaks R by the opening operation and another step which allows locating the

negative peaks R by the application of the closing operation. The final step to arrive at these peaks and valleys is the subtraction of the filtered ECG signal f with the two previous operations.

The following formula illustrates the extraction of R positive peaks:

$$Peak\text{s }(RR) = f - (foB)\tag{18}$$

And the following formula illustrates the extraction of valleys or negative peaks R:

$$\text{Valleys}(RR) = f - (f \bullet B) \tag{19}$$

Formula (18) and (19) are equivalent to:

$$Peaks(RR) + Valleys(RR) = f - (foB \bullet B) \tag{20}$$

Structuring element B is used to detect peaks and valleys. In this case it is considered as a geometric shape "horizontal segment of different lengths". The following formula shows the:

$$b(t) = \begin{cases} \mathbf{1} \, \text{si } |t| < k \\ \mathbf{0} \, \text{si } non \end{cases} \tag{21}$$

where *K* is a constant.

One of the problems of the morphological approach is to determine the optimal value of *K*. If it is too small, the transformation will be sensitive to high frequency noise, and the peaks are incorrectly determined. However, *K* which must be less than the peak width R. Research has shown excellent results by using values between [55–60] ms [48].

After detecting the RR rhythm, the next step is to detect the T wave. It uses the erosion operator to build maximum thresholding. Knowing that the amplitude of the T wave is enormously greater than the Q wave and the S wave in the normal or abnormal case of the ECG signal.

The following formula illustrates the detection of the T wave:

$$\begin{cases} (f \oplus \mathbb{B})(m) = \max\_{n=0,\ldots,M-1} f(n) + B(m-n) \\ \text{for } N > M \text{ and } m = \mathbb{M} - 1, \ldots, \ldots, \mathbb{N} - 1 \end{cases} \tag{22}$$

Where the sets f and B are underlined by *f n*ð Þ¼ f g 0, 1, … *:*, *N* � 1 and *B* ¼ f g 0, 1, … *:*, *M* � 1 .

The next phase is the detection of the Q and S waves of the ECG signal. It uses a morphological operator called closing to detect the negative peaks. On the other hand, the morphology of Q and S waves in an ECG signal are of negative amplitudes. For this, we have chosen the most adapted closing operator to detect the negative peaks. Finally, the detection of the P wave is based on the opening operator which is combined with a structuring element of multi-scale, according to the equation:

$$f\_{\;\;p} = f \odot B\_1 \odot B\_2 \tag{23}$$

#### **Figure 8.** *a) Original ECG. b) Baseline correction. c) Filtered signal.*

The signal is operated by two successive opening operators with a structuring element of an equal scale 2. This means that the number of opening operations and the number of the structuring element is two scales.
