% Common Fragment Search
% This creates and analyzes signals acording to Beta size windows.
% Input: Signal to analize, alpha, windows from previous rounds, increase.
% Output: Window value. (has more now, experimental procedure)
% 6may11
% B

%% Previous step: "progresive"

 
function [windowsnext,WindowsSeqpdist,WindowsOrSignal,Hitswin,Hitspos]=localalignmentwindows2(seq,alpha,beta,increase,windowsnext,WindowsSeqpdist,WindowsOrSignal)
%% Signal Transformation
% Transform Sequence by MT
   
   SeqTM{1}=DNAMeltingArbitrary(seq{1},alpha);
   TMlength(1)=length(SeqTM{1});
   MaxTMlength=max(TMlength);
   

   Signalpretreat{1}=Signalpretreatment(SeqTM{1});
   
%% Create DotDistance Vectors, (x=y(i)-y(y+1)

    for j=1:TMlength-1
        Dotdistance{1}(j)=Signalpretreat{1}(j)-Signalpretreat{1}(j+1);
    end
    Dotdistance{1}(j+1)=0;

MaxTMlength=max(TMlength);

%% Incorporate windows 
p=numel(windowsnext);

%% Comparison 
% Compare small fragment vs whole signal 

   %% one window correction... uncoment 38-40
finale=(TMlength-beta-alpha);
% if numel(Dotdistance{1}(1+alpha:1+beta+alpha-1))==TMlength-finale
%     finale=1;
% end

%% Main For Comparison
     for z=1:p
         WaveCompare=0;
         m=1;
        for i=1:increase:finale                
            %% Create nextdot compare value 
% Derivative analysis several variables are for statistical propurses.
if numel(windowsnext{z})==numel(Dotdistance{1}(i+alpha:i+beta+alpha-1))
                 Toseqpdist{1}=WindowsSeqpdist{z};
                 Toseqpdist{2}=seq{1}(i:i+beta-1);
                 Winsqpdist(i)=seqpdist(Toseqpdist);
                 WindowNextDot{i}=abs(windowsnext{z}-Dotdistance{1}(i+alpha:i+beta+alpha-1));
                 SumWindowNextDot{i}=sum(WindowNextDot{i});
                 StdWindowNextDot{i}=std(WindowNextDot{i});
                 MeanWindowNextDot{i}=mean(WindowNextDot{i});
                 Lim(z)=(MeanWindowNextDot{i});
                 PerSignalNextDot{z}=WindowNextDot;
                 SumPerSignalNextDot{z}=SumWindowNextDot;
                 MeanPerSignalNextDot{z}=MeanWindowNextDot;
                 Seqpdistvalues{z}=Winsqpdist;
                 Corrtrans=corrcoef(WindowsOrSignal{z},Signalpretreat{1}(i+alpha:i+beta+alpha-1));
                 Corrtrans2{i}=abs(Corrtrans(1,2));
                 Corrtrans3{z}=Corrtrans2;
                 
                                  
                 % Autoregresive and Correlation analysis
                 pmax = 5;
                 fm = 1;
                 fmin = 1;            
                 fmax = 10;  
                 M1 = [WindowsOrSignal{z}; Signalpretreat{1}(i:i+beta-1)];
                 M1=M1';
                 Np2 = length(WindowsOrSignal{z});
                 f = fmin:fm/Np2:fmax-fm/Np2;         
                 [w,ARcoeff,E,SBC,FPE] = arfit(M1,1,pmax,'fpe');   
                 [pval p] = min(FPE);         
                 [C,freq] = coherencia(ARcoeff,E,f,fm,p);
                 Cohetrans=((sum(C(1,:)))-(numel(C(1,:))/2))/(numel(C(1,:))/2);
                 Cohetrans2{i}=Cohetrans;
                 Cohetrans3{z}=Cohetrans2;
end
                 
        end
     end
%% Points to consider  

%% Determine Lim & methods value
  Limi=mean(Lim)+std(Lim); % DerMethodlimit 

Limit=0.7; % AllMethodlimit Established treshold
Coheinfluence=1/3; % Influence of each method in the comparison.
Corrinfluence=1/3;
Derinfluence=1/3;
%% 
p=numel(windowsnext);

 % Values
 for m=1:p
     
     for g=1:numel(MeanPerSignalNextDot{1,m})
         a=MeanPerSignalNextDot{1,m};
         a1=Cohetrans3{1,m};
         a2=Corrtrans3{1,m};
         aa(g)=a{g};
         aa1(g)=a1{g};
         aa2(g)=a2{g};
     
   
     

         % Transform der value into 0-1 equivalent scale
         if aa(g)<Limi
             bfix(g)=((Limi-aa(g))/Limi);
         else
             bfix(g)=0;
         end
         v=(bfix(g)*Derinfluence);
         v1=(aa1(g)*Coheinfluence);
         v2=(aa2(g)*Corrinfluence);
         Vtot=(v+v1+v2);


       if Vtot>Limit                 
                Hitswin{m}(g)=1;
                Hitspos{m}(g)=g;
               
         else
               
               Hitswin{m}(g)=0;
               Hitspos{m}(g)=0;
               
                
       end
     end
    
 end



%% Next Step: return to progresive.
end

