PeakSwift is a Swift package designed for accurate and real-time R-peak detection in single-lead Electrocardiogram (ECG) data, tailored for the iOS environment. PeakSwift provides also functionality for context-aware R-Peak detection and ECG signal quality evaluation.
- Features
- Requirements
- Installation
- Usage
- Test suite
- Dependencies
- Contributing
- PeakWatch - Demo app
- License
- 9 R-Peak detectors (Christov, nabian2018, Hamilton, TwoAverage, NeuroKit, Pan & Tompkins, UNSW, Engzee, Kalidas)
- 2 Signal quality evaluators (Zhao2018 Simple, Zhao2018 Fuzzy)
- Context-aware R-Peak detection
Platform | Minimum Swift Version | Installation | Status |
---|---|---|---|
iOS 13.0+ / macOS 10.15+ | 5.3 | Swift Package Manager | Fully Tested |
To install PeakSwift using Swift Package Manager you can follow the tutorial published by Apple using the URL for the PeakSwift repo on the main branch:
- In Xcode, select “File” → “Add Packages...”
- Enter https://github.com/CardioKit/PeakSwift
or you can add the following dependency to your Package.swift
:
dependencies: [
.package(url: "https://github.com/CardioKit/PeakSwift.git", branch: "main")
]
You need to restrict the package version to:
platforms: [
.macOS(.v10_15),
.iOS(.v13)
]
And add PeakSwift to your target library.
targets: [
.target(
name: "<YourLibrary>",
dependencies: ["PeakSwift"])
]
First of all, configure the ECG, you would like to analyze.
let ecg: [Double] = /* put your ECG here*/
let samplingRate: Double = /* put your SamplinRate here*/
let electrocardiogram = Electrocardiogram(ecg: ecg, samplingRate: samplingRate)
PeakSwift provides a R-Peak detection feature as follows:
let qrsDetector = QRSDetector()
// A default algorithm will be selected
qrsDetector.detectPeaks(electrocardiogram: electrocardiogram)
// Alternative: An algorithm may be specified
let qrsResult = qrsDetector.detectPeaks(electrocardiogram: electrocardiogram, algorithm: .neurokit)
// Extract results
let rPeaks = qrsResult.rPeaks
let cleanedSignal = qrsResult.cleanedElectrocardiogram
You can pass the ECG signal context to PeakSwift and let PeakSwift decide on the most suitable algorithm.
let qrsDetector = QRSDetector()
// The most suitable algorithm for signal with Atrial Firbrillation will be selcted
qrsDetector.detectPeaks(electrocardiogram: electrocardiogram) { config in
config.setClassification(.atrialFibrillation)
}
You can also directly specify the context provided by HealthKit:
import HealthKit
let qrsDetector = QRSDetector()
qrsDetector.detectPeaks(electrocardiogram: electrocardiogram) { config in
config.setClassification(fromHealthKit: .sinusRhythm)
}
PeakSwift support signal quality evaluations:
let signalQualityEvaluator = ECGQualityEvaluator()
let signalQuality = signalQualityEvaluator.evaluateECGQuality(
electrocardiogram: electrocardiogram,
algorithm: .zhao2018(.fuzzy))
// signalQuality has to be unacceptable, barelyAcceptable or excellent
The python-based library NeuroKit is used to generate the test data.
Firstly, install the necessary dependencies. We recommend to use the virtual environment manager (venv) for isolating the dependencies.
pip3 install -r requirements.txt
To generate the test data & run the test suite use following commands:
# Generate test data
cd TestDataGenerator
python3 main.py
# Run swift based test suite
cd ..
swift build
swift test
PeakSwift relies on the following libraries:
We welcome contributions to enhance PeakSwift:
- Fork the repository.
- Create a new branch:
git checkout -b feature/your-feature-name
- Commit your changes:
git commit -am 'feat(Scope): Add some feature'
- Push to the branch:
git push origin feature/your-feature-name
- Create a pull request.
To illustrate and analyze the functionalities of PeakSwift, a dedicated demo app is developed. It integrates R-Peak detection and signal quality analysis of PeakSwift and can be evaluated with an external analysis tool.
You can explore PeakWatch here: PeakWatch.
This software is for research purposes only.
PeakSwift is released under Apache License 2.0. See LICENSE for details.