Heartbeat Anomaly Detector
A deep learning-based heart sound classification system that analyzes phonocardiogram (PCG) recordings to detect cardiac anomalies. This model combines AI-powered classification with rule-based analysis to provide comprehensive heart sound assessment.
Live Demo
Upload your heart sound recording and get instant AI-powered analysis with classification, heart rate detection, and risk assessment.
Model Description
The Heartbeat Anomaly Detector is a CNN-based deep learning model designed for automated heart sound analysis. It processes audio recordings of heart sounds and classifies them into four categories while providing detailed cardiac metrics and risk assessments.
Key Features
Multi-Class Classification : Identifies normal heart sounds, murmurs, extra heart sounds, and artifacts
Hybrid Analysis : Combines deep learning predictions with rule-based clinical logic
Real-time Processing : Fast inference suitable for clinical workflows
Comprehensive Metrics : Provides heart rate, rhythm analysis, and risk assessment
RESTful API : Easy integration via FastAPI endpoints
Model Architecture
Neural Network Design
The model uses a 4-layer Convolutional Neural Network (CNN) architecture:
Input: [1, 60000] - 30 seconds of audio at 2000 Hz sample rate
Layer 1: Conv1d(1→32) + BatchNorm + ReLU + MaxPool + Dropout(0.2)
Layer 2: Conv1d(32→64) + BatchNorm + ReLU + MaxPool + Dropout(0.2)
Layer 3: Conv1d(64→128) + BatchNorm + ReLU + MaxPool + Dropout(0.3)
Layer 4: Conv1d(128→256) + BatchNorm + ReLU + AdaptiveAvgPool + Dropout(0.3)
Fully Connected: 256 → 128 → 64 → 4 classes
Technical Specifications
Input Format : Mono audio, 2000 Hz sample rate, 30-second duration
Model Size : Approximately 10-20 MB
Framework : PyTorch
Inference Time : < 1 second per audio file
Device Support : CPU and CUDA-enabled GPU
Classes
The model classifies heart sounds into four categories:
Class Description Clinical Significance Normal Regular heart sounds with clear S1/S2 Healthy cardiac function Murmur Abnormal whooshing sounds May indicate valve disorders Extra Heart Sounds Additional sounds (S3, S4, clicks) Possible heart failure or structural issues Artifact Poor signal quality or noise Recording quality issue
Signal Processing Pipeline
Preprocessing Steps
Audio Loading : Load audio file at 2000 Hz sample rate
Median Filtering : Remove impulse noise
Bandpass Filtering : Extract 20-200 Hz range (primary heart sound frequencies)
Notch Filtering : Remove 50/60 Hz power line interference
Envelope Enhancement : Apply Hilbert transform for amplitude modulation
Normalization : RMS-based amplitude normalization
Smoothing : Savitzky-Golay filter for signal refinement
Feature Extraction
Heart Rate Detection : BioSPPy or peak detection algorithms
S1/S2 Peak Identification : First and second heart sound detection
Rhythm Analysis : Heart rate variability and irregularity scoring
Amplitude Analysis : S1/S2 amplitude ratios
Usage
Installation
pip install fastapi uvicorn torch librosa numpy scipy matplotlib biosppy
Quick Start
1. Download the Model
1 from huggingface_hub import hf_hub_download
2
3 model_path = hf_hub_download (
4 repo_id = "ai-mitra/heartbeat-anomaly-detector" ,
5 filename = "heartbeat-anomaly-detector-model.pt"
6 )
2. Run the API Server
1 # Start the FastAPI server
2 uvicorn inference:app --host 0.0 .0.0 --port 8000
3. Analyze Heart Sounds
Using cURL:
curl "http://localhost:8000/hb?filename=sounds/Heart_Failure_Sound.mp3"
Using Python:
1 import requests
2
3 response = requests . get (
4 "http://localhost:8000/hb" ,
5 params = { "filename" : "sounds/Normal_Heart_Sound.mp3" }
6 )
7 result = response . json ( )
8
9 print ( f"Classification: { result [ 'classification' ] [ 'result' ] } " )
10 print ( f"Confidence: { result [ 'classification' ] [ 'confidence_percentage' ] } " )
11 print ( f"Heart Rate: { result [ 'heart_rate' ] [ 'average_bpm' ] } BPM" )
Using JavaScript (React/Frontend):
1 fetch ( 'http://localhost:8000/hb?filename=sounds/Normal_Heart_Sound.mp3' )
2 . then ( response => response . json ( ) )
3 . then ( data => {
4 console . log ( 'Classification:' , data . classification . result ) ;
5 console . log ( 'Confidence:' , data . classification . confidence_percentage ) ;
6 console . log ( 'Heart Rate:' , data . heart_rate . average_bpm , 'BPM' ) ;
7 } ) ;
API Endpoints
GET /hb
Analyze a heart sound audio file.
Parameters:
filename (string, required): Path to the audio file
Response:
1 {
2 "status" : "OK" ,
3 "analysis_info" : {
4 "target_file" : "Normal_Heart_Sound" ,
5 "analysis_timestamp" : "2026-01-13T10:30:00" ,
6 "analysis_mode" : "Hybrid (AI + Rule-Based)" ,
7 "plot_filename" : "Normal_Heart_Sound_analysis_20260113_103000.png" ,
8 "plot_path" : "/plot/Normal_Heart_Sound_analysis_20260113_103000.png"
9 } ,
10 "ai_prediction" : {
11 "predicted_class" : "normal" ,
12 "confidence" : 0.9245 ,
13 "probabilities" : {
14 "normal" : 0.9245 ,
15 "murmur" : 0.0432 ,
16 "extrahs" : 0.0221 ,
17 "artifact" : 0.0102
18 }
19 } ,
20 "classification" : {
21 "result" : "Normal Heart Sounds" ,
22 "confidence" : 0.9245 ,
23 "confidence_percentage" : "92.5%"
24 } ,
25 "heart_rate" : {
26 "average_bpm" : 75.3 ,
27 "hr_mean" : 75.3 ,
28 "hr_std" : 3.2 ,
29 "hr_min" : 70.1 ,
30 "hr_max" : 81.5 ,
31 "irregularity_score" : 0.042
32 } ,
33 "heart_sounds" : {
34 "num_s1" : 38 ,
35 "num_s2" : 37 ,
36 "s1_peaks" : [ 245 , 1523 , 2801 , ... ] ,
37 "s2_peaks" : [ 876 , 2154 , 3432 , ... ] ,
38 "s1_s2_ratio" : 1.027
39 } ,
40 "detailed_findings" : [
41 {
42 "condition" : "AI Classification: Normal" ,
43 "description" : "Deep learning model prediction with 92.5% confidence" ,
44 "severity" : "Low" ,
45 "recommendation" : "AI Model: Normal detected" ,
46 "source" : "AI Model"
47 }
48 ] ,
49 "recommendations" : [
50 "Analysis Method: Hybrid (AI + Rule-Based)" ,
51 "AI-Rule Agreement: High"
52 ]
53 }
GET /health
Check API health status.
Response:
1 {
2 "status" : "OK" ,
3 "model_loaded" : true ,
4 "biosppy_available" : true ,
5 "timestamp" : "2026-01-13T10:30:00"
6 }
GET /plot/{filename}
Retrieve generated analysis plot image.
Response: PNG image file
Clinical Features
Arrhythmia Detection
The system detects various cardiac rhythm abnormalities:
Tachycardia : Heart rate > 100 BPM
Severe Tachycardia : Heart rate > 150 BPM
Bradycardia : Heart rate < 50 BPM
Severe Bradycardia : Heart rate < 40 BPM
Irregular Rhythm : High heart rate variability (possible atrial fibrillation)
Heart Attack Risk Assessment
Multi-factor risk scoring based on:
Heart rate extremes (tachycardia/bradycardia)
Rhythm irregularity
Multiple high-severity findings
Combined risk factors
Risk Levels:
HIGH : Risk score ≥ 6 - Immediate medical attention required
MODERATE : Risk score 3-5 - Urgent cardiology consultation needed
LOW-MODERATE : Risk score 1-2 - Follow-up recommended
LOW : Risk score 0 - Continue preventive care
Training Details
Dataset
The model was trained on a custom dataset of heart sound recordings including:
Normal heart sounds
Murmurs (various types)
Extra heart sounds (S3, S4, clicks)
Artifacts and noise
Training Configuration
Sample Rate : 2000 Hz
Audio Duration : 30 seconds
Batch Normalization : Applied in all convolutional layers
Dropout Rates : 0.2-0.5 for regularization
Loss Function : Cross-entropy loss
Optimizer : Adam (typical for CNN training)
Performance Baselines
Training statistics for different conditions:
Condition Avg Heart Rate Irregularity Score S1/S2 Ratio Normal 104.5 ± 7.6 BPM 0.317 ± 0.074 1.171 ± 0.244 Murmur 87.5 ± 27.5 BPM 0.185 ± 0.150 0.733 ± 0.092 Extra HS 82.3 ± 9.2 BPM 0.570 ± 0.068 0.836 ± 0.151
Limitations and Disclaimers
Important Medical Disclaimer
This is an automated analysis tool for educational and research purposes only
Results should NOT be used for medical diagnosis or treatment decisions
Always consult qualified healthcare professionals for medical concerns
The analysis may produce false positives/negatives
Professional medical evaluation is required for all cardiac concerns
Technical Limitations
Audio Quality Dependent : Requires clear recordings without excessive noise
Limited Pathology Coverage : Trained on specific condition types
No Real-time Monitoring : Designed for recorded audio analysis
Context Required : Best used as part of comprehensive cardiac assessment
Regional Variations : May not account for all demographic differences
Use Cases
Appropriate Use
Educational : Medical training and learning
Research : Cardiac audio analysis studies
Screening : Initial triage in low-resource settings
Monitoring : Tracking changes over time with physician oversight
Development : Building advanced cardiac diagnostic systems
Inappropriate Use
Primary diagnostic tool without physician review
Emergency medical decision-making
Replacement for echocardiography or ECG
Self-diagnosis or self-treatment
Model Files
heartbeat-anomaly-detector-model.pt - PyTorch model weights (main file)
inference.py - FastAPI server with inference code
requirements.txt - Python dependencies
Dependencies
1 fastapi>=0.68.0
2 uvicorn>=0.15.0
3 torch>=1.9.0
4 librosa>=0.9.0
5 numpy>=1.19.0
6 scipy>=1.7.0
7 matplotlib>=3.3.0
8 biosppy>=0.8.0 (optional but recommended)
Citation
If you use this model in your research, please cite:
1 @misc{heartbeat-anomaly-detector,
2 author = {AI Mitra},
3 title = {Heartbeat Anomaly Detector: CNN-based Heart Sound Classification},
4 year = {2026},
5 publisher = {Hugging Face},
6 howpublished = {\url{https://huggingface.co/ai-mitra/heartbeat-anomaly-detector}}
7 }
License
This project is licensed under the MIT License. See LICENSE file for details.
Contact and Support
For questions, issues, or contributions:
Hugging Face : https://huggingface.co/ai-mitra/heartbeat-anomaly-detector
Issues : Report bugs or request features via the repository issues page
Email : tathagata.mitra@gmail.com
Interested in taking this project forward or collaborating? Feel free to reach out via email.
Acknowledgments
BioSPPy library for cardiac signal processing
PyTorch framework for deep learning
Medical professionals who provided domain expertise
Version History
v2.0.0 (2026-01-13): Initial release with hybrid AI + rule-based analysis
4-class CNN classification
Heart rate and rhythm analysis
Risk assessment module
FastAPI integration with CORS support
Remember : This tool is for research and educational purposes. Always seek professional medical advice for health concerns.