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
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!