❤️ ECG Anomaly Detection with Template Matching: A Streamlit App Walkthrough

Listen to this article

Electrocardiograms (ECGs) are vital for monitoring heart health. However, subtle abnormalities can be difficult to spot by eye. This blog introduces an interactive Streamlit app that uses template matching to detect such anomalies in ECG signals.

This example is focused on ECG, but anomaly detection can be used for many more applications.

The tool is available here: 🌟 ECG Anomaly Detector – AI Science Talk Blog


🚀 Getting Started

The app is designed for simplicity and clarity. Here’s how to use it:

1. Upload Your ECG File

To begin, upload your ECG file in .txt format. Make sure the file contains a single column of numeric ECG values, one value per line.

📌 Note: The app does not currently support multi-channel ECG files.

2. Preview Your Data

Once uploaded, the app will show a preview of the ECG signal (first 1000 samples) so you can ensure the data looks correct.

📈 Data Preview Placeholder


⚙️ Configure Analysis Settings

On the sidebar, you’ll find three key parameter controls to fine-tune the analysis:

📏 Peak Detection

  • Minimum distance between peaks: Controls how far apart detected peaks must be.
  • Minimum peak height: Sets the height threshold for peak detection.

These parameters help isolate individual heartbeats from the ECG trace.

📍 Detected Peaks on ECG Signal


🔄 Cycle Extraction

For each detected peak, the app extracts a window of samples around it:

  • Samples before peak: The number of data points to include before the detected peak.
  • Samples after peak: The number of data points to include after.

This forms an individual beat cycle, which will be compared against a “template” beat.


📊 Template Matching Logic

The core of the detection algorithm is based on template matching:

  1. Template creation: The app computes a median beat from all extracted cycles — this serves as the reference template.
  2. Distance calculation: Each cycle is compared to the template using Euclidean distance.
  3. Anomaly thresholding: Based on a user-defined expected anomaly proportion, a threshold is calculated. Cycles that exceed this threshold are flagged as anomalous.

📉 Template vs. Beat Cycles


⚠️ Anomaly Detection on the ECG Signal

The app highlights the anomalous beats on the original ECG signal, helping you quickly identify where issues may occur.

🔺 ECG Signal with Anomalies Highlighted


💾 Exporting Your Results

After analysis, you can download a .csv file containing:

  • The sample index of each detected peak
  • The distance of that beat to the median template
  • A binary anomaly flag (1 = anomalous, 0 = normal)

This lets you perform further offline analysis or integrate results into clinical records.


🎯 Final Thoughts

This app combines intuitive visualizations with a robust, data-driven approach to anomaly detection. While it’s not a substitute for clinical diagnosis, it can serve as a powerful assistive tool for:

  • Pre-screening ECGs
  • Spotting irregularities in wearable device data
  • Educational demonstrations

You can run it within the aisciencetalk.blog website at 🌟 ECG Anomaly Detector – AI Science Talk Blog


Need Something Similar?

This is a hands-on example of how artificial intelligence can be applied in real-world scenarios.

If you have sensor data (even raw), and want to extract value from it — let’s talk.

Contact Me → https://aisciencetalk.blog/contact-us/

Disclamer

This content has been generated by an artificial intelligence language model. While we strive for accuracy and quality, please note that the information provided may not be entirely error-free or up-to-date. We recommend independently verifying the content and consulting with professionals for specific advice or information. We do not assume any responsibility or liability for the use or interpretation of this content.

Copyright

Author: Simone Togni

Platform: aisciencetalk.blog

Leave a Reply

Scroll to Top

Discover more from AI Science Talk Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading