clear;

filename='AAMWPgfz0.050-year.dat';

fin=fopen(filename,'rt');
fgetl(fin);
A=fscanf(fin,'%f',[3 inf]);% A - array of data
fclose(fin);

l=size(A);
N=l(2);

YEARS=A(1,1:N);
X=A(2,1:N);
Y=A(3,1:N);

dT=YEARS(2)-YEARS(1);

plot(YEARS,X)


%polynomial trend
deg=2;
p_coef=polyfit(YEARS,X,deg);
model_poly = polyval(p_coef,YEARS);
 
plot(YEARS,X,YEARS,model_poly)

% Autocovariance function
signal=X-mean(X);

signal = X-model_poly;

[cf,lags]=xcorr(signal,signal);

DAT=dT*lags;


plot(DAT, cf)
%spectrum dencity based on ACF
 [spectr, omega] = ampl_fft(cf,dT)
 
 plot(1./(omega/2/pi),abs(spectr))
 %periods   amplitudes
 