def reduce_ppg_dim(ppgs: Matrix, transform: SparseMatrix) -> Matrix:
    """Reduce full PPGs to monophone PPGs.

    Args:
        ppgs: A T*D PPG matrix.
        transform: A d*D sparse matrix.

    Returns:
        monophone_ppgs: A T*d matrix. Containing PPGs reduced into monophones.
    """
    num_frames = ppgs.num_rows
    num_phones = transform.num_rows

    # Convert the sparse matrix to a full matrix to avoid having to keep the
    # matrix type consistent
    full_transform = Matrix(num_phones, transform.num_cols)
    transform.copy_to_mat(full_transform)

    monophone_ppg = Matrix(num_frames, num_phones)
    monophone_ppg.add_mat_mat_(ppgs, full_transform,
                               MatrixTransposeType.NO_TRANS,
                               MatrixTransposeType.TRANS, 1.0, 0.0)
    return monophone_ppg
Пример #2
0
def apply_feat_transform(feats: Matrix, transform: Matrix) -> Matrix:
    """Apply an LDA/fMLLR transform on the input features.

    The transform is a simple matrix multiplication: F = FT' (' is transpose) in
    the case of LDA. For fMLLR, please see
    http://kaldi-asr.org/doc/transform.html#transform_cmllr_global
    This function is an extremely simplified version of
    https://github.com/kaldi-asr/kaldi/blob/5.3/src/featbin/transform-feats.cc

    Args:
        feats: A T*D feature matrix.
        transform: A D'*D matrix, where D' is the output feature dim.

    Returns:
        feats_out: A T*D' matrix.
    """
    feat_dim = feats.num_cols
    transform_rows = transform.num_rows
    transform_cols = transform.num_cols

    feats_out = Matrix(feats.num_rows, transform_rows)
    if transform_cols == feat_dim:
        feats_out.add_mat_mat_(feats, transform, MatrixTransposeType.NO_TRANS,
                               MatrixTransposeType.TRANS, 1.0, 0.0)
    elif transform_cols == feat_dim + 1:
        # Append the implicit 1.0 to the input feature.
        linear_part = SubMatrix(transform, 0, transform_rows, 0, feat_dim)
        feats_out.add_mat_mat_(feats, linear_part,
                               MatrixTransposeType.NO_TRANS,
                               MatrixTransposeType.TRANS, 1.0, 0.0)
        offset = Vector(transform_rows)
        offset.copy_col_from_mat_(transform, feat_dim)
        feats_out.add_vec_to_rows_(1.0, offset)
    else:
        logging.error(("Transform matrix has bad dimension %dx%d versus feat "
                       "dim %d") % (transform_rows, transform_cols, feat_dim))
    return feats_out