Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

2D gaussian fitting using Surface Fitting Tool

1,183 views
Skip to first unread message

Tim Balmer

unread,
Jun 8, 2010, 1:33:19 AM6/8/10
to
I'm trying to fit a 2D gaussian to a surface using the Surface Fitting Tool. I choose the custom equation option and enter this formula: A*exp(-x^2/(2*sigmax^2)-y^2/(2*sigmay^2))

I think that either my equation is wrong or I am not using the Surface Fitting Tool properly because it generates a flat surface that doesn't fit at all and has a negative R-square value.

Any help would be greatly appreciated!

Thanks!

Tim Balmer

unread,
Jun 8, 2010, 8:18:04 PM6/8/10
to
Alright, I figured it out. Here is my solution. First, I changed the custom equation to this: A*exp(-(x-x0)^2/(2*sigmax^2)-(y-y0)^2/(2*sigmay^2)) It was also necessary to change the 'Fit Options'. I changed the StartPoint of all the Unknows to 10.

Now it works great. I hope this helps someone out there who is trying to fit 2D gaussians to their data using the Surface Fitting Tool.


"Tim Balmer" <tbal...@student.gsu.edu> wrote in message <hukkmu$pcd$1...@fred.mathworks.com>...

Shu-huai ZHANG

unread,
Jun 14, 2010, 10:56:04 PM6/14/10
to
Hi, Tim Balmer:

I am a newcomer about MATLAB. Now want to do the same thing like you.
Could you give me some help?

Thanks a lot.

"Tim Balmer" <tbal...@student.gsu.edu> wrote in message <hukkmu$pcd$1...@fred.mathworks.com>...

Tim Balmer

unread,
Jun 15, 2010, 9:05:20 AM6/15/10
to
Hi Shu-huai,
I'm pretty new to Matlab myslef, and I have never had any formal instruction, so I'll explain how I have fit 2D gaussians to my own data and hopefully it will help you figure out how to use it for your application.

-First you need to put your data into x, y and z variables. For instance, my z data is in a 13x19 matrix that looks like this:
z=[0 0 0 0 0 1 0 1 0 1 0 0 0
0 0 0 1 1 0 0 0 0 1 1 0 0
0 0 1 0 0 2 1 1 0 0 1 0 1
0 0 0 0 0 0 1 1 1 0 1 1 0
0 0 0 0 0 1 2 2 3 5 0 0 0
0 1 2 4 1 5 13 4 3 9 0 2 0
1 1 8 10 10 14 22 23 9 10 0 0 0
5 4 6 15 21 21 24 24 17 16 0 1 0
4 9 12 24 20 28 34 30 13 19 0 1 0
2 5 13 17 31 31 35 39 20 18 0 0 0
2 6 13 22 28 34 46 35 13 20 0 1 1
5 7 12 14 31 27 39 27 14 12 0 1 0
6 6 10 17 18 32 28 27 17 6 1 0 1
3 4 7 10 15 18 17 11 11 11 2 0 1
1 1 7 8 12 14 8 9 4 2 1 0 1
1 1 1 3 3 6 5 5 3 0 0 0 1
1 1 0 3 1 0 1 1 0 1 0 3 0
1 1 0 0 0 1 0 0 1 2 0 0 1
0 1 1 0 0 0 0 1 1 0 0 0 2]

-to make this 13x19 matrix into x, y, z variables that can be used with the surface fitting tool, do this:
[x,y]=meshgrid(1:13,1:19);
x=x(:);
y=y(:);
z=z(:);

-now call the surface fitting tool:
sftool

-choose x, y and z for the inputs

-choose custom equation from the drop down menu and paste in this equation:
a1*exp(-(x-x0)^2/(2*sigmax^2)-(y-y0)^2/(2*sigmay^2))

-click 'Fit'

-if the equation doesn't fit at all, change the fit options:
change all of the StartPoints to 10

--after doing this you should get these results:
General model:
f(x,y) = a1*exp(-(x-x0)^2/(2*sigmax^2)-(y-y0)^2/(2*sigmay^2))
Coefficients (with 95% confidence bounds):
a1 = 39.23 (37.56, 40.9)
sigmax = 2.332 (2.232, 2.431)
sigmay = 2.725 (2.61, 2.841)
x0 = 6.585 (6.486, 6.684)
y0 = 10.64 (10.52, 10.75)

Goodness of fit:
SSE: 1723
R-square: 0.924
Adjusted R-square: 0.9227
RMSE: 2.668


--To make this much faster you can choose File/Generate M-File and it will make a function called createSurfaceFit, that can be run with any data that is named x, y and z and is in the proper format. That way you won't have to enter the sftool each time.

Hope this helps!
Tim

Shu-huai ZHANG

unread,
Jun 16, 2010, 9:34:04 PM6/16/10
to
Hi,Tim

Thanks so much. I will try it.

shu-huai ZHANG

"Tim Balmer" <tbal...@student.gsu.edu> wrote in message <hv7tqg$hcm$1...@fred.mathworks.com>...

pasino pasin

unread,
Aug 9, 2010, 4:54:04 PM8/9/10
to
I have similar problem but I need to fit multipeak Gaussians. I have a 2D data, xy plane is my pixels and z axis is the values corresponding to each pixel(like an image).
I want to model my z data with Gaussians,but not only one Gaussian, it needs to detect peaks, cluster them and fit Gaussians
Is there such a package of Matlab or any other way, code etc.?


"Shu-huai ZHANG" <zh...@cc.hirosaki-u.ac.jp> wrote in message <hvbu2c$aqf$1...@fred.mathworks.com>...

Zoltan

unread,
Mar 12, 2012, 12:13:11 PM3/12/12
to
Hi Tim
Thanks for your post. It is working great with my own data too. Unfortunatelly, I don't know why, but it happens very often, that the fit didn't work due to an error message: "CAT arguments dimensions are not consistent." . It is very strange, that I always used the same data, without any modification, and sometimes it worked, and more often it didn't. It is frustrating, that I don't know why.
best regards
Zoltan

"Tim Balmer" <tbal...@student.gsu.edu> wrote in message <hv7tqg$hcm$1...@fred.mathworks.com>...

Mat

unread,
Apr 3, 2012, 10:10:16 AM4/3/12
to
"Shu-huai ZHANG" <zh...@cc.hirosaki-u.ac.jp> wrote in message <hvbu2c$aqf$1...@fred.mathworks.com>...
Thanks Tim for this solution. It worked for me too, what a surprise. But I'd like to disable the datapoints in the sftool environment. How can I do that? Bcs of the high amount of datapoints, the fit is barely visible through all the datapoints.

greetings

Maja

unread,
Oct 14, 2013, 5:13:06 PM10/14/13
to
Great help, Thanks!!

cpraj

unread,
Oct 22, 2013, 10:42:47 PM10/22/13
to
Hi Tim,

Thanks for simplifying the steps.

By the way- "how to call sftool"?

jamin2

unread,
Jan 20, 2014, 11:10:13 AM1/20/14
to
Just type "sftool" in the command window

PARTHA PRATIM DEKA

unread,
Dec 8, 2016, 8:27:07 AM12/8/16
to
What is 'z' here? Is it a function of x and y? What I want to know is that the rows and the columns of the 2D matrix are defined by x and y and the values of the matrix elements are given by z, is it correct? Please help as soon as possible. Thanks!


"Mat" wrote in message <jlf0c8$f14$1...@newscl01ah.mathworks.com>...
0 new messages