% Calculates Poincaré parameters for an AAI series

% Input arguments:
% - AAI : interval vector (time sequence)
% - a   : if a > 0, displays a plot with the cloud of points (scatter plot)

% Output arguments:
% SD1  - transverse standard deviation (short-term variability)
% SD2  - longitudinal standard deviation (long-term variability)
% Area - area of the Poincaré ellipse (pi * SD1 * SD2)

function [SD1,SD2,Area] = FucPoincare(IEE,a)
  h=length(IEE);
  x=IEE(1:end-1);
  y=IEE(2:end);
  s1xy=0;
  s2xy=0;
 % Calculo SD1 & SD2 y Area
  % Bucle acumula sumas para varianzas necesarias de SD1 y SD2
 for i=1:h-1
     s1ref=(x(i)-y(i)).^2;
     s1xy=s1xy+s1ref;
     s2ref=((x(i)+y(i)-(2*mean(IEE)))).^2;
     s2xy=s2xy+s2ref;
 end
 SD1=sqrt((1/(2*h))*s1xy);
 SD2=sqrt((1/(2*h))*s2xy);
 Area=pi*SD1*SD2;
  % Mostrar diagrama de dispersión si se solicita
 if a>0
     plot(x,y,'*')
 end
end