HeartBeats.jl provides a heartbeat detector based on the approach described by Pan & Tompkins (1985). It is based on the implementation available in the Python package SleepECG.
Use the package manager to add HeartBeats.jl by typing ] add HeartBeats
in the Julia REPL.
HeartBeats.jl contains a short example ECG dataset taken from scipy.misc.electrocardiogram()
. The function example_ecg()
returns this data, which was sampled with a sampling frequency of 360 Hz, as a Vector{Float64}
. We can use this dataset to showcase the detect_heartbeats()
function:
using HeartBeats
ecg = example_ecg()
fs = 360 # sampling frequency
beats = detect_heartbeats(ecg, fs)
The beats
array will contain all detected R peak locations.
The detector is based on the Python implementation available in SleepECG. It is about 18× faster than the Python implementation and only 2× slower than the C implementation. Follow these steps to reproduce the benchmark:
- Export all data records used in the
'runtime'
benchmark by includingexport_records = True
inconfig.yml
(refer to the SleepECG documentation for details on how to set up and run the benchmarks). This will generate 15 text files. - Move those text files to a folder that you can access from Julia (i.e. set the Julia working directory accordingly).
- Run the following code snippet to benchmark the runtime for 60 minute data segments (note that you need to add the
CSV
package):
using CSV, HeartBeats
function run_benchmark()
total = 0
fs = 128
files = filter(x -> endswith(x, ".txt"), readdir(".", join=true))
for file in files
println(file)
data = CSV.File(file, comment="#")
ecg = view(data[:ecg], 1:60*60*fs) # 60 minutes
stats = @timed detect_heartbeats(ecg, fs)
total += stats.time
end
println("Total: $total, Average: $(total / length(files))")
total
end
run_benchmark()