Attempting a double Gaussian for the data below

68 views Asked by At

Blue curve is raw data, green curve is the Gaussian fit for only the second maximum and yellow curve is the combination of Gaussian for both maxima. Context: yraw is raw data in a list, xpixel is the x axis (500-1250)

Here is my Gaussian method:

def Gauss(x, amp, mu, sigma): 
    return amp*np.exp(-((x-mu)/(np.sqrt(2)*sigma))**2)

def bimode(x, amp1, mu1, sigma1, amp2, mu2, sigma2):
    return Gauss(x, amp1, mu1, sigma1) + Gauss(x, amp2, mu2, sigma2)


ypg1 = np.asarray(yraw) #make np array from raw data list



parameters, covarience = curve_fit(bimode, xpixel, ypg1, p0 = (78, 805, 15, 8, 950, 15)) #does a curve fit for our gauss function. we give it an initial guess of amplitude, mean value (95,000)
#parameters returns amp, mu, sig, amp, mu, sig
print(parameters)


ygauss = bimode(xpixel, parameters[0], parameters[1], parameters[2], parameters[3], parameters[4], parameters[5])

I am expecting a nice curve fit from double Gaussian to envelope both maxima. enter image description here

1

There are 1 answers

0
jlandercy On

Let's create some dataset:

import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize, stats
from pybaselines import Baseline, utils

p0 = (78, 810, 20, 8, 940, 20)

np.random.seed(12345)

x = np.linspace(500, 1250, 300)
y = model(x, *p0)
n = stats.gamma.rvs(a=1, size=y.size)
baseline = peak(x, 10, 850, 100)
yn = y + n + baseline

Now remove the baseline:

fitter = Baseline(x)
background = fitter.noise_median(yn)[0]

ynb = yn - background

And fit:

popt, pcov = optimize.curve_fit(
    model, x, ynb, p0=(100, 800, 10, 10, 950, 10)
)

Both peak can be found:

enter image description here