function [ nextx ] = pf_resample_sys( currx, w ,essMin)
%PF_RESAMPLE_LV Systematic or low variance resampling

% YOUR CODE STARTS HERE:

ess= 1/sum(arrayfun(@(x) x^2,w));

M = size(currx, 3);
if(ess < essMin)

    nextx = zeros(3, 1, M);

    r = rand()*1/M;
    c= w(1);
    i=1;
    for m = 1:M
        U= r+(m-1)*1/M;
            while U>c
                i=i+1;
                c=c+w(i);
            end
            nextx(:,:,m)=currx(:,:,i);
    end
else
        nextx=currx;
end
% YOUR CODE END HERE

end

