add adaptive threshold function