This note provides an interactive, visual simulation of ECG (Electrocardiogram) signal processing. It demonstrates four key processing modules: live signal viewing, interactive filtering, peak detection, and arrhythmia analysis using Poincaré plots.
The simulation loads ECG data sampled at 360 Hz and provides tools to visualize and analyze cardiac signals. You can load your own ECG data files (JSON format) or use the default sample data included with the simulation.
Math behind the Simulation
1. FIR Filtering (Finite Impulse Response)
The simulation implements FIR filters using a moving average (boxcar) window approach. For a low-pass filter with cutoff frequency \(f_c\) and sampling rate \(f_s\):
-
Filter Order: \(N = \max(3, \lfloor 50 / (f_c / (f_s/2)) \rfloor)\)
This determines the window size based on the normalized cutoff frequency.
-
Low-Pass Filter: \(y[n] = \frac{1}{N} \sum_{k=0}^{N-1} x[n - k]\)
The output is the average of the previous \(N\) samples.
-
High-Pass Filter: \(y[n] = x[n] - y_{LP}[n]\)
The high-pass filtered signal is obtained by subtracting the low-pass filtered signal from the original signal. This removes baseline wander (low-frequency components).
2. Peak Detection Algorithm
R-peaks are detected using a threshold-based algorithm with minimum distance constraint:
-
A peak is identified at sample \(i\) if: \(x[i] > \text{threshold}\) AND \(x[i] > x[i-1]\) AND \(x[i] > x[i+1]\)
-
Minimum distance between peaks: \(\Delta t_{min} = 0.3\) seconds (300 ms), corresponding to a maximum heart rate of 200 BPM.
-
This prevents multiple detections from the same QRS complex.
3. RR Interval Calculation
For detected peaks at times \(t_0, t_1, t_2, ..., t_n\), the RR intervals are calculated as:
-
\(RR_i = (t_i - t_{i-1}) \times 1000\) (in milliseconds)
4. Poincaré Plot
The Poincaré plot visualizes heart rate variability by plotting consecutive RR intervals:
-
X-axis: \(RR(n)\) - the \(n\)-th RR interval
-
Y-axis: \(RR(n+1)\) - the next RR interval
-
Each point represents a pair of consecutive heartbeats, revealing patterns in heart rate variability and potential arrhythmias.
Usage Example
Follow these steps to explore the ECG signal processing simulation:
-
Load Data: The simulation automatically loads the default ECG data file (ecg_data.json) on startup. To use your own data, click "Choose File" and select a JSON file containing an array of ECG voltage values.
-
Select Data Range: Use the "Data Start" and "Data Span" sliders to focus on a specific portion of the ECG signal. The Data Start slider selects where to begin processing, and the Data Span slider determines how much data to analyze.
-
Module 1 - Live Signal Viewer:
- Select "1. Live Signal Viewer" from the Module dropdown.
- Click "Play" to start the scrolling animation (displays a 5-second window).
- Adjust the "Speed" slider to control playback speed (0.1x to 5.0x).
- Click "Pause" to stop and view the full selected data span. When paused, you can zoom and pan the plot manually.
-
Module 2 - Interactive Filtering:
- Select "2. Interactive Filtering" from the Module dropdown.
- Choose "High-Pass" to remove baseline wander (default) or "Low-Pass" to remove high-frequency noise.
- Adjust the "Cutoff Frequency" slider to see real-time filtering effects.
- Observe how the filtered signal (red) compares to the raw signal (gray, semi-transparent).
-
Module 3 - Peak Detection:
- Select "3. Peak Detection" from the Module dropdown.
- Optionally, select a filter type (High-Pass recommended for removing baseline wander) and adjust the cutoff frequency.
- Adjust the "Threshold" slider to find the optimal sensitivity for R-peak detection.
- Observe the red triangular markers on detected peaks and the "Beats Detected" count below the chart.
-
Module 4 - Poincaré Plot:
- Select "4. Poincaré Plot" from the Module dropdown.
- Optionally, select a filter type (High-Pass recommended) and adjust the cutoff frequency to improve peak detection accuracy.
- Observe the Poincaré plot on the right, showing RR interval variability.
- Hover over points in the Poincaré plot to see the corresponding ECG segment in the left chart automatically zoomed to that heartbeat.
- Move the mouse away to return to the full view.
Tips:
-
For noisy signals, apply high-pass filtering (0.5 Hz) before peak detection to remove baseline wander.
-
Adjust the threshold in Module 3 to balance between missing peaks (threshold too high) and false detections (threshold too low).
-
In the Poincaré plot, tightly clustered points indicate regular heart rhythm, while scattered points suggest variability or arrhythmias.
Modules
The simulation provides four distinct processing modules that can be selected from the dropdown menu:
-
1. Live Signal Viewer: Displays the ECG signal in a scrolling, live-monitor style view. Use the Play/Pause button to control playback and adjust the Speed slider to change scrolling velocity. When paused, you can zoom and pan the plot manually.
-
2. Interactive Filtering: Applies FIR (Finite Impulse Response) filtering to the ECG signal. Choose between low-pass (removes high-frequency noise, 0.5-200 Hz) or high-pass (removes baseline wander, 0.05-100 Hz) filters. Adjust the Cutoff Frequency slider to see the effect of filtering in real-time. The raw signal is shown in gray (semi-transparent), and the filtered signal is displayed in red.
-
3. Peak Detection: Detects R-peaks (the highest amplitude points in QRS complexes) using a threshold-based algorithm. Optional filtering can be applied before detection (None, Low-Pass, or High-Pass). Adjust the Filter Type, Cutoff Frequency, and Threshold sliders to find the optimal sensitivity for beat detection. Detected peaks are marked with red triangular markers, and the total number of beats detected is displayed below the chart. Both raw and filtered signals (if filtering is enabled) are displayed
for comparison.
-
4. Poincaré Plot: Analyzes heart rate variability by plotting consecutive RR intervals (RR(n) vs RR(n+1)). Optional filtering can be applied before peak detection (None, Low-Pass, or High-Pass). The left chart shows the ECG signal (raw and filtered if enabled), and the right chart shows the Poincaré plot. Hover over points in the Poincaré plot to automatically zoom the ECG chart to the corresponding heartbeat segment.
Controls
-
Module Selector: Dropdown menu to switch between the four processing modules. Only one module is visible at a time.
-
Load Data: File input button to load custom ECG data files. The file should be a JSON array of floating-point numbers representing voltage values. If no file is loaded, the simulation uses the default sample data (ecg_data.json).
-
Data Start (s): Slider to select the starting point in the data file. Range: 0 to (file duration - 0.1) seconds, step: 0.1 seconds.
-
Data Span (s): Slider to select the duration of data to process. Range: 0.1 to (file duration) seconds, step: 0.1 seconds. Default: full file duration.
-
Play/Pause (Module 1): Starts or stops the scrolling animation of the live signal viewer. When playing, displays a 5-second scrolling window. When paused, displays the full selected data span.
-
Speed Slider (Module 1): Controls the scrolling speed of the live signal viewer. Range: 0.1x to 5.0x.
-
Filter Type (Module 2): Dropdown to select filter type: Low-Pass (removes high-frequency noise) or High-Pass (removes baseline wander).
-
Cutoff Frequency Slider (Module 2): Adjusts the cutoff frequency of the FIR filter. Range: 0.05-100 Hz (High-Pass) or 0.5-200 Hz (Low-Pass).
-
Filter Type (Module 3): Dropdown to select filter type: None (no filter), Low-Pass, or High-Pass. Filtering is applied before peak detection.
-
Cutoff Frequency Slider (Module 3): Adjusts the cutoff frequency of the FIR filter (if filtering is enabled). Range: 0.05-100 Hz (High-Pass) or 0.5-200 Hz (Low-Pass).
-
Threshold Slider (Module 3): Sets the detection threshold for R-peak detection. Range: 0.0 to 2.0 mV.
-
Filter Type (Module 4): Dropdown to select filter type: None (no filter), Low-Pass, or High-Pass. Filtering is applied before peak detection for RR interval calculation.
-
Cutoff Frequency Slider (Module 4): Adjusts the cutoff frequency of the FIR filter (if filtering is enabled). Range: 0.05-100 Hz (High-Pass) or 0.5-200 Hz (Low-Pass).
Data Format
The ECG data should be provided as a JSON array of floating-point numbers representing voltage measurements in millivolts. The simulation assumes a sampling rate of 360 Hz. Example format:
[0.25, 0.30, 0.35, 1.50, 1.80, 1.60, 0.40, 0.25, ...]