**4.1 ECG signal filtering**

The proposed morphological approach for baseline correction and noise suppression in the ECG signal was tested by the use of the MIT-BIH arrhythmia database.

**Figure 8** illustrate respectively the noisy ECG signal, the ECG signal after baseline correction and the resulting filtered ECG signal.

After acquiring the ECG signal, the next step is baseline correction. It consists of the application of morphological operations: "opening and closing".

The signal is first opened by a structuring element *Bo*, which means the application of two morphology operations "erosion + dilation", to remove the peaks and preserve the valleys. This "opening" operation generates a signal consisting of valleys which are suppressed, using the second operation which is the closing "dilation + erosion", so this operation uses another structuring element *Bf*. We achieved to the signal that represents the estimate of the derivative of the baseline (or variations of the baseline).

The structuring element *Bo*, *Bf* is used for baseline correction. In this case *Bo*, *Bf* is chosen as a geometric shape corresponding to an horizontal segment of *Mathematical Morphology and the Heart Signals DOI: http://dx.doi.org/10.5772/intechopen.104113*

**Figure 9.**

*Closing effect (to the left) and the opening operator (to the right).*

different lengths. Different lengths of the structuring element *Bo* and *Bf* are employed considering that the construction of the structuring element for the correction of the baseline depends on the duration of the characteristic wave and the sampling frequency (*Fs* Hertz) of the ECG signal. If the length of a characteristic wave is *TL* ð Þ *second* , the number of samples of this wave is *Fs* **∗** *TL*, which signify the structuring element *Bo* should have a length greater than *Fs* **∗** *TL*.

**Figure 9** shows the closing operation which uses a structuring element to remove the valleys left by the opening operation. The length of the structuring element *Bf* must be longer than the length of the *Bo*.

The ECG signal, the most characteristic waves are the P wave, the T wave and the QRS complex, which are generally less than 0.2 second.

Therefore, *Lo*, the length of *Bo* is **0***:***2 ∗** *Fs* and *Lf*, the length of *Bf* is typically chosen to be longer than the structuring element *Bo*, at approximately **1***:***5 ∗** *Lo*.

Since we employ *Fs* ¼ **360** *Hz* as a sampling frequency, we get *Lo* ¼ **0***:***2 ∗** *Fs* ¼ **72** and *Lc* ¼ **1***:***5 ∗** *Lo* ¼ **108**.

The final step to arrive at the baseline correction is the subtraction of the noisy ECG signal *f <sup>o</sup>* with *f <sup>b</sup>* (signal describing the baseline variations).

After baseline correction, the next step is noise removal. It consists of the application of morphology operators of the morphological transformation by top hat.

In fact, the input signal *f cb* is processed simultaneously by the operations "close" and "open", followed by a subtraction, to end up with the filtered ECG signal *f*.

The morphological transformation by Hat-Top permits to extract the positive and negative impulses simultaneously by the application of the operations of opening and closing.

It should be noted that the shape of the structuring element in the noise suppression is different compared to the baseline correction. Indeed, it can take two different shapes and of equal lengths: a triangular shape *B***<sup>1</sup>** for maintain peaks and valleys on a straight shape (segment of zero amplitude) *B***2**.

In our case, the size of the structuring element was fixed at 5 samples, with the value of the structuring element of *B***<sup>1</sup>** ¼ ð Þ **0**, **1**, **5**, **1**, **0** and *B***<sup>2</sup>** ¼ ð Þ **0**, **0**, **0**, **0**, **0** . This value is set in an empirical way where the initial values of minimums and maximums are set to the optimal values in the noise removal step.

The algorithm is applied respectively to records 209 and 234 of the MIT-BIH Arrhythmia database. **Figures 10** and **11** illustrate, that the filtered ECG signal does not present any variation of the base line, also the different waves of the signal are clearly highlighted, and without any deformation.

**Figure 10.** *a) Original ECG (record 234), b) filtered signal.*

**Figure 11.** *a) Original ECG (record 209). b) Filtered signal.*
