function net = cnnffvariable_chg(net, x)

n = numel(net.layers);
net.layers{1}.a{1}{1} = x;
inputmaps = 1;
oupsam=numel(net.layers{1}.a); % output blocks
samporg=size(x,3);

for l = 2 : n   %  for each layer
    if strcmp(net.layers{l}.type, 'c')
        ad1= floor(net.layers{l}.kernelsize(2)/2);
        for si=1:oupsam
            for j = 1 : net.layers{l}.outputmaps   %  for each output map  create temp output map
                x1=net.layers{l - 1}.a{si}{1};
                z = zeros([size(x1,1),size(x1,2)+ad1*2,size(x1,3)] - [net.layers{l}.kernelsize(1) - 1 net.layers{l}.kernelsize(2) - 1 0]);
                for i=1:inputmaps
                    x1=net.layers{l - 1}.a{si}{i};
                    xx=zeros(size(x1,1),size(x1,2)+ad1*2,size(x1,3));
                    xx(:,1:ad1,:)=x1(:,end-ad1+1:end,:);
                    xx(:,ad1+1:size(x1,2)+ad1,:)=x1;
                    xx(:,size(x1,2)+ad1+1:end,:)=x1(:,1:ad1,:);
                    
                    z = z + convn(xx, net.layers{l}.k{i}{j}, 'valid');
                end
                net.layers{l}.a{si}{j} = sigm(z + net.layers{l}.b{j});  %  add bias, pass through nonlinearity
            end
        end
        inputmaps = net.layers{l}.outputmaps;   %  set number of input maps to this layers number of outputmaps
        
    elseif strcmp(net.layers{l}.type, 's')    %  downsample
        ad1s=net.layers{l}.scale(2)-1;
        fil = ones(net.layers{l}.scale) / prod(net.layers{l}.scale );
        
        for si=1:oupsam
            for j=1:inputmaps
                xs1=net.layers{l - 1}.a{si}{j};
                xxs=zeros(size(xs1,1),size(xs1,2)+ad1s,size(xs1,3));
                xxs(:,1:size(xs1,2),:)=xs1;
                xxs(:,size(xs1,2)+ad1s:end,:)=xs1(:,1:ad1s,:);
                
                z = convn(xxs, fil, 'valid');   %  !! replace with variable
                net.layers{l}.a{2*si-1}{j} = z(1 : net.layers{l}.scale(1) : end, 1 : net.layers{l}.scale(2) : end, :);
                net.layers{l}.a{2*si}{j} = z(1 : net.layers{l}.scale(1) : end, 2 : net.layers{l}.scale(2) : end, :);
            end
        end
        oupsam=size(net.layers{l}.a,2);
    end
end

%%%%%%%%%%%%%%%%%%%%%% every block (every sample: sample1(rotate) sample2(rotate))
net.fv=[];

sa= size(net.layers{n}.a{1}{1});
idx = net.idxff;

for j = 1 : numel(net.layers{n}.a{1})
    tmap1=[];
    for bi=1:numel(net.layers{n}.a)
        tmap=[];
        ab=reshape(net.layers{n}.a{bi}{j},sa(1)*sa(2),samporg);
        for si =1:samporg
            abc1=ab(:,si);
            abc=abc1(idx);
            tmap=[tmap,abc(:,1:sa(1):end)];
        end
        tmap1=[tmap1,tmap];
    end
    net.fv=[net.fv; tmap1];
end

net.o = sigm(net.ffW * net.fv + repmat(net.ffb, 1, size(net.fv, 2)));
