Beispiel #1
0
def test_make_frames(ndim, snip_edges, writeable):
    # in that test we use default parameters (fs=16kHz, length=20ms,
    # shift=10ms)
    f = Frames(snip_edges=snip_edges)

    # random time in [0.1, 0.2] seconds
    nsamples = int((random.random() * 0.1 + 0.1) * f.sample_rate)

    # fill a random array
    shape = (nsamples,)
    if ndim >= 2:
        shape = shape + (2,)
    if ndim >= 3:
        shape = shape + (2,)

    aref = np.random.random(shape)
    array = np.copy(aref)

    # make the frames (by copy or by view)
    frames = f.make_frames(array, writeable=writeable)

    assert np.array_equal(array, aref)

    assert frames.shape == (
        f.nframes(aref.shape[0]), f.samples_per_frame) + aref.shape[1:]

    if writeable is False:
        with pytest.raises(ValueError):
            frames[0] = 0
    else:
        frames[0] = -1
        assert (frames[0] == -1).all()
Beispiel #2
0
    def __init__(self,
                 tokens=None,
                 sample_rate=16000,
                 frame_shift=0.01,
                 frame_length=0.025,
                 window_type='povey',
                 blackman_coeff=0.42):
        super().__init__(tokens=tokens)

        self.frame = Frames(sample_rate=sample_rate,
                            frame_shift=frame_shift,
                            frame_length=frame_length)

        self.window_type = window_type
        self.blackman_coeff = blackman_coeff
Beispiel #3
0
def test_frames_1_1(snip_edges):
    f = Frames(sample_rate=1, snip_edges=snip_edges)
    f.frame_shift = 1
    f.frame_length = 1
    assert f.nframes(10) == 10
    assert f.samples_per_frame == 1
    assert f.samples_per_shift == 1
    assert np.array_equal(
        f.boundaries(10),
        np.repeat(np.arange(10), 2).reshape(10, 2) + (0, 1))

    print()
    print(f.make_frames(np.arange(10)))
    print(np.arange(10)[:, np.newaxis])

    assert(np.array_equal(
        f.make_frames(np.arange(10)),
        np.arange(10)[:, np.newaxis]))
Beispiel #4
0
def test_frames_1_2(snip_edges):
    f = Frames(sample_rate=1, snip_edges=snip_edges)
    f.frame_length = 1
    f.frame_shift = 2
    assert f.nframes(10) == 5
    assert f.samples_per_frame == 1
    assert f.samples_per_shift == 2

    assert np.array_equal(
        f.boundaries(5),
        np.repeat(np.arange(5) * 2, 2).reshape(5, 2) + (0, 1))

    assert np.array_equal(
        f.make_frames(np.arange(10)),
        (np.arange(5) * 2)[:, np.newaxis])
Beispiel #5
0
def test_frames_5_3(snip_edges):
    f = Frames(sample_rate=1, snip_edges=snip_edges)
    f.frame_length = 5
    f.frame_shift = 3
    n = (2 if snip_edges else 3)
    assert f.nframes(9) == n
    assert f.samples_per_frame == 5
    assert f.samples_per_shift == 3

    assert np.array_equal(
        f.boundaries(n),
        np.repeat(np.arange(n) * 3, 2).reshape(n, 2) + (0, 5))

    framed = (np.arange(n) * 3).repeat(5).reshape(n, 5) + (0, 1, 2, 3, 4)
    if snip_edges is False:
        framed[-1, -1] = 8
    assert np.array_equal(f.make_frames(np.arange(10)), framed)
Beispiel #6
0
def test_frames_3_1(snip_edges):
    f = Frames(sample_rate=1, snip_edges=snip_edges)
    f.frame_length = 3
    f.frame_shift = 1
    n = 8 if snip_edges else 10
    assert f.nframes(10) == n
    assert f.samples_per_frame == 3
    assert f.samples_per_shift == 1

    assert np.array_equal(
        f.boundaries(n),
        np.repeat(np.arange(n), 2).reshape(n, 2) + (0, 3))

    framed = np.arange(n).repeat(3).reshape(n, 3) + (0, 1, 2)
    if snip_edges is False:
        framed[-2, -1] = 8
        framed[-1, -2:] = (8, 7)
    assert np.array_equal(f.make_frames(np.arange(10)), framed)
Beispiel #7
0
def test_frames_2_1(snip_edges):
    f = Frames(sample_rate=1, snip_edges=snip_edges)
    f.frame_length = 2
    f.frame_shift = 1
    n = 9 if snip_edges else 10
    assert f.nframes(10) == n
    assert f.samples_per_frame == 2
    assert f.samples_per_shift == 1

    assert np.array_equal(
        f.boundaries(n),
        np.repeat(np.arange(n), 2).reshape(n, 2) + (0, 2))

    framed = np.arange(n).repeat(2).reshape(n, 2) + (0, 1)
    if not snip_edges:
        framed[-1, -1] = 8

    print()
    print(f.make_frames(np.arange(10)))
    print(framed)

    assert np.array_equal(f.make_frames(np.arange(10)), framed)
Beispiel #8
0
def test_params():
    p = {'sample_rate': 1, 'frame_shift': 1,
         'frame_length': 1, 'snip_edges': False}
    assert Frames(**p).get_params() == p
    assert Frames().set_params(**p).get_params() == p
Beispiel #9
0
class FramedOneHotProcessor(_OneHotBase):
    """One-hot encoding on framed signals

    Computes the one-hot encoding on framed signals (i.e. on
    overlapping time windows)

    Parameters
    ----------
    tokens : sequence, optional
        The tokens composing the alignment. Specify the tokens if you
        want to have consistant one-hot vectors accross different
        :class:`Features`. By default the tokens are extracted from
        the alignment in :func:`process`.
    sample_rate : int, optional
        Sample frequency used for frames, in Hz, default to 16kHz
    frame_shift : float, optional
        Frame shift in seconds, default to 10ms
    frame_length : float, optional
        Frame length in seconds, default to 25ms
    window_type : {'povey', 'hanning', 'hamming', 'rectangular', 'blackman'}
        The type of the window, default is 'povey' (like hamming but
        goes to zero at edges)
    blackman_coeff : float, optional
        The constant coefficient for generalized Blackman window, used
        only when `window_type` is 'blackman', default is 0.42.

    """
    def __init__(self,
                 tokens=None,
                 sample_rate=16000,
                 frame_shift=0.01,
                 frame_length=0.025,
                 window_type='povey',
                 blackman_coeff=0.42):
        super().__init__(tokens=tokens)

        self.frame = Frames(sample_rate=sample_rate,
                            frame_shift=frame_shift,
                            frame_length=frame_length)

        self.window_type = window_type
        self.blackman_coeff = blackman_coeff

    @property
    def sample_rate(self):
        """The processor operation sample rate

        Must match the sample rate of the signal specified in
        `process`

        """
        return self.frame.sample_rate

    @sample_rate.setter
    def sample_rate(self, value):
        self.frame.sample_rate = value

    @property
    def frame_shift(self):
        """Frame shift in seconds"""
        return self.frame.frame_shift

    @frame_shift.setter
    def frame_shift(self, value):
        self.frame.frame_shift = value

    @property
    def frame_length(self):
        """Frame length in seconds"""
        return self.frame.frame_length

    @frame_length.setter
    def frame_length(self, value):
        self.frame.frame_length = value

    def process(self, alignment):
        # build a bijection token <-> onehot index
        token2index = self._token2index(alignment)

        # sample the alignment at the requested sample rate
        sampled = alignment.at_sample_rate(self.frame.sample_rate)

        # get the frames as pairs (istart:istop)
        nframes = self.frame.nframes(sampled.shape[0])
        frame_boundaries = self.frame.boundaries(nframes)

        # allocate the features data
        data = np.zeros((frame_boundaries.shape[0], len(token2index)),
                        dtype=np.bool)

        # allocate the window function
        window = shennong.features.window.window(
            self.frame.samples_per_frame,
            type=self.window_type,
            blackman_coeff=self.blackman_coeff)

        for i, (onset, offset) in enumerate(frame_boundaries):
            framed = sampled[onset:offset]
            # the frame is made of a single token, no needs to compute
            # a window function
            if np.all(framed[0] == framed[1:]):
                winner = framed[0]
            else:
                # several tokens in the frame, compute the weights

                weights = collections.defaultdict(int)
                for j, w in enumerate(window):
                    weights[framed[j]] += w

                # the winner token has the biggest weight
                winner = sorted(weights.items(),
                                key=operator.itemgetter(1),
                                reverse=True)[0][0]

            data[i, token2index[winner]] = 1

        try:
            properties = self.get_properties()
        except ValueError:  # tokens not defined
            self.tokens = token2index.keys()
            properties = self.get_properties()
            self.tokens = None
        properties[self.name].update({'token2index': token2index})

        return Features(data,
                        frame_boundaries / self.frame.sample_rate,
                        properties=properties)