function [ handles ] = genChannelInversionMatrix( handles )
% Generates the inversion matrix to compute filtered values in the channel
% domain.

xiStepSize = 1/handles.xSizeD;
etaStepSize = 1/handles.ySizeD;

xiStepVecT = -1:xiStepSize:1;
etaStepVecT = -1:etaStepSize:1;

ind = find(abs(xiStepVecT) <=0.5);
xiStepVec = xiStepVecT(ind);

ind = find(abs(etaStepVecT) <=0.5);
etaStepVec = etaStepVecT(ind);

numXiSteps = length(xiStepVec);
numEtaSteps = length(etaStepVec);

for k = 1:handles.numFiltersD
    Z = handles.ZSum{k};        
    for l = 1:numEtaSteps
        for m = 1:numXiSteps
            xiIdx = find(abs(handles.xi-xiStepVec(m)) < 10^-12);
            etaIdx = find(abs(handles.eta - etaStepVec(l)) < 10^-12);
            tempRow((l-1)*numXiSteps + m) = Z(etaIdx,xiIdx);
        end
    end
    Lf(k,:) = tempRow;
end
LfR = real(Lf);
LfI = imag(Lf);

LfR(abs(LfR) < 10^-8) = 0;
LfI(abs(LfI) < 10^-8) = 0;

Lf = LfR + 1i*LfI;
s = svd(Lf);
sN = s./s(1);
sN(sN<10e-10) = 0;
CN = sN(1)/sN(handles.numFiltersD);
EWV = sum(1./(sN(1:handles.numFiltersD).^2));
CNF = sqrt(EWV*sum(sN(1:handles.numFiltersD).^2)); 
ind = find(sN);
CNR = sN(1)/sN(ind(end));
handles.R = max(ind);
handles.Lf = Lf;
handles.CN = CN;
handles.CNR = CNR;
handles.EWV = EWV;
handles.CNF = CNF;
end

