function [s_c,S_largest,ind_absE]=update_sparspara(S_mat,S_largest,...
    ind_absEa,ind_absE_tmp,...
            delta,s_upper,s_c,tracking)
%update_sparspara
%                     
% s_range=s_c;
% s_correct = 0;
tol_precision = 1e-10;

absS_mat=abs(S_mat);
ind_absE= find(absS_mat>delta+tol_precision);
s_c = length(ind_absE);
S_largest   = absS_mat(ind_absE);

if s_c > s_upper
    [~,ind]=sort(absS_mat,'descend');
    ind_absE = sort(ind(1:s_upper));
    S_largest   = absS_mat(ind_absE);
    s_c = length(ind_absE);
end

if s_c == 0
%     s_c = 1;
    S_largest = [];%delta;
    ind_absE = [];%1;
end
% % % % while not(s_correct)
% % % %     if length(find(S_largest>delta)) <= s_range
% % % %         s_c = length(find(S_largest>delta));
% % % %         s_correct = 1;
% % % %     else
% % % %         s_range = min(2*s_range,s_upper);
% % % %         if ~tracking
% % % %             [S_c_maxsp1,ind_absEa] = maxkmex(abs(S_mat(:)),s_range+1);
% % % % %             [S_largest,ind_absE_tmp]=sort(S_c_maxsp1,'descend');
% % % % %             [~,ind_absE_permut]=sort(ind_absE_tmp);
% % % % %             ind_absE=ind_absEa(ind_absE_tmp);
% % % %             
% % % %             [S_c_sort,ind_absE_tmp]=sort(S_c_maxsp1,'descend');
% % % %             [ind_absE,chg_ind]=sort(ind_absEa(ind_absE_tmp));
% % % %             S_largest=S_c_sort(chg_ind);
% % % %         end
% % % %         if length(find(S_largest>delta)) <= s_range
% % % %             s_c = length(find(S_largest>delta));
% % % %             s_correct = 1;
% % % %         end
% % % %     end
% % % %     if s_range == s_upper
% % % %         s_c=min(s_c,s_range);
% % % %         s_correct = 1;
% % % %     end
% % % % end
% % % % 
% % % % if s_c == 0
% % % %    s_c = 1;
% % % %    S_largest = delta;
% % % % else
% % % %     absS_mat=abs(S_mat);
% % % %     ind_absE= find(absS_mat>delta);
% % % %    S_largest   = absS_mat(ind_absE);%S_largest(1:s_c);
% % % % end

% ind_absE=sort(ind_absEa(ind_absE_tmp(1:s_c)));



% ind_absE = ind_absE(1:s_c);
% [~,ind_absE_permut]=sort(ind_absE_tmp(1:s_c));
% [S_c_s_val,ind_absE_permut]=sort(ind_absE);
end

