clc
close all
clear

format

    d = dir('*.csv');
    
%     smallest_index = 0;
%     
%     biggest_index = 0;
%     
for i = length(d):-1:1
    Dstr_max_structure(i).name = d(i).name;
    Dstr_max_structure(i).matrix = csvread(d(i).name);
%     
%     if i == 3
%         sizex = size(Dstr_max_structure(i).matrix(:,2),1);
%     end
% 
%     if size( Dstr_max_structure(i).matrix(:,2) , 1 ) <= sizex
%         sizex = size( Dstr_max_structure(i).matrix(:,2) , 1 );
%         smallest_index = i;
%     end
    
    %if size( Dstr_max_structure(i).matrix(:,2) , 1 ) > sizex
    %    biggest_index = i;
    %end
end
% 
% SmallestSize = size(Dstr_max_structure(smallest_index).matrix(:,2),1);
% tensao = zeros(SmallestSize, length(d));
% angulo = zeros(SmallestSize, length(d));
% secondTension = zeros(SmallestSize, length(d));
% 
% for i = 1:length(d)
%      %aqui crio variaveis com os nº de dados igual ao mais pequeno!
%     tensao(:,i) = Dstr_max_structure(i).matrix(1:SmallestSize,2);
%     angulo(:,i) = Dstr_max_structure(i).matrix(1:SmallestSize,3);
%     
%     secondTension(:,i) = Dstr_max_structure(i).matrix(1:SmallestSize,5);
% end
% 
%  corrTensao = triu( corr(tensao , tensao) , 1 );
%  corrAngulo = triu( corr(angulo , angulo) , 1 );
%  
%  corrsecondTension = triu( corr(secondTension , secondTension) , 1 );
%  
%  corrTensaoSTD = [];
%  corrAnguloSTD = [];
%  corrsecondTensionSTD = [];
%  
%  for n = 1:numel(corrTensao)
%      
%      if corrTensao(n) > 0
%          corrTensaoSTD = [ corrTensaoSTD corrTensao(n) ];
%          corrAnguloSTD = [ corrAnguloSTD corrAngulo(n) ];
%          corrsecondTensionSTD = [ corrsecondTensionSTD corrsecondTension(n) ];
%      end
%      
%  end
%  
%  mediaCorrTensao = mean(corrTensaoSTD)
%  mediaCorrAngulo = mean(corrAnguloSTD)
%  
%  mediacorrsecondTension = mean(corrsecondTensionSTD)
%  
%  desvioCorrTensao = std(corrTensaoSTD)
%  desvioCorrAngulo = std(corrAnguloSTD)
%  
%  desviocorrsecondTensionSTD = std(corrsecondTensionSTD)
%  

initialTensionMeans = [];
initialTensionDeviations = [];
initialSecondTensionMeans = [];
initialSecondTensionDeviations = [];

%PROCURAR O INDEX COM O INICIO DA PERTURBACAO PARA SACAR AS TENSOES MESMO
%ANTES DO ESTIMULO

indexInicioPerturbacao = 0 ;

for n = 1 : size(Dstr_max_structure(1).matrix(:,1) , 1 )
    
    if Dstr_max_structure(1).matrix(n,4) > 400
        break
    end
    
    indexInicioPerturbacao = n ;
end


for i = 1 : 3 
    
    initialTensionMeans(i) = mean( Dstr_max_structure(i).matrix( indexInicioPerturbacao - 105 : indexInicioPerturbacao - 5 , 2 ) );

    initialSecondTensionMeans(i) = mean( Dstr_max_structure(i).matrix(indexInicioPerturbacao - 105 : indexInicioPerturbacao - 5 ,5) );

    initialAngle(i) = mean( Dstr_max_structure(i).matrix(indexInicioPerturbacao - 105 : indexInicioPerturbacao - 5 ,3) );
end

mediaInitialTension = mean(initialTensionMeans)
mediaInitialSecondTension = mean(initialSecondTensionMeans)
mediaInitialAngle = mean(initialAngle)


desvioMediaInitialTension = std(initialTensionMeans)
desvioMediaInitialSecondTension = std(initialSecondTensionMeans)
desvioMediaInitialAngle = std(initialAngle)

  Dstr_max_structure(1).name
 