gusucode.com > 用粒子滤波算法进行跟踪的matlab代码 > gmm_utilities/gmm_to_gaussian.m

    function [x,P,wn] = gmm_to_gaussian(g)
% Convert gmm to a single Gaussian (ie, compute first two moments of mixture). 

[g, wn] = gmm_normalise(g); % gmm must be normalised for weighted sums to be meaningful

[D,N] = size(g.x);
w = reprow(g.w, D);
x = sum(w.*g.x, 2);

P = zeros(D);
for i=1:N
    P = P + g.w(i) * g.P(:,:,i);
end
xerr = g.x - repcol(x, N);
P = P + w.*xerr*xerr'; 

return 


% TODO: implement faster version: ... 
[D,N] = size(g.x);

w = reprow(g.w, D);
x = sum(w.*g.x, 2);

xerr = g.x - repcol(x, N);
P = reshape(g.P, [N,D*D,1]);
Pw = sum(w.*P, 1);
Pw = reshape(Pw, [D,D]);
%Pw = reshape(sum(w.*reshape(g.P, [N,D*D,1]), 1), [D,D]); % reshape permits summing over P values
P = Pw + w.*xerr*xerr'; 

% g.P is DxDxN
% g.w is N
% w is DxN