function [ handles ] = combineFiltsFromUnitCell( handles )
% Uses filter array definitions to generate the system response to filter weights
%  

Z = handles.DirichletZ;
[XI,ETA] = meshgrid(handles.xi,handles.eta);

for l = 1:handles.numFiltersD
    ZSum = zeros(handles.etaSizeD,handles.xiSizeD);
    filtVal = flipud(handles.filtVal{l});
    for k = 1:handles.ySizeD
        for j = 1:handles.xSizeD
            ZSum = ZSum + filtVal(k,j)*Z{j,k};
        end
    end
    if handles.applyPixShapeFcnD == true;
        H = handles.shapeFcnD( 1,1,XI,ETA );
        ZSum = ZSum.*H;
    end
    if handles.showResults == true
        figure(handles.filterPlots(l));
        set(handles.filterPlots(l),'Name',['Channels for Filter ' num2str(l)],'NumberTitle','off');
       subplot(1,2,1);
%        figure(handles.unitCellSinglePlot(1));
        imagesc(handles.xi,handles.eta,real(ZSum));
%         imagesc(real(ZSum));
        axis square;
        ax = gca;
        ax.YDir = 'normal';
%         ax.XTickLabel = [];
%         ax.YTickLabel = [];
        colormap(viridis(256));
%         matlab2tikz(['./figures/filtNum' num2str(l) '_' num2str(handles.xSizeD) 'x' num2str(handles.ySizeD) '_real.tikz'],'width','\fpTwidth','height','\fpTheight');
        subplot(1,2,2);
%         figure(handles.unitCellSinglePlot(2));
        imagesc(handles.xi,handles.eta,imag(ZSum));
%         imagesc(imag(ZSum));
        axis square;
        ax = gca;
        ax.YDir = 'normal';
%         ax.XTickLabel = [];
%         ax.YTickLabel = [];
        colormap(magma(256));
%         matlab2tikz(['./figures/filtNum' num2str(l) '_' num2str(handles.xSizeD) 'x' num2str(handles.ySizeD) '_imag.tikz'],'width','\fpTwidth','height','\fpTheight');
    end
    handles.ZSum{l} = ZSum;
    
    y = reshape(filtVal.',1,handles.xSizeD*handles.ySizeD);
    Lt = handles.L.';
    dLocs = Lt*y.';
    dLocs(abs(dLocs) < 10^-6) = 0;
    handles.fourierFilt{l} = dLocs;
end

totDLocs = zeros(size(dLocs));
for l = 1:handles.numFiltersD
    totDLocs = totDLocs + abs(handles.fourierFilt{l});
end
totDLocs(abs(totDLocs) < 10^-6) = 0;

ind = find(abs(totDLocs) ~= 0);
handles.fourierFiltInd = ind;

if (mod(handles.xSizeD*handles.fsMultD,2) ~= 0) && (mod(handles.ySizeD*handles.fsMultD,2) ~= 0)
    [I,J] = ind2sub([handles.xSizeD*handles.fsMultD handles.ySizeD*handles.fsMultD],ind);
end
if (mod(handles.xSizeD*handles.fsMultD,2) == 0) && (mod(handles.ySizeD*handles.fsMultD,2) ~= 0)
    [I,J] = ind2sub([(handles.xSizeD*handles.fsMultD+1) handles.ySizeD*handles.fsMultD],ind);
end
if (mod(handles.xSizeD*handles.fsMultD,2) ~= 0) && (mod(handles.ySizeD*handles.fsMultD,2) == 0)
    [I,J] = ind2sub([handles.xSizeD*handles.fsMultD (handles.ySizeD*handles.fsMultD+1)],ind);
end
if (mod(handles.xSizeD*handles.fsMultD,2) == 0) && (mod(handles.ySizeD*handles.fsMultD,2) == 0)
    [I,J] = ind2sub([(handles.xSizeD*handles.fsMultD+1) (handles.ySizeD*handles.fsMultD+1)],ind);
end

for k=1:size(ind)
    dCoordinate(k,:) = [handles.xiStepVec(I(k)) handles.etaStepVec(J(k))];
end

handles.dCoordinate = dCoordinate;

end

