Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: 2D gaussian fitting using Surface Fitting Tool
Replies: 11   Last Post: Dec 8, 2016 8:27 AM

 Messages: [ Previous | Next ]
 Tim Balmer Posts: 3 Registered: 6/8/10
Re: 2D gaussian fitting using Surface Fitting Tool
Posted: Jun 15, 2010 9:05 AM

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
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

Date Subject Author
6/8/10 Tim Balmer
6/8/10 Tim Balmer
6/14/10 Shu-huai ZHANG
6/15/10 Tim Balmer
6/16/10 Shu-huai ZHANG
8/9/10 pasino pasin
4/3/12 Mat
12/8/16 PARTHA PRATIM DEKA
3/12/12 Zoltan
10/14/13 Maja
10/22/13 cpraj
1/20/14 jamin2