%% calcula as dist�ncias com o coeficiente de correla��o
clc
clear all
close all

nome = 'concrete200mm500mms0deg.tif';
A=imread(nome);

A=double(A);

distance = zeros(1, size(A,1));
coef = zeros(1, size(A,1));

index=1;
for line = 1:size(A,1)-1
    S1=A(line, 1:size(A,2));
    S2=A(line+1, 1:size(A,2));
    
    S1=S1-mean(S1);
    S2=S2-mean(S2);
    
    maxlag = 200;
    S2(1:maxlag) = 0;
    S2(length(S2)-maxlag:length(S2)) = 0;

    [c1,lags] = xcorr(S1,S2,maxlag,'coeff');

    [value, i] = max(c1);
    coef(index) = value;
%     distance(index) = lags(i)*72/1899;
    distance(index) = lags(i)*36/1455;
    index=index+1;
end

dx=distance; % mm
dt=0.0004; % segundos

v=dx'/dt;

t=zeros(size(v,1),1);
inc=0;
for i=1:size(v,1)
    t(i)=inc;
    inc=inc+dt;
end

figure
hold on

plot(t',v,'b')
xlabel('Time (s)')
ylabel('Velocity (mm/s)')