-
Notifications
You must be signed in to change notification settings - Fork 1
/
Analysis_Functions.R
82 lines (63 loc) · 1.36 KB
/
Analysis_Functions.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# This file contains custom functions for peak detection and calculation of electrophysiological params
# Custom peak detection function
peakdet <- function(v, delta, x = NULL)
{
maxtab <- NULL
mintab <- NULL
if (is.null(x))
{
x <- seq_along(v)
}
if (length(v) != length(x))
{
stop("Input vectors v and x must have the same length")
}
if (!is.numeric(delta))
{
stop("Input argument delta must be numeric")
}
if (delta <= 0)
{
stop("Input argument delta must be positive")
}
mn <- Inf
mx <- -Inf
mnpos <- NA
mxpos <- NA
lookformax <- TRUE
for(i in seq_along(v))
{
this <- v[i]
if (this > mx)
{
mx <- this
mxpos <- x[i]
}
if (this < mn)
{
mn <- this
mnpos <- x[i]
}
if (lookformax)
{
if (this < mx - delta)
{
maxtab <- rbind(maxtab, data.frame(pos = mxpos, val = mx))
mn <- this
mnpos <- x[i]
lookformax <- FALSE
}
}
else
{
if (this > mn + delta)
{
mintab <- rbind(mintab, data.frame(pos = mnpos, val = mn))
mx <- this
mxpos <- x[i]
lookformax <- TRUE
}
}
}
list(maxtab = maxtab, mintab = mintab)
}