using CSV, Plots, Images #, TimeSeries
using StatsBase
using Interact
plotlyjs()
These are all separate files I wrote to handle chunks of the process that are rather mundane.
include("findpeaks.jl")
include("pan_tompkins.jl")
include("run_pan_tompkins.jl")
Run Pan-Tompkins on a chunk of arrhythmia data
fs_arrhyth = 360;
sample_arrhyth = CSV.read("207.csv",header = ["Time", "ECG1", "ECG2"], datarow=1, nullable=false)
fs = fs_arrhyth;
x = sample_arrhyth[360*1700:360*1730,3];
t = sample_arrhyth[360*1700:360*1730,1];
x_HP, x_MAF, qrs_i, qrs_c, SIGL_buf, NOISL_buf, THRS_buf, qrs_i_raw, qrs_amp_raw, SIGL_buf1, NOISL_buf1, THRS_buf1 = run_pan_tompkins(x,t,fs);
#Filtered signal
plot(t,x_HP,title = "Filtered Signal after Algorithm",xlabel="Time (sec)",label = "Filtered Signal")
plot!(t[qrs_i_raw],qrs_amp_raw,line = :scatter,label = "Identified Peaks")
plot!(t[qrs_i_raw],SIGL_buf1,label = "Signal Level")
plot!(t[qrs_i_raw],NOISL_buf1, label = "Noise Level")
plot!(t[qrs_i_raw],THRS_buf1, label = "Threshold")
Separate into individual "beats" based on just breaking halfway in between successive peaks.
#Separate based on halfway in between peaks
breaks = zeros(Int64,0);
breaks = append!(breaks,1)
for i = 1:length(qrs_i_raw)-1
breaks = append!(breaks,round(0.5.*(qrs_i_raw[i] + qrs_i_raw[i+1])))
end
breaks = append!(breaks,min(length(t),2*qrs_i_raw[end]-breaks[end]))
breaks[1] = max(1,2*qrs_i_raw[1]-breaks[2]);
plot(t,x_HP,title = "Filtered Signal after Algorithm",xlabel="Time (sec)")
plot!(t[qrs_i_raw],qrs_amp_raw,line = :scatter)
plot!(t[breaks],line = :vline)
Run Pan-Tompkins on the full dataset from a single subject.
#Test on arrhythmia sample
fs = fs_arrhyth;
x = sample_arrhyth[:,3];
t = sample_arrhyth[:,1];
x_HP, x_MAF, qrs_i, qrs_c, SIGL_buf, NOISL_buf, THRS_buf, qrs_i_raw, qrs_amp_raw, SIGL_buf1, NOISL_buf1, THRS_buf1 = run_pan_tompkins(x,t,fs);
include("get_breaks.jl")
Separate into individual beats as above and plot.
breaks = get_breaks(t,qrs_i_raw);
length(breaks)
@manipulate for n in 1:length(breaks)-1
plot(t[breaks[n]:breaks[n+1]],x_HP[breaks[n]:breaks[n+1]],title = "Beat #$n")
#plot!(t[qrs_i])
end