% author :Repola Francesco , student of electronic engineering for automation and telecomunications
% at University of Sannio (ITALY)
% This function compute the signals' rms value , let see rms function in matlab documentation
function root_main_squared = rms ( x, DIM )
  if (~exist ('DIM') ) % this function can be used also in this way : rms(x)
    DIM = 1 ;
  endif
  if ( ~isnumeric (DIM) )
    error ('DIM must be a number ');
  endif
  columns_number = length (x (1, : ));
  rows_number = length (x (:, 1) );
  if ( DIM > max ( columns_number, rows_number ) )
    root_main_squared = x;
    return;
  endif
  if ( length (x (:, 1) ) > 1 &&  DIM <= 1 )
      for ( j = 1 : columns_number )
        int_squared_module = 0;
        for ( i = 1 : rows_number )
          int_squared_module  = int_squared_module + x ( i, j) * conj ( x ( i, j ) );
        endfor
        root_main_squared ( 1, j ) = sqrt ( int_squared_module / rows_number );
    endfor
    return  ;
  endif
 	for ( j = 1 : rows_number )
    int_squared_module = 0;
    for ( i = 1 : columns_number )
      int_squared_module  = int_squared_module + x ( j, i) * conj ( x ( j, i ) );
    endfor
    root_main_squared ( j, 1 ) = sqrt ( int_squared_module / rows_number );
  endfor
  return  ; 
end
