clear all;
N=16 % degr du polynome generateur du scrambler 

out=[];
outt=[];
VectExt=[];

%x=[1 1 0 0 1 1 0 0 1 1 0 0 1 1 1]
%x=randsrc(1,N,[0 1; 0.25 0.75])
 %x = randi([0 1],40,1)
 %x=[1 1 0 0 1 0 0 0 0 0 0 0 0 0 0]
 xx=[1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 1];
Lv=length(xx);
 Nit = fix(Lv/N);
 Rst = mod(Lv,N);
 if (Rst == 0) 
     VecEntree=xx;
 else
 
        NPadZ=N-Rst ;
        VectZ=zeros(1,NPadZ); 
        VectRst=xx(N*Nit+1:Lv);
        VectRstC=[VectRst VectZ];
        VecEntree= [xx VectZ];
 end;
NPadZ=N-Rst ;

 for j=1: N: Lv+NPadZ
     VectExt=VecEntree(j: N+j-1);
    y(1:N)=[1 0 1 1 1 0 1 1 0 1 1 1 0 1 1 0];
     outSc(1:N)=0;
   for i=1:N
       outSc(i)=xor(VectExt(i),xor( y(16) ,xor(y(12),y(5))));
       
       y(16:-1:5)=y(12:-1:1);
       y(16:-1:12)=y(5:-1:1);
       y(12:-1:8)=y(5:-1:1);
        
       l=1:N-1;
       y(N+1-l)=y(N-l);
      
       y(1)=outSc(i);
   end;
   
   out = [out outSc];
 end; 
  M=length(out);

 for j=1: N: M
     VecEntree1=out(j: N+j-1);
     y(1:N)=[1 0 1 1 1 0 1 1 0 1 1 1 0 1 1 0];
     outDsc(1:N)=0;

   for i=1:N
       outDsc(i)=xor(VecEntree1(i),xor(y(16),xor( y(12) ,y(5))));
       
       y(16:-1:5)=y(12:-1:1);
       y(16:-1:12)=y(5:-1:1);
       y(12:-1:8)=y(5:-1:1);
      k=1:N-1;
      
       y(N+1-k)=y(N-k);
      
       y(1)=outDsc(i);
       
   end;
   outt =[outt outDsc] ;
 end;
  outtt=outt(1:Lv);