%%Clean
clc, clear all, close all;

%%Read file
M= csvread('1.csv'); 
%M= csvread('esferovitentries.csv'); 

%%Data size
n= size(M,1);

%%Var
a0 = 0.0005;
a1 = 0.0003731;
a2 = 0.0005;
a3= 0.0005;

a4 = 1.0963;
a5 = 1.0958;
a6 = 1.0845;
a7 = 1.0809;

b0 = -0.00004;
b1 = 0.000049;
b2 = 0.0001;
b3 = 0.00001;

 b4 = 65.881;
 b5 = 69.702;
 b6 = 66.179;
 b7 = 63.568;
 
 %%Data extraction
for i=1:n
    F2(i)=M(i,4);
    F4(i)=M(i,7);
    F6(i)=M(i,1)-M(1,1);
    F7(i)=M(i,5)-M(1,5);
    F3(i)=M(i,6);
    F1(i)=M(i,2);
    F5(i)=M(i,3)-M(1,3);
    F8(i)=M(i,8)-M(1,8);
    Time_send(i)= M(i,9);
    Time_receive(i)= M(i,10);
    copx(i) = M(i,11);
    copy(i) = M(i,12);
end

%%Time

Time_send = ((Time_send - Time_send(1))/1000000);
Time_send(1,end) = Time_send(1,end-1);

%%Force calculation
Vout0 = (F1+100) * (5.0 / 1023.0);
Vout1 = F2 * (5.0 / 1023.0);
Vout2 = F3 * (5.0 / 1023.0);
Vout3 = F4 * (5.0 / 1023.0);
  
for k=1:n
 Rfsr0(k) = (6000/Vout0(k))-1200;
 Rfsr1(k) = (6000/Vout1(k))- 1200;
Rfsr2(k) = (6000/Vout2(k))- 1200;
  Rfsr3(k)= (6000/Vout3(k))- 1200;
end

for h=1:n
 F0a(h) = ((1/Rfsr0(h)) - b0) *(4.44822162825/a0);
 F1a(h) = ((1/Rfsr1(h)) - b1) *(4.44822162825/a1);
F2a(h) = ((1/Rfsr2(h)) - b2) *(4.44822162825/a2);
 F3a(h) = ((1/Rfsr3(h)) - b3) *(4.44822162825/a3); 
end

F4a = ((F5-b4)/a4)/100;
F5a = ((F6-b5)/a5)/100;
F6a = ((F7-b6)/a6)/100;
F7a = ((F8-b7)/a7)/100;
 
%plot(Time_send, F1a); hold on;

%%COPX COPY FN FHMOV
plot(Time_send, copx); hold on; plot(copx, copy); plot(Time_send, copy,'.');
fn=(F0a+F1a+F2a+F3a);
fhmov=+F4a*0.7071+F5a*0.7071+F6a*0.7071+F7a*0.7071;
%fhmov=+F4a+F5a+F6a+F7a;

%%COEF CALC
for l=1:n
 
uy(l)=fhmov(l)/fn(l);

end


%plot(Time_send, jjj)

%%Plots
plot(Time_send, fn,'.'); hold on; plot(Time_send, fn,'r')
plot(Time_send, fhmov,'.'); hold on; plot(Time_send, fhmov,'r')
plot(Time_send, uy,'.'); hold on; plot(Time_send, uy,'r');


%x=ones(1,81)*64;
%x1=ones(1,81)*70;
%x2=ones(1,81)*77.2;
%x3=ones(1,81)*82;
%x4=ones(1,81)*89.5;
%y=[-20:1:60];
%plot(x,y,'-r'); plot(x1,y,'-r'); plot(x2,y,'-r'); plot(x3,y,'-r'); plot(x4,y,'-r');

%g = nº de passos
%a= ponto de partida


%%step analysis%a=26052;
%a=29818;
%a=588;
a=1;
%%%a=3734;
%%%a=369;
%%%a=400;
%a=277;
%a=26105;
%a=587;
x=-1;
j=1;
z=0;
b=0;
 indzero=find(fn(a:end)<3 & fn(a:end)>-3);
 a1=indzero(1);
 %chao1.5, primeiro a=27360, g = 25; a+39
 %chao1.5, segundo a=29818, g= 32; a+
for g=1:1:23
   % if g==1
    %     plot(Time_send(a:a+39),fn(a:a+39));hold on;
    %     b=a+39;
   % else
        while x==-1
        x=indzero(1,j)-indzero(1,j+1);
        j=j+1
        z=z+1 %n de zeros (pontos c pé no ar)
        end
    y=indzero(1,j-1)-indzero(1,j);
    a=a1+b+z;
    c=a-y-1;
    % plot(Time_send(a+1:c),fhmov(a+1:c));
    
   % plot(Time_send(a+1:c),uy(a+1:c)); hold on %plot(Time_send(a+1:c), copx(a+1:c)); plot(Time_send(a+1:c), copy(a+1:c));
    A(1,g)=(mean(fn(a+1:c)));%fn
    B(1,g)=(mean(fhmov(a+1:c)));%fh
    C(1,g)=(mean(uy(a+1:c)));%uy
    D(1,g)=(mean(copx(a+1:c)));%copx
    E(1,g)=(mean(copy(a+1:c))); hold on;%copy
    if g ~=1
    I(1,g)=a+1;
    F(1,g)=c;
     plot(Time_send(a:c),fn(a:c));hold on;
    end
    b=c;
  %  end 
    %plot(Time_send(a:a+39),fhmov(a:a+39));hold on;
    %A(1,n)=(mean(fn(a:a+39)));
    %B(1,n)=(mean(fhmov(a:a+39)));
    z=0;
    x=-1;
    a1=0;
end


%%remover pontos individuais
bb=1;
for hhh=1:1:size(I,2)
    if F(1,hhh)-I(1,hhh) > 0 
        II(1,bb) = I(1,hhh);
        FF(1,bb) = F(1,hhh);
        bb=bb+1;    
    end
end

jg=size(FF,2)
for kh=1:1:jg
gj(kh)=FF(kh)-II(kh)
end

baba=1;
for jl=1:1:jg
    if gj(jl) == 38 || gj(jl) == 39 || gj(jl) == 40 || gj(jl) == 41
         III(1,baba) = II(1,jl);
        FFF(1,baba) = FF(1,jl);
        baba=baba+1;
    end
end

jg11=FFF-III
mini=min(jg11);

for kh=1:1:size(jg11,2)
    if(jg11(kh) == 39 )
        III(kh) = III(kh)+1;
    elseif(jg11(kh)  == 40)
        III(kh) = III(kh)+2;
    elseif(jg11(kh)  == 41)
        III(kh) = III(kh)+3;
    end
end

%indicesM = find(abs(A)< 25 );
%indicesm = find(abs(A)> 38 );
%A(indicesM) = []; B(indicesM) = []; C(indicesM) = []; D(indicesM) = []; E(indicesM) = [];
%A(indicesm)= [];  B(indicesm)= [];  C(indicesm)= [];  D(indicesm)= [];  E(indicesm)= []; 


%indicesM = find(abs(A)< mean(A)-4 );
%indicesm = find(abs(A)> mean(A)+4 );
%A(indicesM) = []; B(indicesM) = []; C(indicesM) = []; D(indicesM) = []; E(indicesM) = [];
%A(indicesm)= [];  B(indicesm)= [];  C(indicesm)= [];  D(indicesm)= [];  E(indicesm)= []; 


for xx=1:1:size(III,2)
    plot(Time_send(II(1,xx):FF(1,xx)),fn(II(1,xx):FF(1,xx)));hold on;
   % L=size(fn(I(1,xx):F(1,xx)),2);
    Y=fft(fn(III(1,xx):FFF(1,xx)));
    an(:,:,xx)=Y;
end

 crl=size(an,2)*size(an,3)
 Y(1:crl)=an(1,:,:);
 X_phase = angle(Y);

pl=size(an,3);
for pp=1:1:pl
plot(abs(an(:,:,pp))); hold on;
end
%relat=corr2(an(:,:,1),an(:,:,4))

for cc0=1:1:size(III,2)-1
    for cc1=1:1:size(III,2)-1
    relat(cc0,cc1)=corr2(abs(an(:,:,cc0)),abs(an(:,:,cc1)))
    end
end


    
    
%Y=fft(abs(fn));
L=38;
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
L=468
f = L*(1:(L))/L;
plot(f,P2) 




%%


plot(abs(fft(A)))

%%MED DEV
 [xmean, xM, xm]=stat(A(2:end)); %fn
   [xmean1, xM1, xm1]=stat(B(2:end)); %fhmov
    [xmean2, xM2, xm2]=stat(C(2:end)); %uy
     [xmean3, xM3, xm3]=stat(D(2:end)); %COPX
      [xmean4, xM4, xm4]=stat(E(2:end)); %COPY%COPY
  % med=35.6657; chao 35.6656 chao
  % dev= 0.8768; chao dev= 1.3588 chao
   %med= 31.9286; esferovite 29.7592
   %dev = 0.7953 esferovite 0.9999
   
   
%%Rule
      if xm+3*xd > A(2:end) & xm-3*xd < A(2:end)
        S='está dentro';
        disp(S);
      else
          S='está fora';
          disp(S);
      end
      
      lenght= size(A,2);
      ii=2;
      while ii< lenght
         % plot(Time_send(I(1,ii):F(1,ii)),fn(I(1,ii):F(1,ii))); hold on;
            plot(Time_send(I(1,ii):F(1,ii)),uy(I(1,ii):F(1,ii))); hold on;
          ii=ii+1;
      end
        
      
      
%plot(Time_send(27360:27399),fn(27360:27399),'.');hold on;
%plot(Time_send(27417:27455)-1.2,fn(27417:27455),'.')

%plot(Time_send(27473:27511),fn(27473:27511));hold on;
%plot(Time_send(27528:27567),fn(27528:27567));hold on;
%plot(Time_send(27584:27623),fn(27584:27623));

%plot(Time_send(27354:27404),fhmov(27354:27404));hold on;
%plot(Time_send(27409:27460)-1.2,fhmov(27409:27460))

%plot(Time_send(27354:27404),uy(27354:27404));hold on;
%plot(Time_send(27409:27460)-1.2,uy(27409:27460))

%plot(Time_send(27354:27404),copx(27354:27404));hold on;
%plot(Time_send(27409:27460)-1.2,copx(27409:27460))

%plot(Time_send(27354:27404),copy(27354:27404));hold on;
%plot(Time_send(27409:27460)-1.2,copy(27409:27460))

for f=1:1:size(cell(T))
    plot(T{f},Y{f,2}); hold on;
end

