function [huber_loss] = compute_huber_loss(diff, thresh)

    diff_abs = abs(diff);
    
    huber_loss = (diff .^2) .* (diff_abs <= thresh) + ...
        2 * thresh * (diff_abs - 0.5 * thresh) .* (diff_abs > thresh);

end