forf from HMMlib at http://www.cs.au.dk/~asand/?page_id=152