Python Forum
Fitting Gaussian curve to data file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fitting Gaussian curve to data file
#1
Hey,

I'm trying to build a code to fit Gaussians (1, 2 & 3) to some data to determine peak position, and though the code in itself seems to be working, the Gaussian fits all return straight lines. I've tried multiple different guessing parameters, but the result is always a straight line for all 3. If someone has an idea what the problem could be, please help! The code looks like this:

import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize

data = np.genfromtxt('file')

def gaussian(x, height, center, width, offset):
    return height*np.exp(-(x - center)**2/(2*width**2)) + offset

def two_gaussians(x, h1, c1, w1, h2, c2, w2, offset):
    return three_gaussians(x, h1, c1, w1, h2, c2, w2, 0,0,1, offset)

def three_gaussians(x, h1, c1, w1, h2, c2, w2, h3, c3, w3, offset):
    return (gaussian(x, h1, c1, w1, offset=0) +
        gaussian(x, h2, c2, w2, offset=0) +
        gaussian(x, h3, c3, w3, offset=0) + offset)

errfunc2 = lambda p, x, y: (two_gaussians(x, *p) - y)**2
errfunc3 = lambda p, x, y: (three_gaussians(x, *p) - y)**2

guess2 = [514, 900, 20, 552, 925, 20, 275]
guess3 = [514, 900, 20, 552, 925, 20, 531, 950, 20, 275]  
optim2, success = optimize.leastsq(errfunc2, guess2[:], args=(data[:,0], data[:,1]))
optim3, success = optimize.leastsq(errfunc3, guess3[:], args=(data[:,0], data[:,1]))

plt.plot(data[:,0], data[:,1], lw=1, c='b', label='sync1')
plt.plot(data[:,0], data[:,2], lw=1, c='r', label='sync2')
plt.plot(data[:,0], gaussian(data[:,1], 552, 925, 20, 275),
    lw=1, c='m', ls='--', label='Gaussian')
plt.plot(data[:,0], two_gaussians(data[:,1], *optim2),
    lw=1, c='y', ls='--', label='2 Gaussians')
plt.plot(data[:,0], three_gaussians(data[:,1], *optim3),
    lw=1, c='g', ls='--', label='3 Gaussians')
#plt.xlim([200, 2000])
#plt.ylim([0, 700])
plt.legend(loc='best')
The plot this code now gives is in attachments. Could the problem be in the error functions? Switching initial guess gives a small peak if I optimize column 2 and plot the first one, but it's still nowhere near the shape of the data since the amplitude is around 2 and two_gaussian is negative. I've fitted multi-gaussians to data before, and it should be a relatively simple prodecure, but the results are definitely not good.

Attached Files

Thumbnail(s)
   
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Learning curve astral_travel 3 395 Apr-05-2024, 06:58 PM
Last Post: jefsummers
  How can I design shapes on a curve in python? mervea 2 793 Sep-14-2023, 01:04 PM
Last Post: Pedroski55
  Fitting data to a curve daaegp 1 598 Jun-30-2023, 08:06 PM
Last Post: Gribouillis
  Returning values from Gaussian fitting Laplace12 0 1,567 Aug-05-2021, 08:09 AM
Last Post: Laplace12
  Find factor to match test curve to golden curve SriRajesh 0 1,545 Jun-17-2021, 04:39 AM
Last Post: SriRajesh
  xml file creation from an XML file template and data from an excel file naji_python 1 2,092 Dec-21-2020, 03:24 PM
Last Post: Gribouillis
  search of a curve fitting function bluffy5 2 2,410 Dec-13-2020, 09:53 AM
Last Post: ndc85430
  Rotate 2D Gaussian given parameters schniefen 4 2,887 Dec-11-2020, 03:34 PM
Last Post: schniefen
  Multi-gaussian function Laplace12 5 4,001 Jul-21-2020, 11:38 PM
Last Post: scidam
  How to save CSV file data into the Azure Data Lake Storage Gen2 table? Mangesh121 0 2,099 Jun-26-2020, 11:59 AM
Last Post: Mangesh121

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020