Exemplo n.º 1
0
def findPhase(signal: Signal, grid: Signal, period: int, toleranceWindow=0):
    """
    find the phase of the signal based on it's amplitude at the grid positions and the number of peaks
    - signal: works best with a discrete signal as no aglomeration is done
    - grid: positions of the beats
    - period: the periodicity to test
    - tolerance window: if not at 0, returns the closest value in the signal to the grid, within the tolerance window
    
    test:
    # result = findPhase(Signal(np.ones(5), times=np.array([0, 4, 8, 9, 12])+1), Signal(np.ones(16), times=range(16)), period=4)
    # print(result) = 1
    """
    phases = []
    for phase in range(period):
        values = [signal.getValue(grid.times[i], toleranceWindow=0) for i in range(phase, len(grid), period)]
        phases.append((np.sum([v for v in values if v is not None]) * len(values)))

    bestPhase = np.argmax(phases)
    return bestPhase
Exemplo n.º 2
0
    def findPhaseLocal(self,
                       period: int,
                       signal: Signal,
                       grid: Signal,
                       toleranceWindow=0.1):
        """
        find the phase of the signal based on it's amplitude at the grid positions and the number of peaks
        - signal: works best with a discrete signal as no aglomeration is done
        - grid: positions of the beats
        - period: the periodicity to test
        - tolerance window: if not at 0, returns the closest value in the signal to the grid, within the tolerance window

        test:
        # result = findPhase(Signal(np.ones(5), times=np.array([0, 4, 8, 9, 12])+1), Signal(np.ones(16), times=range(16)), 
            period=4)
        # print(result) = 1
        """
        phases = []
        for phase in range(period):
            values = [
                signal.getValue(grid.times[i], toleranceWindow=toleranceWindow)
                for i in range(phase, len(grid), period)
            ]
            values = [v for v in values if v is not None]
            if self.parameters["distanceMetric"].value == "RMS":
                value = np.sqrt(np.mean(np.array(values)**2))
            elif self.parameters["distanceMetric"].value == "sum":
                value = np.sum(values)
            elif self.parameters["distanceMetric"].value == "Veire":
                value = np.sum(values) * len(values)
            else:
                raise Exception("Bad distance metric parameter" +
                                self.parameters["distanceMetric"].value)
            phases.append(value)

        # bestPhase = np.argmax(phases)
        return phases