-
Notifications
You must be signed in to change notification settings - Fork 1
/
methods.R
89 lines (72 loc) · 2.49 KB
/
methods.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
83
84
85
86
87
88
89
## ------------------------------------------------------------------------- ##
## R6 CLASS METHODS ##
## ------------------------------------------------------------------------- ##
## Methods for objects of class ESR.Spectrum
## -----------------------------------------
get_diff <- function(x, order, ...) {
d <- x$y
for (i in seq_len(order)) {
d <- diff(d, ...)
}
until <- nrow(x) - order
c <- attributes(x)$spectrum
x <- x[1:until, ] # TODO: This causes x to loose its attributes
if (!is.null(c)) attr(x, "spectrum") <- c
x$y <- d
class(x) <- c("ESR.Spectrum", class(x))
attr(x, "spectrum") <- c(attr(x, "spectrum"), rep("diff", order))
return(x)
}
get_integral <- function(x) {
t <- x$y
for (i in seq_along(t))
t[i] <- sum(x$y[1:i])
x$y <- t
class(x) <- c("ESR.Spectrum", class(x))
attr(x, "spectrum") <- c(attr(x, "spectrum"), "integral")
return(x)
}
get_spline <- function(x, ...) {
s <- smooth.spline(x, ...)
x$x <- s$x
x$y <- s$y
class(x) <- c("ESR.Spectrum", class(x))
attr(x, "spectrum") <- c(attr(x, "spectrum"), "spline")
return(x)
}
get_gvalues <- function(v, H, x) {
if (grepl("e+", v)) {
v <- format(v, scientific = FALSE)
}
if (v >= 10)
v <- as.numeric(paste0(strtrim(v, 1), ".", substr(v, 2, nchar(v))))
planck_const <- 6.62606957e-34 # SI: J/s
bohr_magneton <- 9.27400968e-24 # SI: J/T
g <- (planck_const * v * 10^9) / (bohr_magneton * 10^-4 * H)
if (length(x$x) != length(g)) {
t <- abs(length(x$x) - length(g))
ifelse(length(x$x) > length(g), x <- x[1:c(nrow(x)-t)], g <- g[1:c(length(g)-1)]) # TODO: this causes x to loose its attributes
}
x$x <- g
# class(x) <- c("ESR.Spectrum", class(x))
return(x)
}
get_peaks <- function(x, interval, th = 10) {
p <- find_Peaks(x, interval, th)
return(p)
}
align_spectrum <- function(x, reference, ...) {
if (inherits(reference, "list")) {
if (all(sapply(reference, function(x) inherits(x, "ESR.Spectrum"))))
reference <- lapply(reference, as.data.frame)
reference <- data.frame(x = reference[[1]][,1],
y = colMeans(do.call(rbind, lapply(reference, function(x) x[,2]))))
}
if (inherits(reference, "ESR.Spectrum"))
reference <- as.data.frame(reference)
if (inherits(x, "ESR.Spectrum"))
x <- as.data.frame(x)
lag <- abs_max_ccf(unlist(reference[ ,2]), unlist(x[ ,2]), ...)
x[ ,1] <- x[ ,1] + diff(range(x[,1])) / nrow(x) * lag
return(x)
}