Exemple #1
0
def test_crossfade(var, benchmark):
    w1, p1, e1 = var
    dict: UserDict = UserDict()
    dict["wave"], dict["partition"] = w1, p1
    mt1 = MultiTrack(dict)

    w2 = Wave(value=np.arange(0, 50), fs=1)
    p2 = Partition(
        np.array([0, 15, 20, 50], dtype=np.int64),
        np.array(["start", "middle", "end"]),
        1,
    )
    dict = UserDict()
    dict["wave"], dict["partition"] = w2, p2
    mt2 = MultiTrack(dict)

    mt3 = benchmark(mt1.crossfade, mt2, 5)
    assert mt3.duration == mt1.duration + mt2.duration - 5
    assert mt3["wave"] == Wave(
        value=np.r_[np.arange(45), np.array([37, 31, 24, 18, 11]), np.arange(5, 50)],
        fs=1,
    )
    assert mt3["partition"] == Partition(
        np.array([0, 15, 20, 48, 60, 65, 95], dtype=np.int64),
        np.array(["start", "middle", "end", "start", "middle", "end"]),
        1,
    )
Exemple #2
0
def test_crossfade(benchmark):
    wav1 = Wave(np.array([1, 1, 1, 1, 1]), 1)
    wav2 = Wave(np.array([10, 10, 10, 10, 10]), 1)
    length = 3
    wav = benchmark(wav1.crossfade, wav2, length)
    assert wav1.duration + wav2.duration - length, wav.duration
    assert np.allclose(wav.value.flatten(), np.array([1, 1, 3, 5, 7, 10, 10]))
Exemple #3
0
def var():
    w = Wave(value=np.arange(0, 50), fs=1)
    p = Partition(
        np.array([0, 15, 20, 50], dtype=np.int64),
        np.array(["start", "middle", "end"]),
        1,
    )
    e = Event(np.array([0, 10, 30]).astype(np.int64), 1, 50)
    return w, p, e
Exemple #4
0
def test_select(var, benchmark):
    dict: UserDict = UserDict()
    dict["wave"], dict["partition"], dict["event"] = var
    mt = MultiTrack(dict)
    new_mt = benchmark(mt.select, 10, 24)
    assert new_mt["wave"] == Wave(value=np.arange(10, 24), fs=1)
    assert new_mt["partition"] == Partition(
        np.array([0, 5, 10, 14], dtype=np.int64),
        np.array(["start", "middle", "end"]),
        1,
    )
Exemple #5
0
    def read_edf(cls, path):
        raise NotImplementedError
        # TODO: adapt
        # the following is copied from elsewhere and won't work as is
        import pyedflib

        with pyedflib.EdfReader(str(path)) as f:
            labels = f.getSignalLabels()
            for label in labels:
                index = labels.index(label)
                wav = Wave(f.readSignal(index), f.getSampleFrequency(index))
                wav.label = label
                wav.path = f.with_name(f.stem + "-" + label + ".wav")
                wav.min = f.getPhysicalMinimum(index)
                wav.max = f.getPhysicalMaximum(index)
                wav.unit = f.getPhysicalDimension(index)
Exemple #6
0
def spectral_subtract(inp: Wave, frame_rate: int, silence_percentage: int) -> Wave:
    assert 0 < silence_percentage < 100
    ftr = frame(inp, frame_rate * 2, frame_rate)
    x = ftr.value * signal.hann(ftr.value.shape[1])
    X = fft(x, 2 ** nextpow2(x.shape[1]))
    M = np.abs(X)
    E = np.mean(M ** 2, axis=1)
    threshold = stats.scoreatpercentile(E, silence_percentage)
    index = np.where(E < threshold)[0]
    noise_profile = np.median(M[index], axis=0)
    M -= noise_profile
    np.clip(
        M, 0, None, out=M
    )  # limit this to a value greater than 0 to avoid -inf due to the following log
    Y = M * np.exp(1j * np.angle(X))  # DEBUG
    y = ifft(Y).real
    s = ola(y, inp.fs, inp.duration, frame_rate * 2, frame_rate)
    return Wave(s, inp.fs)
Exemple #7
0
 def process(
     self,
     progressTracker: Optional[DefaultProgressTracker] = None
 ) -> Tuple[Wave]:
     # Processor.process(self, **kwargs)
     if progressTracker is not None:
         self.progressTracker = progressTracker
     wav = self.data.wave
     assert isinstance(wav, Wave)
     x = wav.value
     self.progressTracker.update(10)
     y = signal.lfilter(self.parameters["B"], self.parameters["A"],
                        x).astype(x.dtype)
     self.progressTracker.update(90)
     new_track = (Wave(
         y,
         fs=wav.fs,
         path=str(
             wav.path.with_name(wav.path.stem + "-filtered").with_suffix(
                 Wave.default_suffix)),
     ), )
     return new_track
Exemple #8
0
    def read_xdf(cls, path):
        raise NotImplementedError
        import openxdf

        # TODO: below is a place holder and needs to be finalize
        xdf = openxdf.OpenXDF(path)
        signals = openxdf.Signal(xdf, path.with_suffix(".nkamp"))
        # TODO: automate this, why are the xdf.header names different from signals.list_channels?
        for label in ["ECG", "Chin"]:
            # logger.info(f'reading {label} channel')
            sig = signals.read_file(label)[label]
            wav = Wave(sig.ravel(), 200)
            wav.label = label
            # wav.path = file.with_name(file.stem + '-' + label + '.wav')
            wav.min = -3200
            wav.max = 3200
            wav.unit = "1"
Exemple #9
0
def var():
    w = Wave(value=np.arange(0, 16000), fs=16000)
    v = Wave(value=np.arange(100, 200), fs=16000)
    assert isinstance(w, Track)
    assert isinstance(v, Track)
    return w, v
Exemple #10
0
def test_resample(benchmark):
    w1 = Wave(value=np.arange(0, 50), fs=1)
    w2 = benchmark(w1.resample, 2)
    assert w1.value[0][0] == int(w2.value[0][0])
Exemple #11
0
def test_select(var, benchmark):
    w, _ = var
    w = benchmark(w.select, 10, 20)
    assert w == Wave(np.arange(10, 20, dtype=np.int64), 16000)