GEA = cell(nbInstrument,nbBandPassFilters);


xHat = sparse(zeros(HFFTS,len));

% Model Spectrogram
for ins = 1:nbInstrument
    for filterInd = 1:nbBandPassFilters
        GEA{ins,filterInd} = sparse(zeros(HFFTS, len));
        for frame = first:curFrame
            GEA{ins,filterInd}(:,frame-first+1) = (filterBasis(:,filterInd)*gains(ins,frame)).*(excitationSpectrums{ins,frame}*partialsWeights(:,ins));
        end
        xHat = xHat + GEA{ins,filterInd}*filterCoefficients(filterInd,ins);
    end
end

%Compute distance and r
distFilter(nbUpdates) = sum(sum( tmpMaskedSpectrogram .* log((tmpMaskedSpectrogram+eps) ./ (xHat+eps)) - tmpMaskedSpectrogram + xHat));
r = (currentMag+eps)./(xHat+eps);
updateFilterCoeff = ones(nbBandPassFilters,nbInstrument);

%Update Filter Coefficients
for ins = 1:nbInstrument
    for filterInd = 1:nbBandPassFilters
        updateFilterCoeff(filterInd,ins) = (sum(sum(GEA{ins,filterInd}.*r))+eps)/(sum(sum(GEA{ins,filterInd}))+eps);
    end
end
clear GEA;
filterCoefficients = filterCoefficients.*updateFilterCoeff;
for ins=1:nbInstrument
    maxValue = max(filterCoefficients(:,ins));
    filterCoefficients(:,ins) = filterCoefficients(:,ins) / maxValue;
    gains(ins,:) = gains(ins,:) * maxValue;
end
outlook = filterBasis * filterCoefficients;
%     figure, plot(outlook(1:blockSize/8,:));