# A statistical designing approach to MATLAB based functions for the ECG signal preprocessing

- 379 Downloads

## Abstract

Though a number of research articles are available based on different preprocessing methodologies, all of them are not available in a single article with the practical utility of implementation. This article scopes to implement the most effective ECG signal preprocessing methods (baseline wander removal, noise cancellation, and peaks detection) with a simple statistical explanation. In addition, this research work contributes to designing several MATLAB based functions to implement the aforementioned preprocessing steps in practice. These functions are available free of cost for all in the MATLAB archive. The foremost aim of this article was to present clear conception about different ECG signal preprocessing steps like baseline wandering removal, noise elimination, QRS complex and point detections, P-peak and T-peak detection, and beat rate calculation. We hope that this work will eradicate all constraints of the ECG signal preprocessing and will motivate the new researchers of this field to reach more acute findings from the ECG signals.

## Keywords

ECG signal processing Baseline wandering removal Noise removal Peak detection of the ECG signal MATLAB based function## 1 Introduction

Since the methodology of ECG was invented more than a hundred years ago, lots of important discoveries were made and tons of signal processing-related problems were solved with numerous methods and algorithms. In addition, the recent publications [3, 4, 5] based on ECG indicate that the research scope of ECG processing is still not finished. In this situation, a primer researcher always faces some difficulties in preprocessing of an ECG signal like baseline wandering, noise filtering, QRS complex detection, different peak detection (for example P, Q, R, S, T), etc. There are a number of articles based on these problems and their solutions [6, 7, 8, 9, 10, 11, 12, 13, 14]. Most of these articles discussed the basic mathematical issues regarding these problems with block diagram or algorithm approach. In most of the cases, many researchers even fail to implement these methods practically and consequently, the researcher may be discouraged to do further research. In addition, in many countries, the biomedical signal processing is a very new field of research.

The students and the researchers of these countries start their journey with ECG signal processing and definitely face the problems of implementing the aforementioned preprocessing techniques. As a result, they have to buy the professional costly software to solve these problems or try to implement by them which may require a long time. Consideration of the previous complications demands an article that describes all of these preprocessing steps with logical clarifications as well as their implementing techniques.

The main contribution of this article was to present a systematic review on the important preprocessing steps and its implementation on the ECG signal processing in MATLAB environment. In this context, this work contributes to the designing of several MATLAB based functions (these functions are freely available in [15]) considering the most suitable algorithms for different ECG signal preprocessing methods that will be helpful for the researchers of this field. These functions have been designed in such a generalized way that even the novice researchers of the signal processing arena can apply them to process the ECG signal for simple applications to high-level research on ECG signal. We conducted this long-term research work to arrange all the processing algorithms and its application in a platform for the benefit of Biomedical Engineering students and the researchers from all over the world. The most amazing thing about the functions is that it is compatible with any version of MATLAB from 2006 to 2018.

## 2 Basic features of the ECG signal

Amongst all the bio-signals, ECG is very special than the others due to its quasi-periodic characteristics. It is already mentioned that the ECG signal consists of several peaks of different shape with different periodicity. As a result, the general characteristics of an ECG signal are necessary to understand properly before going to process it. This subsection of the paper presents the most important features of an ECG signal in normal condition.

The QRS complex is a part of ECG signal. It occurs due to the ventricular depolarization of the heart [2, 16, 17]. So by detecting QRS complex, we will be able to determine if the ventriculation of the heart is working properly or not. If one has problem in heart functioning, the graph at the site of QRS complex will be different from the normal graph, and if the difference is very much noticeable then the detection of QRS complex becomes complicated. So it will be easier if there is a QRS complex detection function and using the function, anyone will be able to detect it easily.

The prominent peaks of the ECG signal and their corresponding characteristics and functions

The prominent points of ECG signal | Amplitude in millivolt | Point to point interval | Duration (s) | Functions of the heart during the intervals |
---|---|---|---|---|

P | 0.15 to 0.25 | P–R interval | 0.12–0.20 | Atrial depolarization and conduction through AV node |

Q | − 0.2 to − 0.3 | Q–T interval | 0.32–0.44 | Ventricular depolarization and ventricular repolarization |

R | 0.9 to 1.50 | S–T intervals | 0.05–0.15 | Ventricular repolarization |

S | − 0.3 to − 0.5 | R–R interval | 0.6–1.0 | End of the ventricular depolarization |

T | 0.1 to 0.5 | QRS duration | 0.08–0.10 | Ventricular depolarization and atrial repolarization |

## 3 Methodology

In the introduction, we have discussed some basic preprocessing steps for ECG signal processing. In this section, our selected preprocessing schemes will be discussed elaborately with their process of using, designing concern, and benefit of applications. The methodologies of the preprocessing schemes have been presented comprehensively with the block diagram that clarified the designing techniques. In addition, the corresponding MATLAB based functions have been introduced with its application procedures.

### 3.1 Baseline wandering removal

**abc_BWPF()**’ function.

According to the BWPF algorithm, it works based on the polynomial fitting technique with multiple segmentation of the raw ECG signal. The segmentation number is chosen by the users. The main benefit of this multiple segmentation is the reduced computational complexity. The polynomial fitting order is self-assessed by this BWPF algorithm. This algorithm sets its polynomial order based on the threshold value of its fitting error. After fitting, all the segments by different orders, the estimated wandering paths are subtracted from the corresponding segmented signals and added one after one to represent the baseline wander removed signal with the same length of the raw ECG signal. The workflow diagram of the BWPF algorithm is given in Fig. 3.

### 3.2 Filtering

Albeit BWPF algorithm removes the baseline noises from the ECG signal, it contains some power line noise (50 or 60 Hz) [23, 24, 25, 26] which is considered as high-frequency noise of the ECG signal. This outlier type noise can be removed by FIR, IIR, or adaptive type filters. In addition, weighted moving average by Gaussian window [27] is another solution for this type of noise removal. On the other hand, according to our long experiences of ECG signal processing Savitzky–Golay (SG) filter [28, 29] can be a nice solution where the order and frame length choosing is a crucial task [30]. In this work, we have suggested two types of filtering technique whether the user can use any one of them: One technique is based on SG filtering method and another is a weighted moving average with a Gaussian window. The designed filtering functions have been named as **abc_filter1()** and **abc_filter2()**. The details regarding operations of these designed functions are discussed in the next section.

### 3.3 QRS complex detection

After removing the baseline wander and power line noises the main interest goes to the peak detection of the ECG signal. The peak detection actually starts with R peak detection because it is easy to detect the R peak than the other. Additionally, there are significant points with R such as Q and S. Therefore, QRS complex detection is now the main objective. To detect Q and S, at first R peak detection is essential.

Any peak detection obeys a simple rule and that is to find the point which is larger than its nearest two neighbors. It means if *x*[*n*] is peak, definitely *x*[*n*] is greater than *x*[*n *+ 1] and *x*[*n* − 1]. But when we are concerned about the highest peaks, R in the ECG signal, the peak R should be greater than a threshold value regarding its amplitude. Therefore, to be an R peak point *x*[*n*] should be greater than *x*[*n* − 1], *x*[*n *+ 1], and the threshold value. We can set an approximate threshold value like 0.3, 0.5, or 0.8 mV. But this is not a proper way because due to this section consideration many R peaks may be missed or other peaks can be selected as R.

To solve this problem, Professor Balda proposed FS2 algorithm which stands for *First and Second Derivative* based algorithm. The FS2 algorithm is designed based on the first and second derivative of ECG signal which was developed by Balda et al. [31] in 1977, and further modified by Ahlstrom and Tompkins [32] in 1983, for high-speed R-peak detection of recorded ECG signals. This algorithm helps to calculate the appropriate threshold for any ECG signal. Here, we have discussed the FS2 algorithm, shortly.

*n*] or x[

*i*] represents the ECG signal. The first derivative is then smoothed by a 4-point moving average low-pass filter as given in (3) and then added to the second derivative as (4). This reduces the P and T wave deflections. Since the P and T wave deflections are almost removed, the only peak that is found is R:

Finally, the threshold is determined to find the R peak location from the signal. Generally, the threshold is determined considering the 30% of the maximum point of *Y*_{3}. Therefore, to be R peak, any point of ECG signal, *x*[*n*] should be greater than *x*[*n* − 1], *x*[*n *+ 1], and threshold = 0.3 × max(Y_{3}[*n*]).

The next step is to determine the Q and S peaks. We know that Q peak is before the R peak and S peak is after the R peak. So if we can find the R peak first, we can determine the Q and S peaks, consequently.

### 3.4 Q and S peaks

The Q peak is the minimum point within the range of Q–R interval. This interval varies between 0.03 and 0.07 s. Therefore, a simple condition can be applied to find the minimum point within the sample number of R_location − (sampling rate × 0.07) and R_location. For example, suppose the sampling rate is 300 Hz and the initial R_location is 214. Therefore, we can find the corresponding Q point within the sample number 214 and 193 (= 214 − [300 × 0.07]). On the other hand, S point appears within 0.07 s after the R peak. After multiplication with the sampling frequency, we can set a range to find the S peak considering the minimum point within this range. In this proposed design, the function was named as **abc_QRS()** that requires an input ECG signal and its sampling rate. This function returns three-column data of Q, R, and S points. The user can easily mark the points registering on the original ECG signal. In addition, according to the necessity, the user can take any of the peaks from the output results of the designed function. The details of operations of the function are discussed in the next section.

### 3.5 P and T peaks

We know that Q peak is situated after P peak. So if we use the position of Q peak as a reference (since it is already found by the help of the previous algorithm), then the position of P peak can be found within the range of P–Q interval. Generally, the P–Q point takes less than 0.2 s. Therefore, the finding range will be the 0.2 × sampling rate. The maximum point within this range is P peak point. As a result, if we consider the sampling rate as 300 Hz the P peak will be found within Q_location − 60 to Q_location sample points. Here, Q is actually not a peak but valley, although it can be considered as a negative peak. Therefore, this peak detection is slightly different than that of R. The valley point is the point which is smaller than its nearest two neighboring samples. It is just the opposite rule of R finding technique. The same rule is also applicable to find the S peak as well within the appropriate reference points. The peak detection function of Q points has been designed as the following flow chart given in Fig. 4.

On the other hand, we know that T peak is situated after S peak. So if we use the position of S peak as the references, the T peak will be found within 0.3 s after the appearance of the S peak. The maximum point within this time is known as the T peak. For 300 Hz sampling rated ECG signal, the T peak is found S_Location to S_Location + 90 samples. The T peak detection procedure follows the same mechanism explained by the flow chart of Fig. 4 where the initial reference points are taken S instead of Q for P detection.

**abc_PT()**’ which requires the ECG signal and its sampling rate. It returns the temporal position of the P and T peaks position regarding the input ECG signal. The function ‘

**abc_PT()**’ returns two-column result where the first column will provide the P peaks and the second column will provide the T peaks (Figs. 5, 6).

## 4 Results using the designed functions

### 4.1 Baseline wandering removal

**abc_BWPF**’ can be used. In this particular function, users need to load ECG data,

*x*and the number of segments

*n*. The number of segments is user defined so that the user can choose it to take the output as anticipated. This function returns only the wander-free ECG signal. Therefore, too many arguments will not be acceptable instead of only one. With the help of a simple MATLAB code given below, a user can compare the wandered ECG signal and wander-free ECG signal.

### 4.2 Filtering

**abc_filter1()**’ and ‘

**abc_filter2()**’. The following code will provide you the filtered ECG signal with compared to the raw ECG signal. The user can use the abc_filter2(), too. The raw ECG signal and the effect of the filtering abc_filter1() and abc_filter2() are given in Figs. 9 and 10, respectively.

### 4.3 QRS peak findings

Generally, a user finds the Q, R, and S peaks at a time instead of finding separately. Due to this within only one function named by abc_QRS() three peaks can be found providing appropriate information. Due to the operation of the function, users have to provide the input ECG signal, its sampling rate, and peak threshold with the chronological order as given in (6). Although the peak threshold should be selected as 30% of the peak, i.e., 0.3, in some cases this threshold value is to be selected slightly lower or higher based on the condition of the ECG signal. As a result, we have provided an option to the users to change it as input. This function returns only one output having three columns of data. This data matrix contains the Q, R, and S points, sequentially in its first, second, and third column, respectively.

### 4.4 P and T peak findings

Although P and T peaks are situated at the beginning and ending part of an ECG period, respectively, only one function has been designed to get them at a time instead of making two different functions. The designed function was named abc_PT(). Using this function, a user will get the peak result matrix having two rows containing peak points of P and T, respectively.

## 5 Conclusions

This work aims to present the basic preprocessing steps of the ECG signal with the designed MATLAB functions. At first, the common features and characteristics of the ECG signal have been discussed with the necessary figures. Second, the most significant preprocessing steps of the ECG signals have been discussed with a mathematical and algorithmic aspect. This paper scoped to design the MATLAB functions based on the statistical approaches that are available in MATLAB archive (the link is given in [15]) from where anybody can download all the functions freely as well as anybody can get it on the request to the corresponding authors of this paper by email. Finally, the application criteria of the designed functions have also been discussed with MATLAB code and their corresponding results shown with the discussion. So far to our knowledge, the functions work very well with the proper prerequisite information like sampling frequency, threshold, etc. in case of peak detection.

Since this article is solely for the new researchers in the field of ECG signal processing, we believe this article will be very helpful for them to eradicate their constraints to move ahead.

## Notes

### Compliance with ethical standards

### Conflict of interest

The authors of the article declare no conflict of interest with any financial party or any researchers regarding this research work.

## References

- 1.Becker, D.E.: Fundamentals of electrocardiography interpretation. Anesth. Prog.
**53**(2), 53–64 (2006)CrossRefGoogle Scholar - 2.Rivera-Ruiz, M., Cajavilca, C., Varon, J.: Einthoven’s string galvanometer: the first electrocardiograph. Tex. Heart Inst. J.
**35**(2), 174–178 (1927)Google Scholar - 3.Beyramienanlou, H., Lotfivand, N.: Shannon’s energy based algorithm in ECG signal processing. Comput. Math. Methods Med.
**2017**, 1–16 (2017)CrossRefGoogle Scholar - 4.Mejhoudi, S., Latif, R., Toumanari, A., Jenkal, W., Elouardi, A.: Implementation and evaluation of ECG signal processing algorithms on embedded architectures. In: 2017 International Conference on Electrical and Information Technologies (ICEIT), Rabat, Morocco, 2017, pp. 1–6 (2017)Google Scholar
- 5.Qureshi, R., Uzair, M., Khurshid, K.: Multistage adaptive filter for ECG signal processing. In: 2017 International Conference on Communication, Computing and Digital Systems (C-CODE), Islamabad, Pakistan, 2017, pp. 363–368 (2017)Google Scholar
- 6.Shin, S.W., Kim, K.S., Song, C.G., Lee, J.W., Kim, J.H., Jeung, G.W.: Removal of baseline wandering in ECG signal by improved detrending method. BioMed. Mater. Eng.
**26**, S1087–S1093 (2015)Google Scholar - 7.Agrawal, S., Gupta, A.: Fractal and EMD based removal of baseline wander and powerline interference from ECG signals. Comput. Biol. Med.
**43**, 1889–1899 (2015)CrossRefGoogle Scholar - 8.Manivel, K., Rabindran, R.S.: Noise removal for baseline wander and power line in electrocardiograph signals. Int. J. Adv. Res. Electr. Electron. Instrum. Eng.
**4**, 1114–1122 (2015)Google Scholar - 9.Mali, B., Zulj, S., Magjarevic, R., Miklavcic, D., Jarm, T.: Matlab-based tool for ECG and HRV analysis. Biomed. Signal Process. Control
**10**, 108–116 (2014)CrossRefGoogle Scholar - 10.Kohler, B.U., Hennig, C., Orglmeister, R.: The principles of software QRS detection. In: IEEE Engineering in Medicine and Biology Magazine, vol. 21, no. 1, pp. 42–57, Jan.–Feb. 2002 (2002)Google Scholar
- 11.Diker, A., Avci, E., Gedıkpinar, M.: Determination of R-peaks in ECG signal using hilbert transform and pan-tompkins algorithms. In: 25th Signal Processing and Communications Applications Conference (SIU), Antalya, Turkey, 2017, pp. 1–4 (2017)Google Scholar
- 12.Elgendi, M., Mohamed, A., Ward, R.: Efficient ECG compression and QRS detection for e-health applications. Sci. Rep.
**7**, 1–16 (2017)CrossRefGoogle Scholar - 13.Chen, C., Chuang, C.: A QRS detection and R point recognition method for wearable single-lead ECG devices. Sensors
**17**(9), 1–19 (2017)CrossRefGoogle Scholar - 14.Qin, Q., Jianqing, l., Yinggao, Y., Chengyu, L.: An adaptive and time-efficient ECG R-peak detection algorithm. J. Healthc. Eng. 2017, 1–14 (2017)Google Scholar
- 15.Rahman, M.A.: MATLAB based functions for ECG signal preprocessing. MATLAB Central File Exchange. Retrieved October 16, 2018. https://www.mathworks.com/matlabcentral/fileexchange/69118-matlab-based-functions-for-ecg-signal-preprocessing. Accessed 16 Oct 2018
- 16.Liu, F., Liu, C., Jiang, X., et al.: Performance analysis of ten common QRS detectors on different ECG application cases. J. Healthc. Eng.
**2018**, 1–8 (2018)Google Scholar - 17.Queiroz, J.A., Barros, A.K.: Computer diagnosis for arrhythmia and atrial fibrillation based on electrocardiogram voltage variation. J. Cardiol. Clin. Res.
**6**(2), 1–4 (2018)Google Scholar - 18.Lyon, A., Mincholé, A., Martínez, J.P., Laguna, P., Rodriguez, B.: Computational techniques for ECG analysis and interpretation in light of their contribution to medical advances. J. R. Soc. Interface
**15**, 1–18 (2018)CrossRefGoogle Scholar - 19.Mandala, S., Di, T.C.: ECG parameters for malignant ventricular arrhythmias: a comprehensive review. J. Med. Biol. Eng.
**37**(4), 441–453 (2017)CrossRefGoogle Scholar - 20.Meijborg, V.M.F., Conrath, C.E., Opthof, T., Belterman, C.N.W., de Bakker, J.M.T., Coronel, R.: Electrocardiographic T wave and its relation with ventricular repolarization along major anatomical axes. Circ. Arrhythm. Electrophysiol.
**7**, 524–531 (2014)CrossRefGoogle Scholar - 21.Ideker, R.E., Kong, W., Pogwizd, S.: Purkinje fibers and arrhythmias. Pacing Clin. Electrophysiol.
**32**(3), 283–285 (2009)CrossRefGoogle Scholar - 22.Rahman, M.A., Milu, M.M.H., Anjum, A., Khanam, F., Ahmad, M.: Baseline wandering removal from ECG signal by wandering path finding algorithm. In: 3rd International Conference on Electrical Information and Communication Technology (EICT), Khulna, Bangladesh, 2017, pp. 1–5 (2017). https://doi.org/10.1109/eict.2017.8275164
- 23.Gilani, S.O., Ilyas, Y., Jamil, M.: Power line noise removal from ECG signal using notch, band stop and adaptive filters. In: International Conference on Electronics, Information, and Communication (ICEIC), Honolulu, HI, Hawaii, 2018, pp. 1–4 (2018)Google Scholar
- 24.Huamani, R., Talavera, J.R., Mendoza, E.A.S., Dávila, N.M., Supo, E.: Implementation of a real-time 60 Hz interference cancellation algorithm for ECG signals based on ARM cortex M4 and ADS1298. In: International Conference on Electronics, Electrical Engineering and Computing (INTERCON), Cusco, Peru, 2017, pp. 1–4 (2017)Google Scholar
- 25.Rana, K.P.S., Kumar, V., Gupta, A.: A pole-radius-varying IIR notch filter with enhanced post-transient performance. Biomed. Signal Process. Control
**33**, 379–391 (2017)CrossRefGoogle Scholar - 26.Khanam, F., Rahman, M.A., Ahmad, M.: Evaluating alpha relative power of EEG signal during psychophysiological activities in Salat. In: International Conference on Innovations in Science, Engineering and Technology 2018 (ICISET), 27–28 October, 2018, International Islamic University Chittagong (IIUC), Bangladesh, pp. 1–6 (2018)Google Scholar
- 27.Zhang, F., Fu, J.: Noise elimination based on moving average by Guassian distribution weighting method. In: 2nd International Conference on Control, Automation and Robotics (ICCAR), Hong Kong, 2016, pp. 169–172 (2016)Google Scholar
- 28.Awal, M.A., Mostafa, S.S., Ahmad, M.: Performance analysis of Savitzky–Golay smoothing filter using ECG signal. Int. J. Comput. Inf. Technol.
**1**(2), 24–29 (2011)Google Scholar - 29.Mahamdya, M.A., Riley, H.B.: Performance study of different denoising methods for ECG signals. In: 4th International Conference on Current and Future Trends of Information and Communication Technologies in Healthcare, ICTH-2014, vol. 34, pp. 325–332 (2014). https://doi.org/10.1016/j.procs.2014.08.048
- 30.Rahman, M.A., Haque, M.M., Anjum, A., Khanam, F., Mollah, M.N., Ahmad, M.: Classification of motor imagery events from prefrontal hemodynamics for BCI application. In: International Joint Conference on Computational Intelligence (IJCCI), 14–15 December, 2018, Daffodil International University, Dhaka, Bangladesh, pp. 1–6 (2018)Google Scholar
- 31.Balda, R.A., Diller, G., Deardorff, E., Doue, J., Hsieh, P.: The HP ECG analysis program. In: Trends in Computer Processed Electrocardiograms. North Holland, Amsterdam, pp. 197–205 (1977)Google Scholar
- 32.Ahlstrom, M.L., Tompkins, W.J.: Automated high-speed analysis of Holter tapes with microcomputers. IEEE Trans. Biomed. Eng.
**30**, 651–657 (1983)CrossRefGoogle Scholar