# Function to pick peaks:
pp <- function (x, span) {
# Borrowed from ChemometricsWithR by R. Wehrens
span.width <- span * 2 + 1
loc.max <- span.width + 1 - apply(embed(x, span.width), 1,
which.max)
loc.max[loc.max == 1 | loc.max == span.width] <- NA
pks <- loc.max + 0:(length(loc.max) - 1)
unique(pks[!is.na(pks)])
}
# Some test data:
set.seed(99)
vec <- abs(rnorm(100))
vec[12] <- 10
vec[75] <- 31 # create two larger peaks
# Now demonstrate:
span <- 20
ppVec <- pp(vec, span)
for (i in 1:length(ppVec)) {
tmp <- calcSN(vec, span, ppVec[i])
print(tmp) # SN of each peak found reported
}
plot(vec, type = "l")
abline(v = ppVec, col = "red") # Shows peaks identified
# Try repeating with span = 10
Run the code above in your browser using DataLab