where
Least squares solution
Assuming the adaptation should be smooth, phase unwrapping is used to make sure that the arctan function gives the wanted value.