import numpy as N

class Signalpath:
    def phase(self, degrees):
        """
        rotate signal by **degrees** for phase cycling, etc.

        :param degrees: rotate signal by this value
        :return:
        """
        if self.get_number_of_channels() != 2:
            raise Exception("rotation defined only for 2 channels")
        # simple case 0, 90, 180, 270 degree
        reduced_angle = divmod(degrees, 90)
        if abs(reduced_angle[1]) < 1e-6:
            reduced_angle = reduced_angle[0] % 4
            if reduced_angle == 0:
                return

            elif reduced_angle == 1:
                self.y[1] *= -1
                self.y = [self.y[1], self.y[0]]

            elif reduced_angle == 2:
                self.y[0] *= -1
                self.y[1] *= -1

            elif reduced_angle == 3:
                self.y[0] *= -1
                self.y = [self.y[1], self.y[0]]
        else:
            sin_angle = N.sin(degrees / 180.0 * N.pi)
            cos_angle = N.cos(degrees / 180.0 * N.pi)
            self.y = [cos_angle * self.y[0] - sin_angle * self.y[1],
                      sin_angle * self.y[0] + cos_angle * self.y[1]]

        return self
