Phasing of segmented mirrors: a new algorithm for piston detection