From: pasino pasin on
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" <zhang(a)cc.hirosaki-u.ac.jp> wrote in message <hvbu2c$aqf$1(a)fred.mathworks.com>...
> Hi,Tim
>
> Thanks so much. I will try it.
>
> shu-huai ZHANG
>
> "Tim Balmer" <tbalmer2(a)student.gsu.edu> wrote in message <hv7tqg$hcm$1(a)fred.mathworks.com>...
> > 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