Peak decomposition

2020-08-01 19:54发布


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).


One way to approach this problem lies in: "Curve fitting by a sum of gaussians"

available at:

标签: r