Electrocardiogram (ECG)
Electrocardiography (ECG) is a non-invasive technique used to measure the electrical activity of the heart. ECG signals are often used to measure heart rate, heart rate variability (HRV), and respiratory rate. In PhysioKit, we provide a variety of routines for processing ECG signals.
physiokit.ecg.defines
EcgFiducial
EcgSegment
physiokit.ecg.clean
ECG cleaning methods.
clean(data, lowcut=0.5, highcut=30, sample_rate=1000, order=3, axis=-1, forward_backward=True)
Clean ECG signal by applying biquad filter.
By default, a 3rd order Butterworth bandpass filter from 0.5 to 30 Hz is applied.
Parameters:
-
data
(NDArray
) –ECG signal.
-
lowcut
(float
, default:0.5
) –Lower cutoff in Hz. Defaults to 0.5 Hz.
-
highcut
(float
, default:30
) –Upper cutoff in Hz. Defaults to 30 Hz.
-
sample_rate
(float
, default:1000
) –Sampling rate in Hz. Defaults to 1000 Hz.
-
order
(int
, default:3
) –Filter order. Defaults to 3 (3rd order Butterworth filter).
-
axis
(int
, default:-1
) –Axis to apply against. Defaults to -1.
-
forward_backward
(bool
, default:True
) –Apply filter forward and backward. Defaults to True.
Returns:
-
NDArray
–npt.NDArray: Cleaned ECG signal.
Source code in physiokit/ecg/clean.py
square_filter_mask(rr_ints, lowcut=300, highcut=900)
Mask out RR intervals that fall outside bounds.
Parameters:
-
rr_ints
(NDArray
) –RR-interval list in ms.
-
lowcut
(float
, default:300
) –Lower cutoff limit. Defaults to 300 ms.
-
highcut
(float
, default:900
) –Upper cutoff limit. Defaults to 900 ms.
Returns:
-
NDArray
–npt.NDArray: RR rejection mask 0=accept, 1=reject.
Source code in physiokit/ecg/clean.py
physiokit.ecg.metrics
compute_heart_rate(data, sample_rate=1000, method='peak', **kwargs)
Compute heart rate from ECG signal.
Parameters:
-
data
(array
) –ECG signal.
-
sample_rate
(float
, default:1000
) –Sampling rate in Hz. Defaults to 1000 Hz.
-
method
(str
, default:'peak'
) –Method to compute heart rate. Defaults to 'peak'.
-
**kwargs
(dict
, default:{}
) –Keyword arguments to pass to method.
Returns:
Source code in physiokit/ecg/metrics.py
compute_heart_rate_from_peaks(data, sample_rate=1000, min_rr=0.3, max_rr=2.0, min_delta=0.3)
Compute heart rate from peaks of ECG signal.
Parameters:
-
data
(array
) –ECG signal.
-
sample_rate
(float
, default:1000
) –Sampling rate in Hz. Defaults to 1000 Hz.
Returns:
Source code in physiokit/ecg/metrics.py
derive_respiratory_rate(peaks, rri=None, sample_rate=1000, method='rifv', lowcut=0.1, highcut=1.0, order=3, threshold=0.85, interpolate_method='linear')
Derive respiratory rate from ECG signal using given method.
Parameters:
-
peaks
(array
) –QRS peaks of ECG signal.
-
rri
(array
, default:None
) –RR intervals. Defaults to None.
-
sample_rate
(float
, default:1000
) –Sampling rate in Hz. Defaults to 1000 Hz.
-
method
(str
, default:'rifv'
) –Method to compute respiratory rate. Defaults to 'riav'.
-
lowcut
(float
, default:0.1
) –Lowcut frequency in Hz. Defaults to 0.1 Hz.
-
highcut
(float
, default:1.0
) –Highcut frequency in Hz. Defaults to 1.0 Hz.
-
order
(int
, default:3
) –Order of filter. Defaults to 3.
-
threshold
(float
, default:0.85
) –Threshold for peak detection. Defaults to 0.85.
Returns:
Source code in physiokit/ecg/metrics.py
physiokit.ecg.peaks
compute_rr_intervals(peaks)
Compute RR intervals from R peaks.
Parameters:
-
peaks
(array
) –R peaks.
Returns:
-
NDArray
–npt.NDArray: RR intervals.
Source code in physiokit/ecg/peaks.py
filter_peaks(peaks, sample_rate=1000, min_rr=0.3, max_rr=2.0, min_delta=0.3)
Filter out peaks with RR intervals outside of normal range.
Parameters:
-
peaks
(array
) –R peaks.
-
sample_rate
(float
, default:1000
) –Sampling rate in Hz. Defaults to 1000 Hz.
-
min_rr
(float
, default:0.3
) –Minimum RR interval in seconds. Defaults to 0.3 s.
-
max_rr
(float
, default:2.0
) –Maximum RR interval in seconds. Defaults to 2.0 s.
-
min_delta
(float
, default:0.3
) –Minimum RR interval delta. Defaults to 0.3.
Returns:
-
NDArray
–npt.NDArray: Filtered peaks.
Source code in physiokit/ecg/peaks.py
filter_rr_intervals(rr_ints, sample_rate=1000, min_rr=0.3, max_rr=2.0, min_delta=0.3)
Filter out peaks with RR intervals outside of normal range.
Parameters:
-
rr_ints
(array
) –RR intervals.
-
sample_rate
(float
, default:1000
) –Sampling rate in Hz. Defaults to 1000 Hz.
-
min_rr
(float
, default:0.3
) –Minimum RR interval in seconds. Defaults to 0.3 s.
-
max_rr
(float
, default:2.0
) –Maximum RR interval in seconds. Defaults to 2.0 s.
-
min_delta
(float
, default:0.3
) –Minimum RR interval delta. Defaults to 0.3.
Returns:
-
NDArray
–npt.NDArray: RR interval mask.
Source code in physiokit/ecg/peaks.py
find_peaks(data, sample_rate=1000, qrs_window=0.1, avg_window=1.0, qrs_prom_weight=1.5, qrs_min_len_weight=0.4, qrs_min_delay=0.3)
Find R peaks in ECG signal using QRS gradient method.
Parameters:
-
data
(array
) –ECG signal.
-
sample_rate
(float
, default:1000
) –Sampling rate in Hz. Defaults to 1000 Hz.
-
qrs_window
(float
, default:0.1
) –Window size in seconds to compute QRS gradient. Defaults to 0.1 s.
-
avg_window
(float
, default:1.0
) –Window size in seconds to compute average gradient. Defaults to 1.0 s.
-
qrs_prom_weight
(float
, default:1.5
) –Weight to compute minimum QRS height. Defaults to 1.5.
-
qrs_min_len_weight
(float
, default:0.4
) –Weight to compute minimum QRS length. Defaults to 0.4.
-
qrs_min_delay
(float
, default:0.3
) –Minimum delay between QRS complexes. Defaults to 0.3 s.
Returns:
-
NDArray
–npt.NDArray: R peaks.
Source code in physiokit/ecg/peaks.py
physiokit.ecg.segment
apply_segmentation(data, sample_rate=1000)
Apply segmentation to ECG signal.
Source code in physiokit/ecg/segment.py
find_pwave()
find_qrs()
find_twave()
locate_pwave_from_qrs_anchor(data, qrs_seg, sample_rate=1000, wave_window=0.1, avg_window=0.3, wave_prom_weight=1.0, wave_min_window=0.01)
Locate P wave in ECG signal using QRS anchor method.
Parameters:
-
data
(array
) –ECG signal.
-
qrs_seg
(tuple[int, int, int]
) –QRS segment.
-
sample_rate
(float
, default:1000
) –Sampling rate in Hz. Defaults to 1000 Hz.
-
wave_window
(float
, default:0.1
) –Window size in seconds to compute wave gradient. Defaults to 0.1 s.
-
avg_window
(float
, default:0.3
) –Window size in seconds to compute average gradient. Defaults to 0.3 s.
-
wave_prom_weight
(float
, default:1.0
) –Weight to compute minimum wave height. Defaults to 1.0.
-
wave_min_window
(float
, default:0.01
) –Minimum wave length in seconds. Defaults to 0.05 s.
Returns:
Source code in physiokit/ecg/segment.py
locate_qrs(data, sample_rate=1000, qrs_window=0.1, avg_window=1.0, qrs_prom_weight=1.5, qrs_min_len_weight=0.4, qrs_min_delay=0.3)
Find QRS segments in ECG signal using QRS gradient method.
Parameters:
-
data
(array
) –ECG signal.
-
sample_rate
(float
, default:1000
) –Sampling rate in Hz. Defaults to 1000 Hz.
-
qrs_window
(float
, default:0.1
) –Window size in seconds to compute QRS gradient. Defaults to 0.1 s.
-
avg_window
(float
, default:1.0
) –Window size in seconds to compute average gradient. Defaults to 1.0 s.
-
qrs_prom_weight
(float
, default:1.5
) –Weight to compute minimum QRS height. Defaults to 1.5.
-
qrs_min_len_weight
(float
, default:0.4
) –Weight to compute minimum QRS length. Defaults to 0.4.
-
qrs_min_delay
(float
, default:0.3
) –Minimum delay between QRS complexes. Defaults to 0.3 s.
Returns:
-
tuple[NDArray]
–tuple[npt.NDArray]: QRS segments
Source code in physiokit/ecg/segment.py
locate_twave_from_qrs_anchor(data, qrs_seg, sample_rate=1000, wave_window=0.3, avg_window=0.4, wave_prom_weight=1.0, wave_min_window=0.1)
Locate T wave in ECG signal using QRS anchor method.
Parameters:
-
data
(array
) –ECG signal.
-
qrs_seg
(tuple[int, int, int]
) –QRS segment.
-
sample_rate
(float
, default:1000
) –Sampling rate in Hz. Defaults to 1000 Hz.
-
wave_window
(float
, default:0.3
) –Window size in seconds to compute wave gradient. Defaults to 0.3 s.
-
avg_window
(float
, default:0.4
) –Window size in seconds to compute average gradient. Defaults to 0.6 s.
-
wave_prom_weight
(float
, default:1.0
) –Weight to compute minimum wave height. Defaults to 1.0.
-
wave_min_window
(float
, default:0.1
) –Minimum wave length in seconds. Defaults to 0.1 s.
Returns:
Source code in physiokit/ecg/segment.py
physiokit.ecg.synthesize
simulate_daubechies(signal_length=10000, sample_rate=1000, heart_rate=70)
Generate an artificial (synthetic) ECG signal of a given duration and sampling rate.
It uses a 'Daubechies' wavelet that roughly approximates a single cardiac cycle.
This function is based on this script <https://github.com/diarmaidocualain/ecg_simulation>
_.
Parameters:
-
signal_length
(int
, default:10000
) –Length of the ECG signal. Defaults to 10000.
-
sample_rate
(float
, default:1000
) –ECG sampling frequency. Defaults to 1000.
-
heart_rate
(float
, default:70
) –Heart rate in BPM. Defaults to 70.
Returns:
-
NDArray
–npt.NDArray: Synthetic ECG signal
Source code in physiokit/ecg/synthesize.py
simulate_ecgsyn(signal_length=10000, sample_rate=256, leads=12, heart_rate=60, hr_std=1, lfhfratio=0.5, sfint=512, ti=(-70, -15, 0, 15, 100), ai=(1.2, -5, 30, -7.5, 0.75), bi=(0.25, 0.1, 0.1, 0.1, 0.4), gamma=None)
Simulate ECG using the ECGSYN algorithm.
This function is a python translation of the matlab script by McSharry & Clifford (2013)
<https://physionet.org/content/ecgsyn>
_.
Parameters:
-
signal_length
(int
, default:10000
) –Length of the ECG signal. Defaults to 10000.
-
sample_rate
(float
, default:256
) –ECG sampling frequency. Defaults to 256.
-
heart_rate
(float
, default:60
) –Mean heart rate. Defaults to 60.
-
hr_std
(float
, default:1
) –Heart rate standard deviation. Defaults to 1.
-
lfhfratio
(float
, default:0.5
) –Low frequency high frequency ratio. Defaults to 0.5.
-
sfint
(float
, default:512
) –Internal sampling frequency. Defaults to 512.
-
ti
(tuple[int]
, default:(-70, -15, 0, 15, 100)
) –Time parameters. Defaults to (-70, -15, 0, 15, 100).
-
ai
(tuple[float]
, default:(1.2, -5, 30, -7.5, 0.75)
) –Amplitude parameters. Defaults to (1.2, -5, 30, -7.5, 0.75).
-
bi
(tuple[float]
, default:(0.25, 0.1, 0.1, 0.1, 0.4)
) –Width parameters. Defaults to (0.25, 0.1, 0.1, 0.1, 0.4).
-
gamma
(NDArray | None
, default:None
) –Lead modification matrix. Defaults to None.
Returns:
-
NDArray
–tuple[list[npt.NDArray], list[npt.NDArray]]: ECG signals and results
Source code in physiokit/ecg/synthesize.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
|
synthesize(signal_length=10000, sample_rate=1000, leads=12, heart_rate=60, preset=EcgPreset.SR, noise_multiplier=1.0, impedance=1.0, p_multiplier=1.0, t_multiplier=1.0, voltage_factor=300)
Generate synthetic ECG signal via brisk method.
Utilize pk.signal.noise methods to make more realistic.
Leads are indexed as follows
["I", "II", "III", "aVR", "aVL", "aVF", "V1", "V2", "V3", "V4", "V5", "V6"]
Parameters:
-
signal_length
(int
, default:10000
) –Length of the ECG signal. Defaults to 10000.
-
sample_rate
(float
, default:1000
) –ECG sampling frequency. Defaults to 1000.
-
leads
(int
, default:12
) –Number of leads. Defaults to 12.
-
heart_rate
(float
, default:60
) –Mean heart rate. Defaults to 60.
-
preset
(EcgPreset
, default:SR
) –ECG preset. Defaults to EcgPreset.SR.
-
noise_multiplier
(float
, default:1.0
) –Noise multiplier. Defaults to 1.0.
-
impedance
(float
, default:1.0
) –Impedance. Defaults to 1.0.
-
p_multiplier
(float
, default:1.0
) –P wave multiplier. Defaults to 1.0.
-
t_multiplier
(float
, default:1.0
) –T wave multiplier. Defaults to 1.0.
-
voltage_factor
(float
, default:300
) –Voltage factor. Defaults to 300.
Returns:
-
tuple[NDArray, NDArray, NDArray]
–npt.NDArray: Synthetic ECG signals