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
  Returning values from Gaussian fitting Laplace12 0 146 Aug-05-2021, 08:09 AM
Last Post: Laplace12
  Find factor to match test curve to golden curve SriRajesh 0 274 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 618 Dec-21-2020, 03:24 PM
Last Post: Gribouillis
  search of a curve fitting function bluffy5 2 923 Dec-13-2020, 09:53 AM
Last Post: ndc85430
  Rotate 2D Gaussian given parameters schniefen 4 676 Dec-11-2020, 03:34 PM
Last Post: schniefen
  saving data from text file to CSV file in python having delimiter as space K11 1 680 Sep-11-2020, 06:28 AM
Last Post: bowlofred
  Multi-gaussian function Laplace12 5 948 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 767 Jun-26-2020, 11:59 AM
Last Post: Mangesh121
  Process Data from one csv file and write to another CSV file specific column ajin9581 1 821 Jun-17-2020, 06:00 PM
Last Post: buran
  Scipy kolmogorov smirnov test for evaluating the fitting of a non-normal distribution mcva 0 719 May-26-2020, 12:01 PM
Last Post: mcva

Forum Jump:

User Panel Messages

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