function K = estimate_K_gmm( mvn_model )
  
% function K = estimate_K_of_mvn_model( mvn_model )
%
% Estimate the number of free parameters of a GMM
% Each component can be full or diagonal.
%
% See also: do_em_gmm.m, compute_post_gmm.m.
%
% By Guillaume Lathoud, 2006.
% lathoud@idiap.ch
  
  if nargin < 1
    error( [ mfilename ' needs 1 input argument.' ] );
  end
  
  % An easy case
  if isempty( mvn_model )
    K = 0;
    return
  end
  
  % Number of components of the GMM
  
  M = numel( mvn_model );
  
  % Dimensionality
  
  D = numel( mvn_model( 1 ).mu );
  
  
  % Start adding the number of free parameters
  
  K = 0;
  
  % Free weight parameters
  
  K = K + ( M > 1 ) * M;
  
  % Free mean vector parameters
  
  K = K + M * D;
  
  % Free diagonal covariance matrix parameters
  
  is_diag = [ mvn_model.diag_Sigma ];
  
  K = K + sum( is_diag ) * D;
  
  % Free full covariance matrix parameters
  
  K = K + sum( ~is_diag ) * D * ( D+1 ) / 2;
  
  
