Preprocessing & SQI API Reference¶
ecg_sdk.preprocessing.filters
¶
Digital Filtering Module for Biopotential ECG Signals¶
Implements zero-phase forward-backward Butterworth bandpass, highpass, lowpass, and IIR Notch filters (50 Hz / 60 Hz powerline cancellation) using Second-Order Sections (SOS) for numerical stability.
References
- Oppenheim, A. V., & Schafer, R. W. (2009). Discrete-Time Signal Processing.
- Clifford, G. D., Azuaje, F., & McSharry, P. (2006). Advanced Methods and Tools for ECG Data Analysis. Artech House.
butter_bandpass_filter(data, lowcut=0.5, highcut=45.0, fs=250.0, order=4)
¶
Apply a zero-phase Butterworth bandpass filter using Second-Order Sections (SOS).
Parameters¶
data : np.ndarray 1D array (N,) or 2D array (N, L) of ECG samples. lowcut : float, default=0.5 Lower cutoff frequency in Hertz (Hz). highcut : float, default=45.0 Upper cutoff frequency in Hertz (Hz). fs : float, default=250.0 Sampling frequency in Hertz (Hz). order : int, default=4 Filter order (effective order is 2*order due to bidirectional filtering).
Returns¶
np.ndarray Filtered zero-phase ECG biopotential signal with identical shape.
Source code in ecg_sdk/preprocessing/filters.py
butter_highpass_filter(data, cutoff=0.5, fs=250.0, order=4)
¶
Apply a zero-phase Butterworth highpass filter using Second-Order Sections (SOS).
Source code in ecg_sdk/preprocessing/filters.py
butter_lowpass_filter(data, cutoff=45.0, fs=250.0, order=4)
¶
Apply a zero-phase Butterworth lowpass filter using Second-Order Sections (SOS).
Source code in ecg_sdk/preprocessing/filters.py
iir_notch_filter(data, freq=50.0, fs=250.0, quality_factor=30.0)
¶
Apply a zero-phase Infinite Impulse Response (IIR) Notch filter to remove powerline interference.
Parameters¶
data : np.ndarray 1D array (N,) or 2D array (N, L) of ECG samples. freq : float, default=50.0 Center frequency to notch out in Hertz (50.0 Hz Europe/Asia, 60.0 Hz Americas). fs : float, default=250.0 Sampling frequency in Hertz (Hz). quality_factor : float, default=30.0 Quality factor Q = freq / bandwidth. Higher Q creates a narrower notch.
Returns¶
np.ndarray Notch-filtered signal with identical shape.
Source code in ecg_sdk/preprocessing/filters.py
filter_ecg(signal_or_data, fs=None, lowcut=0.5, highcut=45.0, notch_freq=50.0, notch_q=30.0, order=4)
¶
Comprehensive ECG signal conditioning pipeline applying zero-phase bandpass and powerline notch filtering.
Parameters¶
signal_or_data : ECGSignal or np.ndarray Input ECG signal container or raw numpy biopotential array. fs : float, optional Sampling frequency in Hz (mandatory if passing raw numpy array; inferred if ECGSignal). lowcut : float, default=0.5 Bandpass lower frequency cutoff (Hz). Suppresses respiratory baseline wander. highcut : float, default=45.0 Bandpass upper frequency cutoff (Hz). Suppresses high-frequency EMG muscle noise. notch_freq : float, optional, default=50.0 Powerline notch frequency (50.0 Hz or 60.0 Hz). Set to None to skip notch filtering. notch_q : float, default=30.0 Quality factor for the IIR notch filter. order : int, default=4 Butterworth filter order.
Returns¶
ECGSignal or np.ndarray Filtered signal with matched container type and preserved metadata.
Source code in ecg_sdk/preprocessing/filters.py
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 | |
ecg_sdk.preprocessing.baseline
¶
Baseline Wander Removal Module¶
Removes low-frequency baseline drift (caused by patient respiration, perspiration, and electrode motion) using the standard clinical two-stage cascaded median filter or polynomial detrending.
References
- de Chazal, P., O'Dwyer, M., & Reilly, R. B. (2004). Automatic classification of heartbeats using ECG morphology and heartbeat interval features. IEEE TBME.
- Clifford, G. D., Azuaje, F., & McSharry, P. (2006). Advanced Methods and Tools for ECG Data Analysis. Artech House.
cascaded_median_baseline_wander(data, fs=250.0, window1_ms=200.0, window2_ms=600.0)
¶
Estimate and remove baseline wander using a two-stage cascaded 1D median filter.
The first stage applies a ~200 ms median filter to suppress the P-wave and QRS complex. The second stage applies a ~600 ms median filter to suppress the T-wave, yielding the estimated low-frequency baseline drift b(t).
Parameters¶
data : np.ndarray 1D array (N,) or 2D array (N, L) of ECG samples. fs : float, default=250.0 Sampling frequency in Hertz (Hz). window1_ms : float, default=200.0 First median filter window in milliseconds (to remove QRS and P waves). window2_ms : float, default=600.0 Second median filter window in milliseconds (to remove T wave).
Returns¶
cleaned_data : np.ndarray ECG signal with baseline wander subtracted. baseline : np.ndarray Estimated baseline drift component b(t).
Source code in ecg_sdk/preprocessing/baseline.py
polynomial_detrend(data, order=3)
¶
Fit and subtract an n-th degree polynomial baseline from the signal.
Parameters¶
data : np.ndarray 1D (N,) or 2D (N, L) ECG array. order : int, default=3 Polynomial degree.
Returns¶
cleaned_data : np.ndarray baseline : np.ndarray
Source code in ecg_sdk/preprocessing/baseline.py
remove_baseline_wander(signal_or_data, fs=None, method='cascaded_median', window1_ms=200.0, window2_ms=600.0, poly_order=3, return_baseline=False)
¶
High-level baseline drift removal dispatcher supporting ECGSignal or numpy arrays.
Parameters¶
signal_or_data : ECGSignal or np.ndarray Input ECG biopotential signal. fs : float, optional Sampling frequency in Hz (required if passing np.ndarray). method : {"cascaded_median", "polynomial"}, default="cascaded_median" Baseline estimation algorithm. window1_ms : float, default=200.0 Stage-1 median filter window (for cascaded_median). window2_ms : float, default=600.0 Stage-2 median filter window (for cascaded_median). poly_order : int, default=3 Polynomial degree (for polynomial method). return_baseline : bool, default=False If True, also returns the extracted baseline array.
Returns¶
cleaned_signal : ECGSignal or np.ndarray baseline : np.ndarray (optional, if return_baseline=True)
Source code in ecg_sdk/preprocessing/baseline.py
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 | |
ecg_sdk.preprocessing.quality
¶
Signal Quality Index (SQI) Engine¶
Implements multi-metric electrophysiological signal quality assessment to detect noise, muscle artifact (EMG), electrode detachment, powerline interference, and severe respiratory baseline wander.
References
- Clifford, G. D., Behar, J., Li, Q., & Rezek, I. (2012). Signal quality indices and data fusion for determining clinical acceptability of electrocardiograms. Physiological Measurement, 33(9), 1419–1433.
- Orphanidou, C., et al. (2015). Signal-quality indices for the electrocardiogram and photoplethysmogram: derivation and applications to mobile monitoring. IEEE JBHI, 19(3), 832–838.
SignalQualityResult
dataclass
¶
Comprehensive multi-lead Signal Quality Index report.
Source code in ecg_sdk/preprocessing/quality.py
summary()
¶
Render a clean text table of lead quality grades.
Source code in ecg_sdk/preprocessing/quality.py
LeadQuality
dataclass
¶
Quality metrics for a single ECG lead.
Source code in ecg_sdk/preprocessing/quality.py
assess_signal_quality(signal_or_data, fs=None, lead_names=None, acceptance_threshold=0.6)
¶
Evaluate multi-lead Signal Quality Index (SQI) across all channels.
Parameters¶
signal_or_data : ECGSignal or np.ndarray Input ECG biopotential signal. fs : float, optional Sampling frequency in Hz (required if passing np.ndarray). lead_names : list of str, optional Lead labels corresponding to channels. acceptance_threshold : float, default=0.60 Minimum composite SQI threshold for acceptable clinical quality.
Returns¶
SignalQualityResult Structured report containing overall score, clinical grade, and per-lead metrics.
Source code in ecg_sdk/preprocessing/quality.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | |
evaluate_lead_quality(data_1d, fs=250.0, lead_name='Lead', acceptance_threshold=0.6)
¶
Assess quality indices for a single 1D lead signal and calculate composite score.
Source code in ecg_sdk/preprocessing/quality.py
calculate_psqi(data_1d, fs=250.0, qrs_band=(5.0, 15.0), total_band=(5.0, 40.0))
¶
Compute Power Spectrum SQI (pSQI): Ratio of power in the QRS band (5-15 Hz) relative to the total ECG diagnostic frequency band (5-40 Hz).
Higher pSQI indicates well-defined QRS complexes with minimal baseline or high-frequency distortion.
Source code in ecg_sdk/preprocessing/quality.py
calculate_ksqi(data_1d)
¶
Compute Kurtosis SQI (kSQI): 4th standardized statistical moment. Clean ECG with tall R-peaks has kSQI > 5.0. Gaussian noise has kSQI ≈ 3.0.
Source code in ecg_sdk/preprocessing/quality.py
calculate_ssqi(data_1d)
¶
Compute Skewness SQI (sSQI): 3rd standardized statistical moment.
Source code in ecg_sdk/preprocessing/quality.py
calculate_baseline_sqi(data_1d, fs=250.0)
¶
Compute Baseline Drift Ratio (basSQI): Power below 0.5 Hz / power in 0.5-40 Hz. Lower values indicate less baseline drift contamination.
Source code in ecg_sdk/preprocessing/quality.py
calculate_hf_sqi(data_1d, fs=250.0)
¶
Compute High-Frequency Noise Ratio (hfSQI): Power above 45 Hz / power in 0.5-40 Hz. Lower values indicate less EMG muscle or powerline noise.