data("stimulation_signal")
signal <- stimulation_signal$signal
sample_rate <- stimulation_signal$sample_rate
# each pulse is roughly <0.001 seconds
pulse_durations <- 0.001
# Initial pulses
pulse_info <- stimpulse_find(signal, sample_rate, pulse_durations)
# number of pulses detected
pulse_info$n_pulses
# extract responses -10 points before onset ~ 20 points after offset
expand_timepoints <- c(-20, 80)
pulses_snippets <- stimpulse_extract(
signal = signal,
pulse_info = pulse_info,
expand_timepoints = expand_timepoints
)
# Visualize the pulses
snippet_time <- seq(
expand_timepoints[[1]], by = 1,
length.out = nrow(pulses_snippets)) / sample_rate * 1000
matplot(snippet_time, pulses_snippets, type = 'l', lty = 1, col = 'gray80',
xlab = "Time (ms)", ylab = "uV", main = "Initial find")
# Align the pulses
pulse_info <- stimpulse_align(signal, pulse_info)
# Estimated pulse duration
estimated_duration <-
(pulse_info$offset_index - pulse_info$onset_index + 1) / sample_rate
# reload aligned pulses
pulses_snippets <- stimpulse_extract(
signal = signal,
pulse_info = pulse_info,
expand_timepoints = expand_timepoints
)
matplot(snippet_time, pulses_snippets, type = 'l', lty = 1, col = 'gray80',
xlab = "Time (ms)", ylab = "uV", main = "Aligned pulses")
lines(snippet_time, rowMeans(pulses_snippets), col = 'red')
# Interpolate the pulses
interpolated <- stimpulse_interpolate(
signal = signal,
sample_rate = sample_rate,
pulse_info = pulse_info,
max_offset = c(-0.0003, 0.0005)
)
interp_snippets <- stimpulse_extract(
signal = interpolated,
pulse_info = pulse_info,
expand_timepoints = expand_timepoints
)
oldpar <- par(mfrow = c(1, 2))
on.exit(par(oldpar))
matplot(snippet_time, pulses_snippets, type = 'l', lty = 1,
col = 'gray80', xlab = "Time (ms)", ylab = "uV",
main = "Stim pulses", ylim = c(-600, 400))
lines(snippet_time, rowMeans(pulses_snippets), col = 'red')
abline(v = max(estimated_duration) * 1000, lty = 2)
matplot(snippet_time, interp_snippets, type = 'l', lty = 1,
col = 'gray80', xlab = "Time (ms)", ylab = "uV",
main = "Interpolated 0.5 ms bandwidth")
lines(snippet_time, rowMeans(interp_snippets), col = 'red')
abline(v = max(estimated_duration) * 1000, lty = 2, col = "gray40")
abline(v = max(estimated_duration) * 1000 + 0.5, lty = 2)
Run the code above in your browser using DataLab