GitHunt
SE

SergeyFilipov/ecg-arrhythmia-detection

Deep learning model for automated classification of cardiac arrhythmias using ECG signals from the MIT-BIH database. The project combines signal preprocessing via wavelet transform and a multi-layer CNN architecture, achieving over 98% test accuracy across 15 heartbeat classes. Designed for real-time and clinical applications.

๐Ÿซ€ ECG Arrhythmia Detection with Deep Learning
A deep learning pipeline for automatic classification of cardiac arrhythmias using ECG signals from the MIT-BIH Arrhythmia Database.
The model combines signal denoising with wavelet transforms and a multi-layer CNN architecture, achieving over 98% test accuracy across 15 heartbeat classes.

Real-time ready. Clinically inspired. Built with โค๏ธ, Python, and TensorFlow.

๐Ÿ“ 1. Dataset: MIT-BIH Arrhythmia Database
The project uses ECG signals from the mitdb/ folder, which contains .dat and .atr files from the MIT-BIH Arrhythmia Database.

Signals are loaded using the wfdb library.

Only 15 clinically meaningful heartbeat symbols are retained.

Each ECG segment is sliced to 256 samples, centered around the detected QRS complex.

๐Ÿ“Œ Retained heartbeat classes:
['N', 'A', 'V', 'R', 'L', 'a', '!', 'F', 'f', 'j', 'J', '/', 'E', 'x', 'e']

๐Ÿ” Code: get_signal_data(), get_instances()

๐ŸŒ€ 2. Signal Preprocessing with Wavelets
To reduce noise and preserve key signal structures, we apply wavelet-based denoising:

Wavelet: Symlet-7

Decomposition: 8 levels

Thresholding: MAD (Median Absolute Deviation)

Transformation: pywt.dwt(signal, 'sym7')

๐Ÿ”ง Code: apply_wavelet()

This process enhances signal clarity and suppresses high-frequency noise, improving model performance.

๐Ÿ“Š 3. Visualization of Raw and Filtered ECG
You can visualize ECG signals before and after preprocessing using:
plot_signal('100', start=0, end=1000, save_fig=True)

Results are saved automatically to the plots/ folder.

Great for debugging and inspecting waveform patterns.

๐Ÿ–ผ๏ธ Code: plot_signal()
๐Ÿ“ Output: plots/

๐Ÿง  4. CNN Architecture for ECG Classification
The model uses dilated temporal convolutions to capture long-range dependencies between waveform peaks:

7 Conv1D layers with dilation rates: [1, 2, 4, 8, 16, 32, 64]

Global max pooling layer

Dense layers: 256 โ†’ 128 โ†’ 15 (Softmax)

This architecture balances computational efficiency with depth for real-time readiness.

๐Ÿงฉ Code: get_model()
๐Ÿ’พ Saved model: model.keras

๐Ÿ‹๏ธโ€โ™€๏ธ 5. Model Training & Evaluation
Training configuration:

Optimizer: Adam

Loss: Categorical Crossentropy

Callback: EarlyStopping (patience = 5)

After training:

Training curves (accuracy/loss) are saved as training_metrics.png

Evaluation is performed using a confusion matrix (absolute + normalized)

๐Ÿ“ˆ Code: training loop model.fit()
๐Ÿ“Š Evaluation: confusion_matrix()

๐Ÿ” 6. Misclassification Analysis
To inspect errors, we visualize misclassified ECG segments for each class:

Randomly selected 5 misclassified signals per class

Facilitates interpretation of confusion patterns

๐ŸŽฏ Code: for i, n in enumerate(names):

๐Ÿš€ 7. How to Run
โœ… Install dependencies
pip install -r requirements.txt

โ–ถ๏ธ Run the full pipeline
python ecg_arrhythmia_classifier.py

๐Ÿ’ก Or try the interactive version
jupyter notebook ecg_arrhythmia_classifier.ipynb

๐Ÿ“ฆ Project Structure
File/Folder Description
ecg_arrhythmia_classifier.py Full model pipeline โ€“ from data to prediction
model.keras Trained Keras model for direct use
plots/ ECG and training plots
mitdb/ Folder containing ECG signal data

๐Ÿ“œ License
This project is released under the MIT License.
Feel free to use, fork, and adapt โ€“ but cite the original repository!

Languages

Jupyter Notebook99.9%Python0.1%

Contributors

MIT License
Created May 29, 2025
Updated January 6, 2026