function [r, p] = corr_auto(a,b)

% CORR_AUTO:
%   This function calculate correlation and significance of two time
%   series, considering autocorrelation of themselves.

r = corrcoef(a,b);
r = r(1,2);

%  student t-test
ra = autocorr(a,1); ra = ra(2);
rb = autocorr(b,1); rb = rb(2);
nu = (length(a)-2) * (1-ra*rb)/(1+ra*rb);
t = nu^0.5 * r/(1-r^2)^0.5;

if t>0
    p = 1-cdf('T',t,nu);
else 
    p = cdf('T',t,nu);
end

end