Skip to content

Commit

Permalink
update: tpt
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamWysokinski committed Dec 13, 2024
1 parent 036d9d5 commit 59b4538
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/NeuroAnalyzer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ include("internal/map_channels.jl")
include("internal/markers.jl")
include("internal/misc.jl")
include("internal/ml.jl")
include("internal/peaks.jl")
include("internal/plots.jl")
include("internal/recorder.jl")
include("internal/reflect_chop.jl")
Expand Down
21 changes: 13 additions & 8 deletions src/analyze/tpt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@ function tpt_detect(obj::NeuroAnalyzer.NEURO)::Vector{Int64}
acc_y = obj.data[5, :, :][:]
acc_z = obj.data[6, :, :][:]

p_idx_x, _ = findpeaks1d(pos_x, distance=(sr(obj) ÷ 2), height=mean(pos_x) + 2*std(pos_x))
p_idx_y, _ = findpeaks1d(pos_y, distance=(sr(obj) ÷ 2), height=mean(pos_y) + 2*std(pos_y))
p_idx_z, _ = findpeaks1d(pos_z, distance=(sr(obj) ÷ 2), height=mean(pos_z) + 2*std(pos_z))
p_idx_acc_x, _ = findpeaks1d(acc_x, distance=(sr(obj) ÷ 2), height=mean(acc_x) + 2*std(acc_x))
p_idx_acc_y, _ = findpeaks1d(acc_y, distance=(sr(obj) ÷ 2), height=mean(acc_y) + 2*std(acc_y))
p_idx_acc_z, _ = findpeaks1d(acc_z, distance=(sr(obj) ÷ 2), height=mean(acc_z) + 2*std(acc_z))

p_idx = round.(Int64, p_idx_x)
t = obj.time_pts

p_idx_x = _tpt_peaks(pos_x, t)
p_idx_y = _tpt_peaks(pos_y, t)
p_idx_z = _tpt_peaks(pos_z, t)
p_idx_acc_x = _tpt_peaks(acc_x, t)
p_idx_acc_y = _tpt_peaks(acc_y, t)
p_idx_acc_z = _tpt_peaks(acc_z, t)

p_idx = unique(sort(union(p_idx_x, p_idx_y, p_idx_acc_x, p_idx_acc_y)))
tx = t[p_idx]
tx = [tx[1]; diff(tx)]
p_idx = p_idx[tx .>= 0.2]

_info("Detected pinches: $(length(p_idx))")

Expand Down
2 changes: 1 addition & 1 deletion src/gui/iview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,7 @@ Nothing
"""
function iview(obj1::NeuroAnalyzer.NEURO, obj2::NeuroAnalyzer.NEURO; zoom::Real=10)::Nothing

(signal_len(obj1) / sr(obj1)) < zoom && (zoom = round(obj1.time_pts[end]) / 2)
obj1.time_pts[end] < zoom && (zoom = round(obj1.time_pts[end]) / 2)

@assert size(obj1) == size(obj2) "Both signals must have the same size."
@assert obj1.header.recording[:channel_order] == obj2.header.recording[:channel_order] "Both signals must have the same order."
Expand Down
16 changes: 16 additions & 0 deletions src/internal/peaks.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function _tpt_peaks(x::AbstractVector, t::AbstractVector)::Vector{Int64}
x[x .< 0] .= 0
x = diff(x)
t = t[1:end-1]
x[x .< 0] .= 0
#x = derivative(x)
dx = env_up(x, d=4, t)
verbose_tmp = NeuroAnalyzer.verbose
NeuroAnalyzer.verbose = false
f = filter_create(fprototype=:fir, ftype=:lp, cutoff=15, n=length(dx), fs=50, order=8)
NeuroAnalyzer.verbose = verbose_tmp
dx = filter_apply(dx, flt=f)
p_idx, _ = findpeaks1d(dx, distance=10, height=mean(dx) + 0.25 * std(dx))
# p_idx, _ = findpeaks1d(dx2, distance=(sr(obj) ÷ 4), height=mean(dx) + 1.5 * std(dx))
return p_idx
end
2 changes: 1 addition & 1 deletion src/tester/tpt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ function tpt(; duration::Int64=20, port_name::String="/dev/ttyUSB0")::NeuroAnaly
# sampling rate is 50 Hz = 20 ms per loop
fs = 50
t = collect(0:1/fs:duration)
t = t[1:(end - 1)]
tpt_ch_x = zeros(length(t))
tpt_ch_y = zeros(length(t))
tpt_ch_z = zeros(length(t))
Expand Down Expand Up @@ -218,7 +219,6 @@ function tpt(; duration::Int64=20, port_name::String="/dev/ttyUSB0")::NeuroAnaly
println()
println()
println("Testing completed")

tpt_signal = Matrix([tpt_ch_x tpt_ch_y tpt_ch_z tpt_ch_accx tpt_ch_accy tpt_ch_accz]')
tpt_signal = reshape(tpt_signal, 6, :, 1)

Expand Down

0 comments on commit 59b4538

Please sign in to comment.