Skip to content

Instantly share code, notes, and snippets.

@sgibb
Created October 18, 2012 19:33
Show Gist options
  • Save sgibb/3914291 to your computer and use it in GitHub Desktop.
Save sgibb/3914291 to your computer and use it in GitHub Desktop.
Calculate FWHM.
fwhm <- function(spectrum, peaks) {
## work horse
.fwhm <- function(spectrum, i) {
n <- length(spectrum)
left <- ifelse(i <= 1, 1, i)
right <- ifelse(i >= n, n, i)
hm <- spectrum@intensity[i]/2
while (left > 1 && spectrum@intensity[left] > hm) {
left <- left-1
}
while (right < n && spectrum@intensity[right] > hm) {
right <- right+1
}
## interpolate x values
xleft <- approx(x=spectrum@intensity[left:(left+1)],
y=spectrum@mass[left:(left+1)], xout=hm)$y
xright <- approx(x=spectrum@intensity[(right-1):right],
y=spectrum@mass[(right-1):right], xout=hm)$y
return(abs(xleft-xright))
}
return(unlist(lapply(which(spectrum@mass %in% peaks@mass), .fwhm, spectrum=spectrum)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment