
Re: 2D gaussian fitting using Surface Fitting Tool
Posted:
Jun 15, 2010 9:05 AM


Hi Shuhuai, 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((xx0)^2/(2*sigmax^2)(yy0)^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((xx0)^2/(2*sigmax^2)(yy0)^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 Rsquare: 0.924 Adjusted Rsquare: 0.9227 RMSE: 2.668
To make this much faster you can choose File/Generate MFile 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

