Peak decomposition

Posted by midtiby on Stack Overflow See other posts from Stack Overflow or by midtiby
Published on 2010-05-16T19:52:00Z Indexed on 2010/05/16 20:00 UTC
Read the original article Hit count: 297

Filed under:

Hi

I want to examine a NMR spectre and make the best fit of a specific peak using a sum of gaussians. With the following code it is possible to fit two gaussians to the peak, but can it easily be generalized to n gaussians?

freq <- seq(100, 200, 0.1)
signal <- 3.5*exp(-(freq-130)^2/50) + 0.2 + 1.5*exp(-(freq-120)^2/10)
simsignal <- rpois(length(signal), 100*signal) + rnorm(length(signal))
plot(freq, simsignal)
res <- nls(simsignal ~ bg + h1 * exp(-((freq - m1)/s1)^2) + h2 * exp(-((freq - m2)/s2)^2),
start=c(bg = 4, h1 = 300, m1 = 128, s1 = 6, h2 = 200, m2 = 122, s2 = 4), trace=T)
lines(freq, predict(res, freq), col='red')

Another wish is a visulization of the contribution from each of the gaussians to the original peak, eg. the gaussians should be plotted side by side (instead of plotting their sum as done above).

© Stack Overflow or respective owner

Related posts about r