简体   繁体   English

将2D高斯拟合到2D Data Matlab

[英]Fitting a 2D Gaussian to 2D Data Matlab

I have a vector of x and y coordinates drawn from two separate unknown Gaussian distributions. 我有一个从两个单独的未知高斯分布中得出的xy坐标向量。 I would like to fit these points to a three dimensional Gauss function and evaluate this function at any x and y . 我想将这些点拟合为三维高斯函数,并在任何xy处评估此函数。

So far the only manner I've found of doing this is using a Gaussian Mixture model with a maximum of 1 component (see code below) and going into the handle of ezcontour to take the X, Y, and Z data out. 到目前为止,我发现这样做的唯一方法是使用具有最多1个分量的高斯混合模型(请参见下面的代码),并进入ezcontour的句柄以获取X,Y和Z数据。

The problems with this method is firstly that its a very ugly roundabout manner of getting this done and secondly the ezcontour command only gives me a grid of 60x60 but I need a much higher resolution. 这种方法的问题是,首先,它以一种非常丑陋的回旋方式完成该任务,其次,ezcontour命令仅给我60x60的网格,但是我需要更高的分辨率。

Does anyone know a more elegant and useful method that will allow me to find the underlying Gauss function and extract its value at any x and y ? 有谁知道一种更优雅,更有用的方法,该方法将使我能够找到潜在的高斯函数并提取任意xy值?

Code: 码:

GaussDistribution = fitgmdist([varX varY],1); %Not exactly the intention of fitgmdist, but it gets the job done.
h = ezcontour(@(x,y)pdf(GaussDistributions,[x y]),[-500 -400], [-40 40]);

Gaussian Distribution in general form is like this: 一般形式的高斯分布是这样的:

I am not allowed to upload picture but the Formula of gaussian is: 我不允许上传图片,但高斯公式为:

1/((2*pi)^(D/2)*sqrt(det(Sigma)))*exp(-1/2*(x-Mu)*Sigma^-1*(x-Mu)');

where D is the data dimension (for you is 2); 其中D是数据维度(您的数据为2); Sigma is covariance matrix; Sigma是协方差矩阵; and Mu is mean of each data vector. Mu是每个数据向量的均值。

here is an example. 这是一个例子。 In this example a guassian is fitted into two vectors of randomly generated samples from normal distributions with parameters N1(4,7) and N2(-2,4): 在此示例中,将一个高斯拟合为两个从正态分布中随机生成的,具有参数N1(4,7)和N2(-2,4)的样本的向量:

Data = [random('norm',4,7,30,1),random('norm',-2,4,30,1)];
X = -25:.2:25;
Y = -25:.2:25;

D = length(Data(1,:));
Mu = mean(Data);
Sigma = cov(Data);
P_Gaussian = zeros(length(X),length(Y));
for i=1:length(X)
   for j=1:length(Y)
       x = [X(i),Y(j)];
       P_Gaussian(i,j) = 1/((2*pi)^(D/2)*sqrt(det(Sigma)))...
                    *exp(-1/2*(x-Mu)*Sigma^-1*(x-Mu)');
   end
end

mesh(P_Gaussian)

run the code in matlab. 在matlab中运行代码。 For the sake of clarity I wrote the code like this it can be written more more efficient from programming point of view. 为了清楚起见,我这样编写代码,从编程角度来看,它可以更高效地编写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM