function tsmatC= smoothcov(lmat, length)

%computes the covariance using the exponential
%moving average: mat is the matrix ordered from the
%most recent values on top to the oldest values ont the bottom
%of the file

% if ischar(mat)
%     mat= DLMREAD(mat, ';')
% end
% 
% lmat= diff(log(mat));
% lmat= flipud(lmat)

n=0;
i=1;

for col1= lmat;
    n=n+1;
    m=0;
    % moving norm
    NCol1 = col1;
    NCol1(find(isnan(NCol1))) = [];
    NCol1 =NCol1.^2;  % norme 2
    NCol1=smoothts(NCol1', 'e', 1-2^(-1/length))';
    %NCol1=smoothts(NCol1', 'b', length)';
    NCol1 = NCol1.^0.5;
    for col2= lmat(1:end, 1:n);
        plot(col1,col2);
        % moving norm
        NCol2 = col2;
        NCol2(find(isnan(NCol2))) = [];
        NCol2 =NCol2.^2;  % norme 2
        NCol2=smoothts(NCol2', 'e', 1-2^(-1/length))';
        %NCol2=smoothts(NCol2', 'b', length)';
        NCol2 = NCol2.^0.5;
        Ncol2 = NCol1.*NCol2;
        m=m+1;
        prod= col1 .* col2;
        prod(find(isnan(prod))) = [];
        prod=smoothts(prod', 'e', 1-2^(-1/length))';
        %prod=smoothts(prod', 'b', length)';
        tsCorr = prod ./ Ncol2;  % time series of correlations
        tsCorr(1,:) = [];
        aa =corr2(col1,col2);
        tsCorr= [tsCorr;aa];
        tsmatC(:,i) = tsCorr;
        i=i+1;
        s=size(prod);
        if not (s(1)==0)
            A(n,m)=tsCorr(end); % covariances
            A(m,n)=tsCorr(end);
        end
    end 
end
