Working on automatic calculation of threshold