Microscopic theory of impurity states in coupled quantum wells and superlattices