trans_splice.py 2.1 KB
Newer Older
1 2 3 4
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

Z
zhxfl 已提交
5
import numpy as np
Z
zhxfl 已提交
6 7 8 9
import math


class TransSplice(object):
Z
zhxfl 已提交
10 11
    """ copy feature context to construct new feature
        expand feature data from shape (frame_num, frame_dim) 
Z
zhxfl 已提交
12
        to shape (frame_num, frame_dim * 11)
Z
zhxfl 已提交
13 14 15 16

        Attributes:
            _nleft_context(int): copy left context number
            _nright_context(int): copy right context number
Z
zhxfl 已提交
17 18 19 20
    """

    def __init__(self, nleft_context=5, nright_context=5):
        """ init construction
Z
zhxfl 已提交
21 22 23
            Args:
                nleft_context(int):
                nright_context(int):
Z
zhxfl 已提交
24 25 26 27 28
        """
        self._nleft_context = nleft_context
        self._nright_context = nright_context

    def perform_trans(self, sample):
Z
zhxfl 已提交
29 30 31 32
        """ copy feature context 
        Args:
            sample(object): input sample(feature, label)
        Return:
Z
zhxfl 已提交
33
            (feature, label, name)
Z
zhxfl 已提交
34
        """
Z
zhxfl 已提交
35
        (feature, label, name) = sample
Z
zhxfl 已提交
36 37 38 39
        nframe_num = feature.shape[0]
        nframe_dim = feature.shape[1]
        nnew_frame_dim = nframe_dim * (
            self._nleft_context + self._nright_context + 1)
Z
zhxfl 已提交
40
        mat = np.zeros(
Z
zhxfl 已提交
41 42 43
            (nframe_num + self._nleft_context + self._nright_context,
             nframe_dim),
            dtype="float32")
Z
zhxfl 已提交
44
        ret = np.zeros((nframe_num, nnew_frame_dim), dtype="float32")
Z
zhxfl 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

        #copy left
        for i in xrange(self._nleft_context):
            mat[i, :] = feature[0, :]

        #copy middle 
        mat[self._nleft_context:self._nleft_context +
            nframe_num, :] = feature[:, :]

        #copy right
        for i in xrange(self._nright_context):
            mat[i + self._nleft_context + nframe_num, :] = feature[-1, :]

        mat = mat.reshape(mat.shape[0] * mat.shape[1])
        ret = ret.reshape(ret.shape[0] * ret.shape[1])
        for i in xrange(nframe_num):
Z
zhxfl 已提交
61 62
            np.copyto(ret[i * nnew_frame_dim:(i + 1) * nnew_frame_dim],
                      mat[i * nframe_dim:i * nframe_dim + nnew_frame_dim])
Z
zhxfl 已提交
63
        ret = ret.reshape((nframe_num, nnew_frame_dim))
Z
zhxfl 已提交
64
        return (ret, label, name)