function net = cnnbpvariable_chgn712(net, y)

n = numel(net.layers);
samporg=size(y,2);
sampfin=size(net.o,2);


y1 = [];
for i = 1 : size(y,2)
    y1 = [y1 repmat(y(:,i), [1, size(net.o,2) / size(y,2) / numel(net.layers{n}.a)])];
end

blocksz = size(y1,2);

y1=repmat(y1,1,numel(net.layers{n}.a));

net.e = net.o - y1;
net.L = 1/2* sum(net.e(:) .^ 2) / size(net.e, 2);  %  loss function

%%  backprop deltas
net.od = net.e .* (net.o .* (1 - net.o));   %  output delta
net.fvd = (net.ffW' * net.od);              %  feature vector delta

idx = net.idxbp;
sa = net.mapsize;
fvnum = sa(1) * sa(2);

dz=net.fvd';
for ci=1:sa(2)-1
    dz1=dz(ci+1:sa(2):end,:);
    dz(ci+1:sa(2):end,:)= dz1(:,idx(ci+1,:));
end
dz=dz';

for j = 1 : numel(net.layers{n}.a{1})%%%feature map
    for i=1 : 1 %%%block
        d=dz(((j - 1) * fvnum + 1) : j * fvnum,(i-1)*sa(2)* samporg+1:i* samporg*sa(2));
        ds1=[];
        for si=1:samporg
            ds=sum(d(:,(si-1)*sa(2)+1:si*sa(2)),2)/sa(2);
            ds1=[ds1,ds];            %%% concrete all sample, each has only one average result
        end
        net.layers{n}.d{j}(:,:,(i-1)*samporg+1:(i)*samporg)= reshape(ds1, sa(1), sa(2),  samporg);
    end
end

for l = (n - 1) : -1 : 1
    if strcmp(net.layers{l}.type, 'c')
        sc=prod(net.layers{l + 1}.scale);
        gap= numel(net.layers{n}.a)/(2*numel(net.layers{l}.a))*samporg;
        for j = 1 : numel(net.layers{l}.a{1})
            sa=size(net.layers{l}.a{1}{j});
            tmap=zeros(sa(1),sa(2),samporg*1);
%             for bi=1:numel(net.layers{l}.a)
%                 tmap(:,:,2*gap*(bi-1)+1:2*gap*bi)=repmat(net.layers{l}.a{bi}{j},[1 1 ,2*gap/samporg]);     %%%all feature map j from all blocks
%             end
            tmap(:,:,1:samporg)=net.layers{l}.a{1}{j};
            
            %%%%%%%%%%%%%%to align, expand then shift some blocks
            if(  size(net.layers{l+1}.d{j}, 3) > 1 )
                dd=expand( net.layers{l+1}.d{j},[net.layers{l + 1}.scale(1) net.layers{l + 1}.scale(2) 1]);
            else
                dd=expand( net.layers{l+1}.d{j},[net.layers{l + 1}.scale(1) net.layers{l + 1}.scale(2)]);
            end
%             for  i=gap+1:gap*2:size(dd,3)
%                 dd(:,:,i:i+gap-1)=circshift( dd(:,:,i:i+gap-1),[0 1 0]);
%             end
            %%%%%%%%%%%%%%%% 8/4: 01010101;8/2:00110011;8/1:00001111
            net.layers{l}.d{j} =tmap.*(1-tmap).* dd / sc;
        end
        
    elseif strcmp(net.layers{l}.type, 's')
        ad1= floor((net.layers{l+1}.kernelsize(2)-1)/2);
        for i = 1 : numel(net.layers{l}.a{1})
            sa=size(net.layers{l}.a{1}{1});
            z = zeros(sa(1),sa(2),samporg*1);
            
            for j = 1 : numel(net.layers{l + 1}.a{1})
                tem=[net.layers{l + 1}.d{j}(:,end-ad1+1:end,:),net.layers{l + 1}.d{j},net.layers{l + 1}.d{j}(:,1:ad1,:)];
                tmp2 = convn(tem,  rot180(net.layers{l + 1}.k{i}{j}),'full');
                z = z + tmp2(:, net.layers{l+1}.kernelsize(2):end-net.layers{l+1}.kernelsize(2)+1, :);
            end
            net.layers{l}.d{i}= z;
        end
    end
end

%calculate gradient
for l = 2 : n
    if strcmp(net.layers{l}.type, 'c')
        gap= numel(net.layers{n}.a)/(2*numel(net.layers{l}.a))*samporg;
        ad2= net.layers{l}.kernelsize(2)-1;
        for j = 1 : numel(net.layers{l}.a{1})
          
          
          
            for i = 1 : numel(net.layers{l - 1}.a{1})
                sa=size(net.layers{l - 1}.a{1}{i});
                tmap=zeros(sa(1),sa(2),samporg*1);
%                 for bi=1:numel(net.layers{l}.a)
%                     tmap(:,:,2*gap*(bi-1)+1:2*gap*bi)=repmat(net.layers{l - 1}.a{bi}{i},[1 1 ,2*gap/samporg]);     %%%all feature map j from all blocks
%                 end
                tmap(:,:,1:samporg)=net.layers{l - 1}.a{1}{i};
                xx=zeros(size(tmap,1),size(tmap,2)+ad2,size(tmap,3));
                xx(:,1:floor(ad2/2),:)=tmap(:, end-floor(ad2/2)+1:end, :);
                xx(:,floor(ad2/2)+1:end-floor(ad2/2), :)=tmap;
                xx(:, end-floor(ad2/2)+1:end,:)=tmap(:,1:floor(ad2/2),:);
                z1 =convn(flipall(xx), net.layers{l}.d{j}, 'valid');
                net.layers{l}.dk{i}{j}=z1 / size(net.layers{l}.d{j}, 3);
            end
            net.layers{l}.db{j} = sum(net.layers{l}.d{j}(:)) / size(net.layers{l}.d{j}, 3);
        end
    end
end
net.dffW = net.od(:,1:blocksz) * (net.fv(:, 1:blocksz))' / blocksz;
net.dffb = mean(net.od(:,1:blocksz), 2);
