/* * wiigee - accelerometerbased gesture recognition * Copyright (C) 2007, 2008 Benjamin Poppinga * * Developed at University of Oldenburg * Contact: benjamin.poppinga@informatik.uni-oldenburg.de * * This file is part of wiigee. * * wiigee is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ package org.wiigee.logic; import java.text.DecimalFormat; import java.util.Vector; import org.wiigee.util.Log; /** * This is a Hidden Markov Model implementation which internally provides * the basic algorithms for training and recognition (forward and backward * algorithm). Since a regular Hidden Markov Model doesn't provide a possibility * to train multiple sequences, this implementation has been optimized for this * purposes using some state-of-the-art technologies described in several papers. * * @author Benjamin 'BePo' Poppinga * */ public class HMM { /** The number of states */ protected int numStates; /** The number of observations */ protected int numObservations; /** The initial probabilities for each state: p[state] */ protected double pi[]; /** The state change probability to switch from state A to * state B: a[stateA][stateB] */ protected double a[][]; /** The probability to emit symbol S in state A: b[stateA][symbolS] */ protected double b[][]; /** * Initialize the Hidden Markov Model in a left-to-right version. * * @param numStates Number of states * @param numObservations Number of observations */ public HMM(int numStates, int numObservations) { this.numStates = numStates; this.numObservations = numObservations; pi = new double[numStates]; a = new double[numStates][numStates]; b = new double[numStates][numObservations]; this.reset(); } /** * Reset the Hidden Markov Model to the initial left-to-right values. * */ private void reset() { int jumplimit = 2; // set startup probability pi[0] = 1; for(int i=1; ij-jumplimit-1) { a[i][j] = 1.0/(jumplimit+1); } else { a[i][j] = 0.0; } } } // emission probability for(int i=0; i