Standard image-domain augmentations — random crops, color jitter, horizontal flips — are semantically arbitrary when applied to ECG waveforms. A horizontally flipped P-wave no longer represents a valid sinus beat; a color-shifted QRS complex conveys no physiological meaning. The SSRL-ECG augmentation pipeline is built around a different principle: every transformation must correspond to a real source of variation observed in clinical ECG recordings. This alignment between augmentation design and cardiac physiology is what enables the 7-technique pipeline to deliver a +12.15% F1 improvement (0.5750 → 0.6448) over a supervised CNN baseline trained on the same 10% label budget.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Tumo505/SSL-for-ECG-classification/llms.txt
Use this file to discover all available pages before exploring further.
ECGAugmentations at a Glance
TheECGAugmentations class is the single entry point for all augmentation logic. It accepts a raw ECG tensor and returns two independently augmented views suitable for contrastive or momentum-based SSL objectives.
| Parameter | Default | Meaning |
|---|---|---|
signal_length | 5000 | Samples per recording (500 Hz × 10 s = 5000) |
sampling_rate | 500 | Hz — used for frequency-domain calculations |
prob_strong | 0.8 | Probability gate for the entire strong-augmentation branch |
(channels, time) and batch (batch, channels, time) inputs and returns output in the same format.
Weak Augmentations (Always Applied)
Weak augmentations are small, safe perturbations applied unconditionally to every sample before the strong branch runs. They model the irreducible measurement noise present in any real clinical recording.Gaussian Jitter
Mechanism: Additive white Gaussian noise with std=0.03 (≈3% of signal amplitude)
Simulates: Sensor electronics noise, EMI interference
Application rate: 90%
Amplitude Scaling
Mechanism: Uniform global scale factor drawn from
[1 − 0.15, 1 + 0.15]
Simulates: Inter-session gain calibration differences
Application rate: 80%Per-Channel Noise
Mechanism: Independent Gaussian noise per lead, level ∈ [0.5%, 2%] of that lead’s std
Simulates: Varying contact quality across 12 electrode sites
Application rate: 60%
Strong Augmentations (Probabilistic)
The strong-augmentation branch fires with probabilityprob_strong (default 0.8). When active, all nine strong transforms are applied in sequence, each with its own internal application rate. This creates high augmentation diversity while leaving 20% of samples with only weak perturbations, preserving some easy positive pairs for training stability.
The Domain-Specific Strong Techniques
| Augmentation | Mechanism | Physiological Simulation | Rate |
|---|---|---|---|
| Frequency warping | Non-linear time-index remapping via random control points (±5% jitter) | Natural heart rate variability (±5 bpm) across the recording | 50% |
| Medical mixup | Convex combination λx + (1−λ)x', λ ~ Beta(0.1, 0.1), with random within-batch partner | Averaged readings from patients with similar pathologies | 40% |
| Bandpass filtering | FFT-domain mask with random f_low ∈ [0, 5] Hz, f_high ∈ [50, 250] Hz | Different recording devices with non-identical frequency responses | 50% |
| Segment CutMix | Replace a 10–30% contiguous time window with the corresponding window from a random batch partner | Stitched recordings, electrode switching mid-session | 30% |
| Motion artifacts | Sinusoidal baseline wander (amplitude ~ Uniform(0.5, 2.0) @ 0.5–2 Hz) plus a high-frequency noise burst | Patient movement, loose electrode contact, respiratory artifact | 50% |
| Segment cropping | Remove a contiguous 10–30% window and interpolate linearly across the gap | Missing data, electrode contact loss, transmission dropout | 50% |
| Temporal dropout | Zero-out (fill with batch mean) a contiguous segment; multiple segments possible | Signal interruptions, sensor dropouts, transmission loss | 50% |
Frequency Warping — Implementation Detail
Frequency Warping — Implementation Detail
_time_warp() creates a smooth non-linear index mapping using randomly placed control points. The endpoint positions are fixed (no boundary artifacts), while interior waypoints are jittered by up to ±5% of the signal length. The mapping is computed via np.interp, then applied as an integer index gather — no learnable parameters are involved.Medical Mixup — Implementation Detail
Medical Mixup — Implementation Detail
_augment_mixup() samples λ from a symmetric Beta(0.1, 0.1) distribution, which concentrates weight near 0 and 1 (most mixes are dominated by one sample). A random within-batch permutation selects the mixing partner. Unlike image mixup, ECG mixup is physiologically plausible: a blended waveform resembles a composite of overlapping cardiac conditions.Motion Artifacts — Implementation Detail
Motion Artifacts — Implementation Detail
_augment_motion_artifacts() injects 1–3 artifact bursts per sample. Each burst combines a low-frequency sinusoidal baseline shift (modeling slow body movement or respiratory drift at 0.5–2 Hz) with an additive high-frequency noise burst (std=0.3, modeling rapid electrode motion). Amplitude is drawn from Uniform(0.5, 2.0), matching realistic ambulatory ECG noise profiles.Additional Strong Transforms
Beyond the 7 domain-specific techniques, the strong branch includes two general temporal transforms that further diversify the representation space:Time Shift
Circularly shifts the entire signal by up to ±10% of its length (zero-padded, not wrapped). Applied with 70% probability. Simulates recording start offset and rhythm phase variation.
Channel Shift
Applies an independent random scale (±10%) and DC offset (±0.05) to each of the 12 leads separately. Applied with 60% probability. Simulates per-electrode impedance and placement variation.
Weak vs. Strong: The Two-Tier Design
The split between weak (always-on) and strong (gated) augmentations is a deliberate design choice with two benefits:- Training Stability
- Augmentation Diversity
Always applying at least the three weak transforms ensures that every positive pair has some meaningful difference to resolve. Pure identical pairs would produce zero contrastive gradient. At the same time, leaving 20% of iterations without strong augmentation provides easier positive pairs that stabilize early training when the encoder representations are still noisy.