function  [test] = corrtest(corr,n)

% test of statistical significance of correlation coefficient
% input: corr ... coeficient of cross-correlation (vector)
%           n ... length of time series (number of observations, withot nan)
% output: test [tstat ... t-statistics
%               tcrit ... critical value (significance level 5 %)
%               answ] ... if tstat > tcrit => 1 (corr. coef is statisticaly significant) 
%                                 otherwise   0 (corr. coef is NOT statisticaly significant)
                         
nn = length(corr);
center = ceil(length(corr)/2);
alpha = 0.05;
tstat = zeros([nn,1]);

for i = 1:nn
k = abs(center-i);    
tstat(i,1) = corr(i) / ((1-(corr(i))^2)^(0.5)) *(n-2-k)^0.5;
tcrit(i,1) = tinv(1-alpha/2,n-2-k);
end

answ = abs(tstat)>tcrit;
test = [tstat, tcrit answ];

return




