
function [ xout ] = sim2 ( xout1 , Lg , delta  )

global p0 layer numBlocks q0 maxLevel simlength

xout2=[];
xout4=[];
 for j = 1 : numBlocks * (maxLevel+1) 
     xout2( (j-1)*p0+1:j*p0,: )=Lg(1).xq'*xout1( (layer)*(j-1)*q0 +1 : (j-1)*(layer)*q0 + (layer)*q0,: );   %%% p0 + p0*levels x simlength
 end

xout_nom = zeros ( numBlocks*p0 , simlength+1 ) ;
xout_sens = zeros ( p0 , simlength+1)  ;
xout = [];

idx = 1 ;

for i=1:numBlocks
    xout_nom ( (i-1)*p0 + 1: (i-1)*p0 + p0 , : )   = xout2 ( (maxLevel+1)*(i-1)*p0 + 1 : (maxLevel+1)*(i-1)*p0 + p0 , : ) ;
    xout_sens = zeros ( p0 , simlength+1 ) ;
    for j=1:maxLevel
        chk = (maxLevel+1)*(i-1)*p0 + j*p0 + 1
        delta(idx,1)
    xout_sens = xout_sens + xout2 ( (maxLevel+1)*(i-1)*p0 + (j)*p0 + 1 : (maxLevel+1)*(i-1)*p0 + (j)*p0 + p0 , :) * delta ( idx , 1 ) ;
    idx = idx + 1 ;
    end

    tmp = xout_nom ( (i-1)*p0+1 : (i-1)*p0+p0 , : ) - xout_sens ;
    xout = [xout ; tmp ];
end



