4.46154

4.5 | 14 ratings Rate this file 288 Downloads (last 30 days) File Size: 1.71 KB File ID: #11733

gaussian curve fit

by Yohanan Sivan

 

19 Jul 2006 (Updated 19 Jul 2006)

gaussian curve fit

| Watch this File

File Information
Description

[sigma,mu,A]=mygaussfit(x,y)
[sigma,mu,A]=mygaussfit(x,y,h)

this function is doing fit to the function
y=A * exp( -(x-mu)^2 / (2*sigma^2) )

the fitting is been done by a polyfit
the lan of the data.

h is the threshold which is the fraction
from the maximum y height that the data
is been taken from.
h should be a number between 0-1.
if h have not been taken it is set to be 0.2
as default.

Acknowledgements
This submission has inspired the following:
Slant Edge Script
MATLAB release MATLAB 7.2 (R2006a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (22)
11 Jan 2007 n gg

working fine. how can i rewrite it for a lorentzian function of the form,

y=y0+(2*A/pi)*(w./(4*(x-x0).^2+w.^2))

21 Mar 2007 m m

works as advertised

22 Jun 2007 Gustavo P

Very smart idea

24 Jul 2007 ll ss

very helpful but I don't think you should use logx for calculation and you should just do the polyfit with ylog and x.

25 Sep 2007 Marinna M

Thanks, this was a great help.

16 Nov 2007 Bob Marley

The code could be written in a more efficient manner -- i.e., using matlab syntax instead of the 'for' loop. Something like:

%% threshold
if nargin==2, h=0.2; end

%% cutting
indx = (y > max(y)*h);

%% fitting
p=polyfit(x(indx), log(y(indx)), 2);
sigma=sqrt(-1/(2*p(1)));
mu=p(2)*sigma^2;
A=exp(p(3)+mu^2/(2*sigma^2));

12 Dec 2007 Dinh Vo  
09 Jul 2008 Marie-Eve Gagne

Thank you very much!
This is exactly what I was looking for!

29 Jul 2008 David last name

great, thanks!

05 Aug 2008 Changlong Jin

there is a problem,
When I fit a data, for example,
y = [0.0651 0.0548 0.0461 0.0686 0.1268 0.2266 0.2292 0.1187 0.0299 0.0146 0.0092 0.0048 0.0032 0.0024];
it gives out a result like this:
yout = [0.0470 0.0594 0.0743 0.0918 0.1120 0.1352 0.1611 0.1897 0.2208 0.2538 0.2884 0.3238 0.3592 0.3937];
it is not a good fitting, how to solve this problem?

05 Aug 2008 Changlong Jin

The value of parameter h severely influence the result, in last comment, I use the default value, the fitting result is not correct, it looks more better when h = 0.1, how to solve this automatically?

08 Aug 2008 Matheca ProbStock

The formula used for a Gaussian pdf is wrong. pdf(x)=(A/sqrt(2*sigma)) * exp( -(x-mu)^2 / (2*sigma^2) )...should be used.

21 Dec 2008 an ‰Ê

thanks a lot

23 Jan 2009 Michael Jordan  
30 Sep 2009 Ryan  
30 Sep 2009 Ryan

I disagree with Matheca. The function is intended to fit a general gaussian, not necessarily a probability distribution function. The equation is correct.

However, the user should be aware that removing data points in a deterministic manner (i.e. by thresholding) definitely skews the resulting fit.

Rather than fitting to the whole series with negatives removed, try finding the largest contiguous positive subset of the original data series and fitting to that. This method won't work when the noise amplitude is greater than the distribution amplitude, but in most cases it will give you a better fit.

Even better yet: if accuracy is more important than computation speed, use fmincon with a least-squares difference cost function:

p = fmincon(@(p) sum((y-(p(1)*exp(-(x-p(2)).^2/2/p(3)^2))).^2),...
[max(y) mean(x) 1],[],[],[],[],[0 -inf 0],[2*max(y) inf inf])
A=p(1);
mu=p(2);
sigma=p(3);

01 Jan 2010 David Holz

Bob Marley is absolutely correct. The use of a for loop is unnecessary and slows the function down by ~20%.

If you are confident in your inputs you can also remove the warning lines #74-85 of polyfit to knock out 87% of the total previous computation time.

11 Feb 2010 Jan

Where do you get he formulas from? and where do you use h for? Can it also without h?

Thanks in advance

12 Feb 2010 Kiran

hello...could someone please explain to me how the approximation from polynomial back to gaussian is done in the code? why are the coefficients equated in the way that they are below :

sigma=sqrt(-1/(2*A2));
mu=A1*sigma^2;
A=exp(A0+mu^2/(2*sigma^2));

Thank you very much

05 Oct 2010 Avi

GREAT

05 Nov 2010 Muhammad khan

hello every one any one can tell me about gussian curve fitting back groung why we use this instead of other curve fitting method what is the benift of this from other curve fitting method.If any one have some good data regarding gussian curve fitting kindly inform me.
Advance thx to all

01 Jan 2012 Namra Aftab

hi...i have a sequence of 40 frames and i want to plot gaussian distribution of, say, first pixel of all 40 frames.the result does not look like a gaussian at all.what should i correct?

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
statistics Yohanan Sivan 22 Oct 2008 08:32:50
probability Yohanan Sivan 22 Oct 2008 08:32:50
gaussian curve fit Yohanan Sivan 22 Oct 2008 08:32:50
gaussian curve fit Itay 10 Feb 2010 23:46:44
gaussian curve fit Jan 11 Feb 2010 09:41:14
gaussian curve fit Mustafa 29 Jun 2010 11:39:15
probability Mustafa 29 Jun 2010 11:39:21
statistics Mustafa 29 Jun 2010 11:39:25
gaussian curve fit Maxwell 13 Jul 2010 17:20:28
gaussian curve fit Tom Phillips 04 Mar 2011 11:01:18
gaussian curve fit Edgar 27 Apr 2011 06:48:54
gaussian curve fit condor72 02 May 2011 05:25:12
gaussian curve fit Tu 17 May 2011 07:47:26
gaussian wan jj 11 Aug 2011 22:36:27
gaussian Dirk 23 Sep 2011 03:31:23
gaussian Athina 10 Oct 2011 11:56:32

Contact us at files@mathworks.com