mhrv.ecg.bpfilt(ecg, fs, lcf, hcf, nt, debug)

This function [1] is made for prefiltering an ecg time series before it is passed through a peak detector. Of important note: the upper cut off (hcf) and lower cutoff (lcf) of the bandpass filter that are applied, highly depends on the mammal that is being considered. This is particularly true for the hcf which will be higher for a mouse ECG file versus a Human ecg. This is because the QRS is `sharper’ for a mouse ecg than for a Human one. Of note, NaN should be represented by the value -32768 in the ecg (WFDB standard).

  • ecg – electrocardiogram (mV)
  • fs – sampling frequency (Hz)
  • lcf – low cut-off frequency (Hz)
  • hcf – high cut-off frequency (Hz)
  • debug – plot output filtered signal (boolean)
  • nt – frequency to cut with a Notch filter (Hz). Leave the field empty (‘[]’) if you do not want the Notch filter to be applied.

  • bpfecg: band pass filtered ecg signal


mhrv.ecg.bsqi(refqrs, testqrs, agw, fs)

This algorithm can be used to estimate the signal quality of a single channel electrocardiogram [3] [4]. It compares the agreement between the R-peak annotations (ann1 and ann2) made by two different R-peak detectors. If the two detectors agree locally then the signal quality (sqi) is good and if they disagree then it is likely because of some uderlying artifacts/noise in the signal. The limitation of this method is that when one of the two detectors fails for whatever reason other than the presence of noise then the quality will be zero.

  • refqrs – vector of QRS annotations from a first peak detector (in seconds)
  • testqrs – vector of QRS annotations from a secon peak detector (in seconds)
  • agw – agreement window (sec)
  • fs – sampling frequency (Hz)

  • F1: signal quality measure (nu)
  • IndMatch: indices of matching peaks (nu)
  • meanDist: mean distance between matching refqrs and testqrs peaks (sec)

[3](1, 2) Behar, J., Oster, J., Li, Q., & Clifford, G. D. (2013). ECG signal quality during arrhythmia and its application to false alarm reduction. IEEE transactions on biomedical engineering, 60(6), 1660-1666.
[4](1, 2) Johnson, Alistair EW, Joachim Behar, Fernando Andreotti, Gari D. Clifford, and Julien Oster. “Multimodal heart beat detection using signal quality indices.” Physiological measurement 36, no. 8 (2015): 1665.


mhrv.ecg.ecgsqi(ann1, ann2, ecg, fs, varargin)

Computes bsqi [3] [4] to estimate the signal quality of an ECG signal. The bsqi index is computed over the whole ecg recording with a granularity specified by the user.

  • ann1 – vector of QRS annotations from a first peak detector (sample)
  • ann2 – vector of QRS annotations from a secon peak detector (sample)
  • ecg – electrocardiogram time series (mV)
  • fs – sampling frequency of the ecg (Hz)
  • varargin

    pass in name-value pairs to configure advanced options:

    • agw: agreement window tolerated between annotations from the two peak detectors in order for the two annotations to be considered in agreement (in seconds)
    • sw: the size of the window on which the signal quality is computed (in seconds). Default is 5 sec.
    • rw: granularity at which the sqi is computed (in seconds). By default the sqi is computed at every second.
    • mw: window for a post-processing median smoothing (in number of samples). By default there is no median post-processing.
    • thrsqi: final sqi threshold. The signal will be considered of good quality for all sqi value above this threshold (output sqi will be one for these). Conversely the signal will be considered of bad quality for all sqi value below this threshold (output sqi will be zero for these).
    • plot: true/false whether to generate a plot. Defaults to true if no output arguments were specified.

  • sqi: signal quality time series (nu, in range 0-1)
  • tsqi: time vector for the sqi vector (sec)


[ sqi, tsqi ] = ecgsqi( ann_jqrs, ann_gqrs, ecg, fs, 'plot', true, 'mw', 1, 'sw', 5, 'rw', 1, 'agw', 0.050);


mhrv.ecg.jqrs(ecg, fs, thr, rp, debug)

Implementation of an energy based qrs detector [1]. The algorithm is an adaptation of the popular Pan & Tompkins algorithm [2]. The function assumes the input ecg is already pre-filtered i.e. bandpass filtered and that the power-line interference was removed. Of note, NaN should be represented by the value -32768 in the ecg (WFDB standard).

  • ecg – vector of ecg signal amplitude (mV)
  • fs – sampling frequency (Hz)
  • thr – threshold (nu)
  • rp – refractory period (sec)
  • debug – plot results (boolean)

  • qrs_pos: position of the qrs (sample)

Example: process an ecg recording sampled at 1000 Hz and specifying a refractory period of 250 ms (a standard value for Human ECG) and with a threshold of 0.5.

qrs_pos = ptqrs(bpecg,1000,0.5,0.250,0);
[1](1, 2, 3, 4) Behar, Joachim, Alistair Johnson, Gari D. Clifford, and Julien Oster. “A comparison of single channel fetal ECG extraction methods.” Annals of biomedical engineering 42, no. 6 (2014): 1340-1353.
[2]Pan, Jiapu, and Willis J. Tompkins. “A real-time QRS detection algorithm.” IEEE Trans. Biomed. Eng 32.3 (1985): 230-236.


mhrv.ecg.wjqrs(ecg, fs, thres, rp, ws)

This function [1] is used to run the jqrs peak detector using a sliding (non-overlapping) window. This is usefull in the cases where the signal contains important artefacts which could bias the jqrs threshold evaluation or if the amplitude of the ecg is changing substantially over long recordings because of the position of the electrodes move for example. In these instances the adaptation of the energy threshold is useful.

  • ecg – ecg signal (mV)
  • fs – sampling frequency (Hz)
  • thres – threshold to be used in the P&T algorithm(nu)
  • rp – refractory period (sec)
  • ws – window size (sec)

  • qrs: qrs location in nb samples (ms)


mhrv.ecg.qrs_adjust(ecg, qrs, fs, inputsign, tol, debug)

This function [1] is used to adjust the qrs location by looking for a local min or max around the input qrs points. For example, this is useful when parts of the qrs are located on a positive part of the R-wave and parts on a negative part of the R-wave in order to make them all positive or negative - this can be useful for heart rate variability analysis. It is also useful for template substraction in the context of non-invasive fetal ECG source separation in order to ensure the maternal template is well aligned with each beat.

  • ecg – vector of ecg signal amplitude (mV)
  • qrs – peak position (sample number)
  • fs – sampling frequency (Hz)
  • inputsign – sign of the peak to look for (-1 for negative and 1 for positive)
  • tol – tolerance window (ms)
  • debug – (boolean)

  • cqrs: adjusted (corrected) qrs positions (sample number)


qrs_pos = adjust_qrs_location(bpecg,qrs_pos,1000,-1,0.050,0);