# mhrv.ecg¶

## bpfilt()¶

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).

Parameters: 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

Example: preprocess an ecg recording from the mitdb (physionet.com) by applying a bandpass filter (0.5-100 Hz)

download_wfdb_records('mitdb', '105', '.');
[~,ecg,Fs]=rdsamp('mitdb/105',1);

bpfecg = bpfilt(ecg,Fs,0.5,100,[],1);


## bsqi()¶

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.

Parameters: 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)

Example: Comparing gqrs to wjqrs on a record from mitdb.

download_wfdb_records('mitdb', '105', '.');
[tm,ecg,Fs]=rdsamp('mitdb/105',1,'from',450000,'to',480000);
bpfecg = bpfilt(ecg,Fs,5,45,[],0); % prefilter in range [5 - 45] Hz
anns_jqrs = wjqrs(bpfecg,Fs,0.3,0.250,10); % wjqrs peak detector
anns_gqrs = gqrs('mitdb/105','from',450000,'to',480000); % gqrs peak detector
anns_gqrs = double(anns_gqrs);

[F1,~] = bsqi( anns_jqrs', anns_gqrs,0.050,Fs);
plot(tm,ecg);
hold on; plot(tm(anns_jqrs),ecg(anns_jqrs),'+r');
hold on; plot(tm(anns_gqrs),ecg(anns_gqrs),'+k');
legend(['ecg with quality ' num2str(F1)],'wjqrs','gqrs');

 [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.

## ecgsqi()¶

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.

Parameters: 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)

Example: an ecg sample from the mitdb (physionet.org) is downloaded, preprocessed and two peak detectors are ran (wjqrs and gqrs). The two sets of R-peak annotations are used to compare the signal quality using the ecgsqi function.

download_wfdb_records('mitdb', '105', '.');
recordName = 'mitdb/105';
[~,ecg,Fs]=rdsamp(recordName,1);
bpfecg = bpfilt(ecg,Fs,5,45,[],0); % prefilter in range [5 - 45] Hz
anns_jqrs = wjqrs(bpfecg,Fs,0.3,0.250,10); % wjqrs peak detector
anns_gqrs = gqrs(recordName); % gqrs peak detector
anns_gqrs = double(anns_gqrs);

[ sqi, tsqi ] = ecgsqi( anns_jqrs', anns_gqrs, ecg, Fs, 'plot', true, 'mw', 1, 'sw', 5, 'rw', 1, 'agw', 0.050);


## jqrs()¶

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).

Parameters: 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 from the mitdb (physionet.org). A bandpass filter is used to preprocess the ECG in the range [4 - 45]Hz. Then the peak detector is ran with a refractory period of 250 ms (a standard value for Human ECG analysis) is specified.

download_wfdb_records('mitdb', '105', '.');
[~,ecg,Fs]=rdsamp('mitdb/105',1);

bpfecg = bpfilt(ecg,Fs,4,45,[],0); % prefilter in range [4-45] Hz

qrs_pos = jqrs(bpfecg,Fs,0.4,0.250,1); % peak detection and plot

 [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.

## wjqrs()¶

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.

Parameters: 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)

Example: perform peak detection on an ecg recording from the mitdb (physionet.org), A refractory period of 250 ms (a standard value for Human ECG) and a threshold of 0.3 are used.

download_wfdb_records('mitdb', '105', '.');
[~,ecg,Fs]=rdsamp('mitdb/105',1);
bpfecg = bpfilt(ecg,Fs,4,45,[],0); % prefilter in range [4-45] Hz

anns_jqrs = wjqrs(bpfecg,Fs,0.3,0.250,10); % jqrs running on each segment of 10 sec length


## qrs_adjust()¶

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.

Parameters: 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 (sec) debug – (boolean) cqrs: adjusted (corrected) qrs positions (sample number)

Example:

download_wfdb_records('mitdb', '105', '.');
[~,ecg,Fs]=rdsamp('mitdb/105',1);
bpfecg = bpfilt(ecg,Fs,4,45,[],0); % prefilter in range [4-45] Hz
anns_jqrs = wjqrs(bpfecg,Fs,0.3,0.250,10); % jqrs running on each segment of 10 sec length

`