function the_mean_angle = mean_angle( angle_data )
  
% function the_mean_angle = mean_angle( angle_data )
%
% Estimate the mean angle of some angular data (real values in radians).
% If angle_data is a matrix, the mean angle is estimated along dimension 1.
%
% Return value: "the_mean_angle", a real scalar value in radians.
%
% By G. Lathoud - 2005

  

  if nargin < 1
    error( [ mfilename ' needs 1 input argument.' ] );
  end

  % Angles are defined modulo 2pi,
  % so taking the mean is not enough,
  % we first need to shift the data appropriately

  shift = angle( mean( exp( j * angle_data ), 1 ) );
  
  % Now compute the mean, using this shift

  the_mean_angle = -pi + shift + mean( mod( angle_data + pi - repmat( shift, size( angle_data, 1 ), 1 ), 2 * pi ), 1 );
  
  % Just out of convention, put it within [-pi +pi]

  the_mean_angle = -pi + mod( the_mean_angle + pi, 2 * pi );
  
    
  