clear;
clc;
%path to the file to analyze
%cd('c:\');

[fname,pname]=uigetfile('*.*','source stream file');
streamFile=fullfile(pname,fname);
fid=fopen(streamFile,'r');

%h: window size
%N: number of tuples to analyze
h=1000;
N=75000000;
[S,count]=fscanf(fid,'%d',N);
sums=0;
for i=1:N
    sums=sums+S(i);
end
Y=sums/N;


for i=1:h
    for j=1:h
        indexL=h+1-i;
        indexH=N-i;
        sum1=0;
        sum2=0;
        for t=indexL:indexH
            sum1=sum1+S(t)*S(t+i-j);
            sum2=sum2+S(t);
        end
        A(i,j)=sum1;
    end
    A(i,h+1)=Y*sum2;
end

for j=1:h
    indexL=h+1-j;
    indexH=N-j;
    sum1=0;
    for t=indexL:indexH
        sum1=sum1+S(t);
    end
    A(h+1,j)=sum1;
end

%for j=1:h
%    A(h+1,j)=1;
%end    
%A(h+1,h+1)=1;

A(h+1,h+1)=Y*(N-h);

for i=1:h
    sum=0;
    indexL=h+1-i;
    indexH=N-i;
    for t=indexL:indexH
        sum=sum+S(t)*S(t+i);
    end
    B(i)=sum;
end

sum=0;
for t=h+1:N
    sum=sum+S(t);
end
B(h+1)=sum;

B=B';
%da=det(A);
AA=inv(A);
X=AA*B;

suma=0;
for i=1:h
    suma=suma+X(i);
end

b=X(h+1);

%sumexp=0;
%for i=1:h
%    sumexp=sumexp+X(i)*S(N+1-i);
%end


%sumexp=sumexp+X(i+1)*Y;

