Automatic phase-error correction (APEC)