function allFiltered = applyfilterfftsameLocalNorm(I, allfilter)
% applyfilterfftsameLocalNorm - Filter image by a bank of filters with local normalization. 
%
% I: input images
% allfilter: filter bank
% threshold: filter response is set to zero if below threshold

numImage = size(I, 2);    % number of training images
numOrient = size(allfilter, 2);     % number of orientations
h = (size(allfilter{1}, 1)-1)/2;  % half size of filters
[sx, sy] = size(I{1});  % size of images
thresholdFactor = 0.01;

allFiltered = cell(numImage, numOrient);  % filtered images
for i = 1 : numImage
    fftI{i} = fft2(I{i}, sx+h+h, sy+h+h);
end
for o = 1 : numOrient
   fftf{o} = fft2(allfilter{1, o}, sx+h+h, sy+h+h); 
end

radius = floor(h*2.5);
localHalfx = radius; localHalfy = radius;
for i = 1 : numImage
   tot = 0.;
   fftIi = fftI{i}; 
   for (o = 1 : numOrient)
      fftfo = fftf{o}; 
      out = ifft2(fftIi.*fftfo); 
      filtered = out(h+1:h+sx, h+1:h+sy); 
      re = real(filtered); im = imag(filtered); 
      energy = re.*re + im.*im;  % compute the local energy
      energy([1:h sx-h:sx],:) = 0.; energy(:,[1:h sy-h:sy]) = 0.; 
      allFiltered{i, o} = energy; 
      tot = tot + sum(sum(energy((h+1):(sx-h-1), (h+1):(sy-h-1))))/(sx-2*h-1)/(sy-2*h-1); 
   end
   ave = tot/numOrient; 
   for o = 1 : numOrient
       allFiltered{i, o} = allFiltered{i, o}/ave;  % normalizing by whitening
   end
       ClocalNormalizeDouble(sx, sy, numOrient, h, localHalfx, localHalfy, allFiltered(i, :), thresholdFactor); 
end
