Replace tabs with spaces
This commit is contained in:
@@ -175,7 +175,7 @@ public class Device {
|
||||
|
||||
// don't need to create an event if filtered away
|
||||
if (vector != null) {
|
||||
// calculate the absolute value for the accelerationevent
|
||||
// calculate the absolute value for the accelerationevent
|
||||
double absvalue = Math.sqrt((vector[0] * vector[0]) +
|
||||
(vector[1] * vector[1]) + (vector[2] * vector[2]));
|
||||
|
||||
@@ -190,7 +190,7 @@ public class Device {
|
||||
|
||||
/** Fires a button pressed event.
|
||||
* @param button
|
||||
* Integer value of the pressed button.
|
||||
* Integer value of the pressed button.
|
||||
*/
|
||||
public void fireButtonPressedEvent(int button) {
|
||||
ButtonPressedEvent w = new ButtonPressedEvent(this, button);
|
||||
|
||||
@@ -38,42 +38,42 @@ import org.wiigee.device.*;
|
||||
*
|
||||
*/
|
||||
public class AccelerationEvent extends EventObject {
|
||||
|
||||
double X, Y, Z;
|
||||
double absvalue;
|
||||
|
||||
/**
|
||||
* Create an AccelerationEvent with a specific source,
|
||||
* all the three acceleration values and the calculated absolute
|
||||
* value.
|
||||
*
|
||||
* @param source The source which has been accelerated (Wiimote).
|
||||
* @param X The value of acceleration in the x direction.
|
||||
* @param Y The value of acceleration in the y direction.
|
||||
* @param Z The value of acceleration in the z direction.
|
||||
* @param absvalue The absolute value of this acceleration vector.
|
||||
*/
|
||||
public AccelerationEvent(Device source, double X, double Y, double Z, double absvalue) {
|
||||
super(source);
|
||||
this.X=X;
|
||||
this.Y=Y;
|
||||
this.Z=Z;
|
||||
this.absvalue=absvalue;
|
||||
}
|
||||
|
||||
double X, Y, Z;
|
||||
double absvalue;
|
||||
|
||||
/**
|
||||
* Create an AccelerationEvent with a specific source,
|
||||
* all the three acceleration values and the calculated absolute
|
||||
* value.
|
||||
*
|
||||
* @param source The source which has been accelerated (Wiimote).
|
||||
* @param X The value of acceleration in the x direction.
|
||||
* @param Y The value of acceleration in the y direction.
|
||||
* @param Z The value of acceleration in the z direction.
|
||||
* @param absvalue The absolute value of this acceleration vector.
|
||||
*/
|
||||
public AccelerationEvent(Device source, double X, double Y, double Z, double absvalue) {
|
||||
super(source);
|
||||
this.X=X;
|
||||
this.Y=Y;
|
||||
this.Z=Z;
|
||||
this.absvalue=absvalue;
|
||||
}
|
||||
|
||||
public double getX() {
|
||||
return X;
|
||||
}
|
||||
public double getX() {
|
||||
return X;
|
||||
}
|
||||
|
||||
public double getY() {
|
||||
return Y;
|
||||
}
|
||||
public double getY() {
|
||||
return Y;
|
||||
}
|
||||
|
||||
public double getZ() {
|
||||
return Z;
|
||||
}
|
||||
|
||||
public double getAbsValue() {
|
||||
return absvalue;
|
||||
}
|
||||
public double getZ() {
|
||||
return Z;
|
||||
}
|
||||
|
||||
public double getAbsValue() {
|
||||
return absvalue;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,32 +34,32 @@ import java.util.EventListener;
|
||||
* @author Benjamin 'BePo' Poppinga
|
||||
*/
|
||||
public interface AccelerationListener extends EventListener {
|
||||
|
||||
/**
|
||||
* This method would be called if a Device source has been accelerated.
|
||||
*
|
||||
* @param event The acceleration representation as an event.
|
||||
*/
|
||||
public abstract void accelerationReceived(AccelerationEvent event);
|
||||
|
||||
/**
|
||||
* This method would be called if a Device is in idle state and then a
|
||||
* motion starts or if a Device is in motion and then the motion stops and
|
||||
* the Device is in idle state.
|
||||
*
|
||||
* @param event This is the event which contains if the Wiimote is now
|
||||
* in motion or not.
|
||||
*/
|
||||
public abstract void motionStartReceived(MotionStartEvent event);
|
||||
|
||||
/**
|
||||
* This method would be called if a Device is in motion and then the motion
|
||||
* stops and the Device is in idle state.
|
||||
*
|
||||
* @param event This is the event which contains if the Device is now
|
||||
* in motion or not.
|
||||
*/
|
||||
public abstract void motionStopReceived(MotionStopEvent event);
|
||||
|
||||
|
||||
/**
|
||||
* This method would be called if a Device source has been accelerated.
|
||||
*
|
||||
* @param event The acceleration representation as an event.
|
||||
*/
|
||||
public abstract void accelerationReceived(AccelerationEvent event);
|
||||
|
||||
/**
|
||||
* This method would be called if a Device is in idle state and then a
|
||||
* motion starts or if a Device is in motion and then the motion stops and
|
||||
* the Device is in idle state.
|
||||
*
|
||||
* @param event This is the event which contains if the Wiimote is now
|
||||
* in motion or not.
|
||||
*/
|
||||
public abstract void motionStartReceived(MotionStartEvent event);
|
||||
|
||||
/**
|
||||
* This method would be called if a Device is in motion and then the motion
|
||||
* stops and the Device is in idle state.
|
||||
*
|
||||
* @param event This is the event which contains if the Device is now
|
||||
* in motion or not.
|
||||
*/
|
||||
public abstract void motionStopReceived(MotionStopEvent event);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -37,48 +37,48 @@ import org.wiigee.device.Device;
|
||||
*/
|
||||
public class ActionStartEvent extends EventObject {
|
||||
|
||||
protected boolean trainbutton;
|
||||
protected boolean recognitionbutton;
|
||||
protected boolean closegesturebutton;
|
||||
|
||||
public ActionStartEvent(Device source) {
|
||||
super(source);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is true if this button press has been done by the
|
||||
* individual defined RecognitionButton which has to be
|
||||
* set during initialization of a Wiimote.
|
||||
*
|
||||
* @return Is this button press initiated by the recognition button.
|
||||
* @see device.Wiimote#setRecognitionButton(int) setRecognitionButton()
|
||||
*/
|
||||
public boolean isRecognitionInitEvent() {
|
||||
return this.recognitionbutton;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is true if this button press has been done by the
|
||||
* individual defined TrainButton which has to be
|
||||
* set during initialization of a Wiimote.
|
||||
*
|
||||
* @return Is this button pres initiated by the training button.
|
||||
* @see device.Wiimote#setTrainButton(int) setTrainButton()
|
||||
*/
|
||||
public boolean isTrainInitEvent() {
|
||||
return this.trainbutton;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is true if this button press has been done by the
|
||||
* individual defined CloseGestureButton which has to be
|
||||
* set during initialization of a Wiimote.
|
||||
*
|
||||
* @return Is this button press initiated by the close gesture button.
|
||||
* @see device.Wiimote#setCloseGestureButton(int) setCloseGestureButton()
|
||||
*/
|
||||
public boolean isCloseGestureInitEvent() {
|
||||
return this.closegesturebutton;
|
||||
}
|
||||
protected boolean trainbutton;
|
||||
protected boolean recognitionbutton;
|
||||
protected boolean closegesturebutton;
|
||||
|
||||
public ActionStartEvent(Device source) {
|
||||
super(source);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is true if this button press has been done by the
|
||||
* individual defined RecognitionButton which has to be
|
||||
* set during initialization of a Wiimote.
|
||||
*
|
||||
* @return Is this button press initiated by the recognition button.
|
||||
* @see device.Wiimote#setRecognitionButton(int) setRecognitionButton()
|
||||
*/
|
||||
public boolean isRecognitionInitEvent() {
|
||||
return this.recognitionbutton;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is true if this button press has been done by the
|
||||
* individual defined TrainButton which has to be
|
||||
* set during initialization of a Wiimote.
|
||||
*
|
||||
* @return Is this button pres initiated by the training button.
|
||||
* @see device.Wiimote#setTrainButton(int) setTrainButton()
|
||||
*/
|
||||
public boolean isTrainInitEvent() {
|
||||
return this.trainbutton;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is true if this button press has been done by the
|
||||
* individual defined CloseGestureButton which has to be
|
||||
* set during initialization of a Wiimote.
|
||||
*
|
||||
* @return Is this button press initiated by the close gesture button.
|
||||
* @see device.Wiimote#setCloseGestureButton(int) setCloseGestureButton()
|
||||
*/
|
||||
public boolean isCloseGestureInitEvent() {
|
||||
return this.closegesturebutton;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -35,9 +35,9 @@ import org.wiigee.device.Device;
|
||||
* @author Benjamin 'BePo' Poppinga
|
||||
*/
|
||||
public class ActionStopEvent extends EventObject {
|
||||
|
||||
public ActionStopEvent(Device source) {
|
||||
super(source);
|
||||
}
|
||||
|
||||
public ActionStopEvent(Device source) {
|
||||
super(source);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -35,20 +35,20 @@ import java.util.EventListener;
|
||||
public interface ButtonListener extends EventListener {
|
||||
|
||||
|
||||
/**
|
||||
* This method would be called if a Device button has been pressed.
|
||||
*
|
||||
* @param event The button representation as an event.
|
||||
*/
|
||||
public abstract void buttonPressReceived(ButtonPressedEvent event);
|
||||
/**
|
||||
* This method would be called if a Device button has been pressed.
|
||||
*
|
||||
* @param event The button representation as an event.
|
||||
*/
|
||||
public abstract void buttonPressReceived(ButtonPressedEvent event);
|
||||
|
||||
/**
|
||||
* This method would be called if a Device button has been released.
|
||||
*
|
||||
* @param event This is actually a meta-event NOT containing which button
|
||||
* has been released.
|
||||
*/
|
||||
public abstract void buttonReleaseReceived(ButtonReleasedEvent event);
|
||||
/**
|
||||
* This method would be called if a Device button has been released.
|
||||
*
|
||||
* @param event This is actually a meta-event NOT containing which button
|
||||
* has been released.
|
||||
*/
|
||||
public abstract void buttonReleaseReceived(ButtonReleasedEvent event);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -37,44 +37,44 @@ import org.wiigee.device.*;
|
||||
* @author Benjamin 'BePo' Poppinga
|
||||
*/
|
||||
public class ButtonPressedEvent extends ActionStartEvent {
|
||||
|
||||
// Fixed number values.
|
||||
public static final int BUTTON_2 = 1;
|
||||
public static final int BUTTON_1 = 2;
|
||||
public static final int BUTTON_B = 3;
|
||||
public static final int BUTTON_A = 4;
|
||||
public static final int BUTTON_MINUS = 5;
|
||||
public static final int BUTTON_HOME = 8;
|
||||
public static final int BUTTON_LEFT = 9;
|
||||
public static final int BUTTON_RIGHT = 10;
|
||||
public static final int BUTTON_DOWN = 11;
|
||||
public static final int BUTTON_UP = 12;
|
||||
public static final int BUTTON_PLUS = 13;
|
||||
|
||||
int button;
|
||||
|
||||
// Fixed number values.
|
||||
public static final int BUTTON_2 = 1;
|
||||
public static final int BUTTON_1 = 2;
|
||||
public static final int BUTTON_B = 3;
|
||||
public static final int BUTTON_A = 4;
|
||||
public static final int BUTTON_MINUS = 5;
|
||||
public static final int BUTTON_HOME = 8;
|
||||
public static final int BUTTON_LEFT = 9;
|
||||
public static final int BUTTON_RIGHT = 10;
|
||||
public static final int BUTTON_DOWN = 11;
|
||||
public static final int BUTTON_UP = 12;
|
||||
public static final int BUTTON_PLUS = 13;
|
||||
|
||||
int button;
|
||||
|
||||
/**
|
||||
* Create a WiimoteButtonPressedEvent with the Wiimote source whose
|
||||
* Button has been pressed and the integer representation of the button.
|
||||
*
|
||||
* @param source
|
||||
* @param button
|
||||
*/
|
||||
public ButtonPressedEvent(Device source, int button) {
|
||||
super(source);
|
||||
this.button=button;
|
||||
|
||||
if(source.getRecognitionButton()==button) {
|
||||
this.recognitionbutton=true;
|
||||
} else if(source.getTrainButton()==button) {
|
||||
this.trainbutton=true;
|
||||
} else if(source.getCloseGestureButton()==button) {
|
||||
this.closegesturebutton=true;
|
||||
}
|
||||
}
|
||||
|
||||
public int getButton() {
|
||||
return this.button;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a WiimoteButtonPressedEvent with the Wiimote source whose
|
||||
* Button has been pressed and the integer representation of the button.
|
||||
*
|
||||
* @param source
|
||||
* @param button
|
||||
*/
|
||||
public ButtonPressedEvent(Device source, int button) {
|
||||
super(source);
|
||||
this.button=button;
|
||||
|
||||
if(source.getRecognitionButton()==button) {
|
||||
this.recognitionbutton=true;
|
||||
} else if(source.getTrainButton()==button) {
|
||||
this.trainbutton=true;
|
||||
} else if(source.getCloseGestureButton()==button) {
|
||||
this.closegesturebutton=true;
|
||||
}
|
||||
}
|
||||
|
||||
public int getButton() {
|
||||
return this.button;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -37,13 +37,13 @@ public class ButtonReleasedEvent extends ActionStopEvent {
|
||||
|
||||
int button;
|
||||
|
||||
public ButtonReleasedEvent(Device source, int button) {
|
||||
super(source);
|
||||
public ButtonReleasedEvent(Device source, int button) {
|
||||
super(source);
|
||||
this.button = button;
|
||||
}
|
||||
}
|
||||
|
||||
public int getButton() {
|
||||
return this.button;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -36,12 +36,12 @@ import java.util.EventListener;
|
||||
*/
|
||||
public interface GestureListener extends EventListener {
|
||||
|
||||
/**
|
||||
* This method would be called if a gesture has been recognized.
|
||||
*
|
||||
* @param event The GestureEvent containing information about
|
||||
* the recognized gesture.
|
||||
*/
|
||||
public abstract void gestureReceived(GestureEvent event);
|
||||
|
||||
/**
|
||||
* This method would be called if a gesture has been recognized.
|
||||
*
|
||||
* @param event The GestureEvent containing information about
|
||||
* the recognized gesture.
|
||||
*/
|
||||
public abstract void gestureReceived(GestureEvent event);
|
||||
|
||||
}
|
||||
|
||||
@@ -32,32 +32,32 @@ import org.wiigee.device.Device;
|
||||
* @author Benjamin 'BePo' Poppinga
|
||||
*/
|
||||
public class MotionStartEvent extends ActionStartEvent {
|
||||
|
||||
public MotionStartEvent(Device source) {
|
||||
super(source);
|
||||
|
||||
if(source.getRecognitionButton()==Device.MOTION) {
|
||||
this.recognitionbutton=true;
|
||||
} else if(source.getTrainButton()==Device.MOTION) {
|
||||
this.trainbutton=true;
|
||||
} else if(source.getCloseGestureButton()==Device.MOTION) {
|
||||
this.closegesturebutton=true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTrainInitEvent() {
|
||||
return this.trainbutton;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCloseGestureInitEvent() {
|
||||
return this.closegesturebutton;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRecognitionInitEvent() {
|
||||
return this.recognitionbutton;
|
||||
}
|
||||
|
||||
public MotionStartEvent(Device source) {
|
||||
super(source);
|
||||
|
||||
if(source.getRecognitionButton()==Device.MOTION) {
|
||||
this.recognitionbutton=true;
|
||||
} else if(source.getTrainButton()==Device.MOTION) {
|
||||
this.trainbutton=true;
|
||||
} else if(source.getCloseGestureButton()==Device.MOTION) {
|
||||
this.closegesturebutton=true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTrainInitEvent() {
|
||||
return this.trainbutton;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCloseGestureInitEvent() {
|
||||
return this.closegesturebutton;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRecognitionInitEvent() {
|
||||
return this.recognitionbutton;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -35,8 +35,8 @@ import org.wiigee.device.Device;
|
||||
*/
|
||||
public class MotionStopEvent extends ActionStopEvent {
|
||||
|
||||
public MotionStopEvent(Device source) {
|
||||
super(source);
|
||||
}
|
||||
|
||||
public MotionStopEvent(Device source) {
|
||||
super(source);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -31,40 +31,40 @@ package org.wiigee.filter;
|
||||
* @author Benjamin 'BePo' Poppinga
|
||||
*/
|
||||
public class DirectionalEquivalenceFilter extends Filter {
|
||||
|
||||
private double sensivity;
|
||||
private double[] reference;
|
||||
|
||||
private double sensivity;
|
||||
private double[] reference;
|
||||
|
||||
public DirectionalEquivalenceFilter() {
|
||||
super();
|
||||
this.reset();
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
this.sensivity=0.2;
|
||||
this.reference = new double[] {0.0, 0.0, 0.0};
|
||||
}
|
||||
|
||||
public double[] filterAlgorithm(double[] vector) {
|
||||
if(vector[0]<reference[0]-this.sensivity ||
|
||||
vector[0]>reference[0]+this.sensivity ||
|
||||
vector[1]<reference[1]-this.sensivity ||
|
||||
vector[1]>reference[1]+this.sensivity ||
|
||||
vector[2]<reference[2]-this.sensivity ||
|
||||
vector[2]>reference[2]+this.sensivity) {
|
||||
this.reference=vector;
|
||||
return vector;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void setSensivity(double sensivity) {
|
||||
this.sensivity=sensivity;
|
||||
}
|
||||
|
||||
public double getSensivity() {
|
||||
return this.sensivity;
|
||||
}
|
||||
public DirectionalEquivalenceFilter() {
|
||||
super();
|
||||
this.reset();
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
this.sensivity=0.2;
|
||||
this.reference = new double[] {0.0, 0.0, 0.0};
|
||||
}
|
||||
|
||||
public double[] filterAlgorithm(double[] vector) {
|
||||
if(vector[0]<reference[0]-this.sensivity ||
|
||||
vector[0]>reference[0]+this.sensivity ||
|
||||
vector[1]<reference[1]-this.sensivity ||
|
||||
vector[1]>reference[1]+this.sensivity ||
|
||||
vector[2]<reference[2]-this.sensivity ||
|
||||
vector[2]>reference[2]+this.sensivity) {
|
||||
this.reference=vector;
|
||||
return vector;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void setSensivity(double sensivity) {
|
||||
this.sensivity=sensivity;
|
||||
}
|
||||
|
||||
public double getSensivity() {
|
||||
return this.sensivity;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,33 +30,33 @@ package org.wiigee.filter;
|
||||
* @author Benjamin 'BePo' Poppinga
|
||||
*/
|
||||
public abstract class Filter {
|
||||
|
||||
/***
|
||||
* The actual called method to filter anything. It checks if the vector is
|
||||
* already set to NULL by another filter and won't process it anymore. If it's
|
||||
* not NULL it would be forwarded to the actual implemented method - filterAlgorithm().
|
||||
* @param vector The acceleration vector, encoding: 0/x, 1/y, 2/z
|
||||
* @return a new, filtered acceleration vector, encoded the same way
|
||||
*/
|
||||
public double[] filter(double[] vector) {
|
||||
if(vector==null) {
|
||||
return null;
|
||||
} else {
|
||||
return filterAlgorithm(vector);
|
||||
}
|
||||
}
|
||||
|
||||
/***
|
||||
* A filter receives a triple of acceleration values within the variable 'vector'.
|
||||
* It's encoded as vector[0]=x, vector[1]=y, vector[2]=z. This is not an object since the
|
||||
* processing of the filter should be really fast, since every acceleration of the wiimote
|
||||
* passes the filter.
|
||||
* @param vector
|
||||
* @param absvalue
|
||||
* @return
|
||||
*/
|
||||
abstract public double[] filterAlgorithm(double[] vector);
|
||||
|
||||
/***
|
||||
* The actual called method to filter anything. It checks if the vector is
|
||||
* already set to NULL by another filter and won't process it anymore. If it's
|
||||
* not NULL it would be forwarded to the actual implemented method - filterAlgorithm().
|
||||
* @param vector The acceleration vector, encoding: 0/x, 1/y, 2/z
|
||||
* @return a new, filtered acceleration vector, encoded the same way
|
||||
*/
|
||||
public double[] filter(double[] vector) {
|
||||
if(vector==null) {
|
||||
return null;
|
||||
} else {
|
||||
return filterAlgorithm(vector);
|
||||
}
|
||||
}
|
||||
|
||||
/***
|
||||
* A filter receives a triple of acceleration values within the variable 'vector'.
|
||||
* It's encoded as vector[0]=x, vector[1]=y, vector[2]=z. This is not an object since the
|
||||
* processing of the filter should be really fast, since every acceleration of the wiimote
|
||||
* passes the filter.
|
||||
* @param vector
|
||||
* @param absvalue
|
||||
* @return
|
||||
*/
|
||||
abstract public double[] filterAlgorithm(double[] vector);
|
||||
|
||||
abstract public void reset();
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -31,18 +31,18 @@ package org.wiigee.filter;
|
||||
* @author Benjamin 'BePo' Poppinga
|
||||
*/
|
||||
public class IdleStateFilter extends Filter {
|
||||
|
||||
private double sensivity;
|
||||
|
||||
/**
|
||||
* Since an acceleration sensor usually provides information even
|
||||
* if it doesn't move, this filter removes the data if it's in the
|
||||
* idle state.
|
||||
*/
|
||||
public IdleStateFilter() {
|
||||
super();
|
||||
this.sensivity = 0.1;
|
||||
}
|
||||
|
||||
private double sensivity;
|
||||
|
||||
/**
|
||||
* Since an acceleration sensor usually provides information even
|
||||
* if it doesn't move, this filter removes the data if it's in the
|
||||
* idle state.
|
||||
*/
|
||||
public IdleStateFilter() {
|
||||
super();
|
||||
this.sensivity = 0.1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
@@ -50,37 +50,37 @@ public class IdleStateFilter extends Filter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public double[] filterAlgorithm(double[] vector) {
|
||||
// calculate values needed for filtering:
|
||||
// absolute value
|
||||
double absvalue = Math.sqrt((vector[0]*vector[0])+
|
||||
(vector[1]*vector[1])+(vector[2]*vector[2]));
|
||||
|
||||
// filter formulaes and return values
|
||||
if(absvalue > 1+this.sensivity ||
|
||||
absvalue < 1-this.sensivity) {
|
||||
return vector;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the absolute value when the wiimote should react to acceleration.
|
||||
* This is a parameter for the first of the two filters: idle state
|
||||
* filter. For example: sensivity=0.2 makes the wiimote react to acceleration
|
||||
* where the absolute value is equal or greater than 1.2g. The default value 0.1
|
||||
* should work well. Only change if you are sure what you're doing.
|
||||
*
|
||||
* @param sensivity
|
||||
* acceleration data values smaller than this value wouldn't be detected.
|
||||
*/
|
||||
public void setSensivity(double sensivity) {
|
||||
this.sensivity = sensivity;
|
||||
}
|
||||
|
||||
public double getSensivity() {
|
||||
return this.sensivity;
|
||||
}
|
||||
public double[] filterAlgorithm(double[] vector) {
|
||||
// calculate values needed for filtering:
|
||||
// absolute value
|
||||
double absvalue = Math.sqrt((vector[0]*vector[0])+
|
||||
(vector[1]*vector[1])+(vector[2]*vector[2]));
|
||||
|
||||
// filter formulaes and return values
|
||||
if(absvalue > 1+this.sensivity ||
|
||||
absvalue < 1-this.sensivity) {
|
||||
return vector;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the absolute value when the wiimote should react to acceleration.
|
||||
* This is a parameter for the first of the two filters: idle state
|
||||
* filter. For example: sensivity=0.2 makes the wiimote react to acceleration
|
||||
* where the absolute value is equal or greater than 1.2g. The default value 0.1
|
||||
* should work well. Only change if you are sure what you're doing.
|
||||
*
|
||||
* @param sensivity
|
||||
* acceleration data values smaller than this value wouldn't be detected.
|
||||
*/
|
||||
public void setSensivity(double sensivity) {
|
||||
this.sensivity = sensivity;
|
||||
}
|
||||
|
||||
public double getSensivity() {
|
||||
return this.sensivity;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -34,69 +34,69 @@ import org.wiigee.device.Device;
|
||||
*/
|
||||
public class MotionDetectFilter extends Filter {
|
||||
|
||||
private int motionchangetime;
|
||||
private boolean nowinmotion;
|
||||
private long motionstartstamp;
|
||||
private Device device;
|
||||
|
||||
/***
|
||||
* Detects wheather the wiimote receives acceleration or not and
|
||||
* raises an event, if the device starts or stops. This is actual a
|
||||
* null filter, not manipulating anything. But looks pretty good in
|
||||
* this datatype since it could be removed easily.
|
||||
*
|
||||
* @param wiimote The Wiimote object which is controlled by the filter.
|
||||
*/
|
||||
public MotionDetectFilter(Device device) {
|
||||
super();
|
||||
this.device=device;
|
||||
this.reset();
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
this.motionstartstamp=System.currentTimeMillis();
|
||||
this.nowinmotion=false;
|
||||
this.motionchangetime=190;
|
||||
}
|
||||
|
||||
private int motionchangetime;
|
||||
private boolean nowinmotion;
|
||||
private long motionstartstamp;
|
||||
private Device device;
|
||||
|
||||
/***
|
||||
* Detects wheather the wiimote receives acceleration or not and
|
||||
* raises an event, if the device starts or stops. This is actual a
|
||||
* null filter, not manipulating anything. But looks pretty good in
|
||||
* this datatype since it could be removed easily.
|
||||
*
|
||||
* @param wiimote The Wiimote object which is controlled by the filter.
|
||||
*/
|
||||
public MotionDetectFilter(Device device) {
|
||||
super();
|
||||
this.device=device;
|
||||
this.reset();
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
this.motionstartstamp=System.currentTimeMillis();
|
||||
this.nowinmotion=false;
|
||||
this.motionchangetime=190;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double[] filter(double[] vector) {
|
||||
|
||||
if(this.nowinmotion &&
|
||||
(System.currentTimeMillis()-this.motionstartstamp)>=
|
||||
this.motionchangetime) {
|
||||
this.nowinmotion=false;
|
||||
this.device.fireMotionStopEvent();
|
||||
} // fi
|
||||
|
||||
return filterAlgorithm(vector);
|
||||
}
|
||||
|
||||
public double[] filterAlgorithm(double[] vector) {
|
||||
if(vector!=null) {
|
||||
this.motionstartstamp=System.currentTimeMillis();
|
||||
if(!this.nowinmotion) {
|
||||
this.nowinmotion=true;
|
||||
this.motionstartstamp=System.currentTimeMillis();
|
||||
this.device.fireMotionStartEvent();
|
||||
}
|
||||
}
|
||||
|
||||
return vector;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the time the wiimote has to be in idle state before a new motion change
|
||||
* event appears. The default value 500ms should work well, only change it if you are sure
|
||||
* about what you're doing.
|
||||
* @param time Time in ms
|
||||
*/
|
||||
public void setMotionChangeTime(int time) {
|
||||
this.motionchangetime=time;
|
||||
}
|
||||
|
||||
public int getMotionChangeTime() {
|
||||
return this.motionchangetime;
|
||||
}
|
||||
public double[] filter(double[] vector) {
|
||||
|
||||
if(this.nowinmotion &&
|
||||
(System.currentTimeMillis()-this.motionstartstamp)>=
|
||||
this.motionchangetime) {
|
||||
this.nowinmotion=false;
|
||||
this.device.fireMotionStopEvent();
|
||||
} // fi
|
||||
|
||||
return filterAlgorithm(vector);
|
||||
}
|
||||
|
||||
public double[] filterAlgorithm(double[] vector) {
|
||||
if(vector!=null) {
|
||||
this.motionstartstamp=System.currentTimeMillis();
|
||||
if(!this.nowinmotion) {
|
||||
this.nowinmotion=true;
|
||||
this.motionstartstamp=System.currentTimeMillis();
|
||||
this.device.fireMotionStartEvent();
|
||||
}
|
||||
}
|
||||
|
||||
return vector;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the time the wiimote has to be in idle state before a new motion change
|
||||
* event appears. The default value 500ms should work well, only change it if you are sure
|
||||
* about what you're doing.
|
||||
* @param time Time in ms
|
||||
*/
|
||||
public void setMotionChangeTime(int time) {
|
||||
this.motionchangetime=time;
|
||||
}
|
||||
|
||||
public int getMotionChangeTime() {
|
||||
return this.motionchangetime;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,82 +28,82 @@ import java.util.Vector;
|
||||
|
||||
public class Classifier {
|
||||
|
||||
private Vector<GestureModel> gesturemodel; // each gesturetype got its own
|
||||
// gesturemodel in this vector
|
||||
private double lastprob;
|
||||
|
||||
public Classifier() {
|
||||
this.gesturemodel=new Vector<GestureModel>();
|
||||
this.lastprob=0.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method recognize a specific gesture, given to the procedure.
|
||||
* For classification a bayes classification algorithm is used.
|
||||
*
|
||||
* @param g gesture to classify
|
||||
*/
|
||||
public int classifyGesture(Gesture g) {
|
||||
//Log.write("Recognizing gesture...");
|
||||
|
||||
// Wert im Nenner berechnen, nach Bayes
|
||||
double sum = 0;
|
||||
for(int i=0; i<this.gesturemodel.size(); i++) {
|
||||
sum+=this.gesturemodel.elementAt(i).getDefaultProbability()*
|
||||
this.gesturemodel.elementAt(i).matches(g);
|
||||
}
|
||||
|
||||
int recognized = -1; // which gesture has been recognized
|
||||
double recogprob = Integer.MIN_VALUE; // probability of this gesture
|
||||
double probgesture = 0; // temporal value for bayes algorithm
|
||||
double probmodel = 0; // temporal value for bayes algorithm
|
||||
for(int i=0; i<this.gesturemodel.size(); i++) {
|
||||
//this.gesturemodel.elementAt(i).print(); // Debug
|
||||
double tmpgesture = this.gesturemodel.elementAt(i).matches(g);
|
||||
double tmpmodel = this.gesturemodel.elementAt(i).getDefaultProbability();
|
||||
|
||||
if(((tmpmodel*tmpgesture)/sum)>recogprob) {
|
||||
probgesture=tmpgesture;
|
||||
probmodel=tmpmodel;
|
||||
recogprob=((tmpmodel*tmpgesture)/sum);
|
||||
recognized=i;
|
||||
}
|
||||
}
|
||||
|
||||
// a gesture could be recognized
|
||||
if(recogprob>0 && probmodel>0 && probgesture>0 && sum>0) {
|
||||
this.lastprob=recogprob;
|
||||
return recognized;
|
||||
} else {
|
||||
// no gesture could be recognized
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public double getLastProbability() {
|
||||
return this.lastprob;
|
||||
}
|
||||
|
||||
public void addGestureModel(GestureModel gm) {
|
||||
this.gesturemodel.add(gm);
|
||||
}
|
||||
private Vector<GestureModel> gesturemodel; // each gesturetype got its own
|
||||
// gesturemodel in this vector
|
||||
private double lastprob;
|
||||
|
||||
public Classifier() {
|
||||
this.gesturemodel=new Vector<GestureModel>();
|
||||
this.lastprob=0.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method recognize a specific gesture, given to the procedure.
|
||||
* For classification a bayes classification algorithm is used.
|
||||
*
|
||||
* @param g gesture to classify
|
||||
*/
|
||||
public int classifyGesture(Gesture g) {
|
||||
//Log.write("Recognizing gesture...");
|
||||
|
||||
// Wert im Nenner berechnen, nach Bayes
|
||||
double sum = 0;
|
||||
for(int i=0; i<this.gesturemodel.size(); i++) {
|
||||
sum+=this.gesturemodel.elementAt(i).getDefaultProbability()*
|
||||
this.gesturemodel.elementAt(i).matches(g);
|
||||
}
|
||||
|
||||
int recognized = -1; // which gesture has been recognized
|
||||
double recogprob = Integer.MIN_VALUE; // probability of this gesture
|
||||
double probgesture = 0; // temporal value for bayes algorithm
|
||||
double probmodel = 0; // temporal value for bayes algorithm
|
||||
for(int i=0; i<this.gesturemodel.size(); i++) {
|
||||
//this.gesturemodel.elementAt(i).print(); // Debug
|
||||
double tmpgesture = this.gesturemodel.elementAt(i).matches(g);
|
||||
double tmpmodel = this.gesturemodel.elementAt(i).getDefaultProbability();
|
||||
|
||||
if(((tmpmodel*tmpgesture)/sum)>recogprob) {
|
||||
probgesture=tmpgesture;
|
||||
probmodel=tmpmodel;
|
||||
recogprob=((tmpmodel*tmpgesture)/sum);
|
||||
recognized=i;
|
||||
}
|
||||
}
|
||||
|
||||
// a gesture could be recognized
|
||||
if(recogprob>0 && probmodel>0 && probgesture>0 && sum>0) {
|
||||
this.lastprob=recogprob;
|
||||
return recognized;
|
||||
} else {
|
||||
// no gesture could be recognized
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public double getLastProbability() {
|
||||
return this.lastprob;
|
||||
}
|
||||
|
||||
public void addGestureModel(GestureModel gm) {
|
||||
this.gesturemodel.add(gm);
|
||||
}
|
||||
|
||||
public GestureModel getGestureModel(int id) {
|
||||
return this.gesturemodel.elementAt(id);
|
||||
}
|
||||
|
||||
public Vector<GestureModel> getGestureModels() {
|
||||
return this.gesturemodel;
|
||||
}
|
||||
public GestureModel getGestureModel(int id) {
|
||||
return this.gesturemodel.elementAt(id);
|
||||
}
|
||||
|
||||
public Vector<GestureModel> getGestureModels() {
|
||||
return this.gesturemodel;
|
||||
}
|
||||
|
||||
public int getCountOfGestures() {
|
||||
return this.gesturemodel.size();
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
this.gesturemodel = new Vector<GestureModel>();
|
||||
}
|
||||
|
||||
|
||||
public void clear() {
|
||||
this.gesturemodel = new Vector<GestureModel>();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -36,120 +36,120 @@ import org.wiigee.event.AccelerationEvent;
|
||||
|
||||
public class Gesture implements Cloneable {
|
||||
|
||||
/** Min/MaxAcceleration setup manually? */
|
||||
private boolean minmaxmanual;
|
||||
private double minacc;
|
||||
private double maxacc;
|
||||
|
||||
/** The complete trajectory as WiimoteAccelerationEvents
|
||||
* as a vector. It's a vector because we don't want to
|
||||
* loose the chronology of the stored events.
|
||||
*/
|
||||
private Vector<AccelerationEvent> data;
|
||||
/** Min/MaxAcceleration setup manually? */
|
||||
private boolean minmaxmanual;
|
||||
private double minacc;
|
||||
private double maxacc;
|
||||
|
||||
/** The complete trajectory as WiimoteAccelerationEvents
|
||||
* as a vector. It's a vector because we don't want to
|
||||
* loose the chronology of the stored events.
|
||||
*/
|
||||
private Vector<AccelerationEvent> data;
|
||||
|
||||
/**
|
||||
* Create an empty Gesture.
|
||||
*/
|
||||
public Gesture() {
|
||||
this.data = new Vector<AccelerationEvent>();
|
||||
}
|
||||
/**
|
||||
* Create an empty Gesture.
|
||||
*/
|
||||
public Gesture() {
|
||||
this.data = new Vector<AccelerationEvent>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a deep copy of another Gesture object.
|
||||
*
|
||||
* @param original Another Gesture object
|
||||
*/
|
||||
public Gesture(Gesture original) {
|
||||
this.data = new Vector<AccelerationEvent>();
|
||||
Vector<AccelerationEvent> origin = original.getData();
|
||||
for (int i = 0; i < origin.size(); i++) {
|
||||
this.add((AccelerationEvent) origin.get(i));
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Make a deep copy of another Gesture object.
|
||||
*
|
||||
* @param original Another Gesture object
|
||||
*/
|
||||
public Gesture(Gesture original) {
|
||||
this.data = new Vector<AccelerationEvent>();
|
||||
Vector<AccelerationEvent> origin = original.getData();
|
||||
for (int i = 0; i < origin.size(); i++) {
|
||||
this.add((AccelerationEvent) origin.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds a new acceleration event to this gesture.
|
||||
*
|
||||
* @param event The WiimoteAccelerationEvent to add.
|
||||
*/
|
||||
public void add(AccelerationEvent event) {
|
||||
this.data.add(event);
|
||||
}
|
||||
/**
|
||||
* Adds a new acceleration event to this gesture.
|
||||
*
|
||||
* @param event The WiimoteAccelerationEvent to add.
|
||||
*/
|
||||
public void add(AccelerationEvent event) {
|
||||
this.data.add(event);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last acceleration added to this gesture.
|
||||
*
|
||||
* @return the last acceleration event added.
|
||||
*/
|
||||
public AccelerationEvent getLastData() {
|
||||
return (AccelerationEvent) this.data.get(this.data.size() - 1);
|
||||
}
|
||||
/**
|
||||
* Returns the last acceleration added to this gesture.
|
||||
*
|
||||
* @return the last acceleration event added.
|
||||
*/
|
||||
public AccelerationEvent getLastData() {
|
||||
return (AccelerationEvent) this.data.get(this.data.size() - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the whole chronological sequence of accelerations as
|
||||
* a vector.
|
||||
*
|
||||
* @return chronological sequence of accelerations.
|
||||
*/
|
||||
public Vector<AccelerationEvent> getData() {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the first element of the acceleration queue of a gesture
|
||||
*/
|
||||
public void removeFirstData() {
|
||||
this.data.remove(0);
|
||||
}
|
||||
|
||||
public int getCountOfData() {
|
||||
return this.data.size();
|
||||
}
|
||||
|
||||
public void setMaxAndMinAcceleration(double max, double min) {
|
||||
this.maxacc = max;
|
||||
this.minacc = min;
|
||||
this.minmaxmanual = true;
|
||||
}
|
||||
|
||||
public double getMaxAcceleration() {
|
||||
if(!this.minmaxmanual) {
|
||||
double maxacc = Double.MIN_VALUE;
|
||||
for(int i=0; i<this.data.size(); i++) {
|
||||
if(Math.abs(this.data.get(i).getX()) > maxacc) {
|
||||
maxacc=Math.abs(this.data.get(i).getX());
|
||||
}
|
||||
if(Math.abs(this.data.get(i).getY()) > maxacc) {
|
||||
maxacc=Math.abs(this.data.get(i).getY());
|
||||
}
|
||||
if(Math.abs(this.data.get(i).getZ()) > maxacc) {
|
||||
maxacc=Math.abs(this.data.get(i).getZ());
|
||||
}
|
||||
}
|
||||
return maxacc;
|
||||
} else {
|
||||
return this.maxacc;
|
||||
}
|
||||
}
|
||||
|
||||
public double getMinAcceleration() {
|
||||
if(!this.minmaxmanual) {
|
||||
double minacc = Double.MAX_VALUE;
|
||||
for(int i=0; i<this.data.size(); i++) {
|
||||
if(Math.abs(this.data.get(i).getX()) < minacc) {
|
||||
minacc=Math.abs(this.data.get(i).getX());
|
||||
}
|
||||
if(Math.abs(this.data.get(i).getY()) < minacc) {
|
||||
minacc=Math.abs(this.data.get(i).getY());
|
||||
}
|
||||
if(Math.abs(this.data.get(i).getZ()) < minacc) {
|
||||
minacc=Math.abs(this.data.get(i).getZ());
|
||||
}
|
||||
}
|
||||
return minacc;
|
||||
} else {
|
||||
return this.minacc;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Returns the whole chronological sequence of accelerations as
|
||||
* a vector.
|
||||
*
|
||||
* @return chronological sequence of accelerations.
|
||||
*/
|
||||
public Vector<AccelerationEvent> getData() {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the first element of the acceleration queue of a gesture
|
||||
*/
|
||||
public void removeFirstData() {
|
||||
this.data.remove(0);
|
||||
}
|
||||
|
||||
public int getCountOfData() {
|
||||
return this.data.size();
|
||||
}
|
||||
|
||||
public void setMaxAndMinAcceleration(double max, double min) {
|
||||
this.maxacc = max;
|
||||
this.minacc = min;
|
||||
this.minmaxmanual = true;
|
||||
}
|
||||
|
||||
public double getMaxAcceleration() {
|
||||
if(!this.minmaxmanual) {
|
||||
double maxacc = Double.MIN_VALUE;
|
||||
for(int i=0; i<this.data.size(); i++) {
|
||||
if(Math.abs(this.data.get(i).getX()) > maxacc) {
|
||||
maxacc=Math.abs(this.data.get(i).getX());
|
||||
}
|
||||
if(Math.abs(this.data.get(i).getY()) > maxacc) {
|
||||
maxacc=Math.abs(this.data.get(i).getY());
|
||||
}
|
||||
if(Math.abs(this.data.get(i).getZ()) > maxacc) {
|
||||
maxacc=Math.abs(this.data.get(i).getZ());
|
||||
}
|
||||
}
|
||||
return maxacc;
|
||||
} else {
|
||||
return this.maxacc;
|
||||
}
|
||||
}
|
||||
|
||||
public double getMinAcceleration() {
|
||||
if(!this.minmaxmanual) {
|
||||
double minacc = Double.MAX_VALUE;
|
||||
for(int i=0; i<this.data.size(); i++) {
|
||||
if(Math.abs(this.data.get(i).getX()) < minacc) {
|
||||
minacc=Math.abs(this.data.get(i).getX());
|
||||
}
|
||||
if(Math.abs(this.data.get(i).getY()) < minacc) {
|
||||
minacc=Math.abs(this.data.get(i).getY());
|
||||
}
|
||||
if(Math.abs(this.data.get(i).getZ()) < minacc) {
|
||||
minacc=Math.abs(this.data.get(i).getZ());
|
||||
}
|
||||
}
|
||||
return minacc;
|
||||
} else {
|
||||
return this.minacc;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,170 +36,170 @@ import org.wiigee.util.Log;
|
||||
* @author Benjamin 'BePo' Poppinga
|
||||
*/
|
||||
public class GestureModel {
|
||||
|
||||
/** The number of states the hidden markov model consists of */
|
||||
private int numStates;
|
||||
|
||||
/** The number of observations for the hmm and k-mean */
|
||||
private int numObservations;
|
||||
|
||||
/** The quantization component */
|
||||
private Quantizer quantizer;
|
||||
|
||||
/** The statistical model, hidden markov model */
|
||||
private HMM markovmodell;
|
||||
|
||||
/** The number of states the hidden markov model consists of */
|
||||
private int numStates;
|
||||
|
||||
/** The number of observations for the hmm and k-mean */
|
||||
private int numObservations;
|
||||
|
||||
/** The quantization component */
|
||||
private Quantizer quantizer;
|
||||
|
||||
/** The statistical model, hidden markov model */
|
||||
private HMM markovmodell;
|
||||
|
||||
/** The default probability of this gesturemodel,
|
||||
* needed for the bayes classifier */
|
||||
private double defaultprobability;
|
||||
/** The default probability of this gesturemodel,
|
||||
* needed for the bayes classifier */
|
||||
private double defaultprobability;
|
||||
|
||||
|
||||
/** Creates a Unit (Quantizer&Model).
|
||||
*
|
||||
* @param id
|
||||
* int representation of a gesture "name"/class.
|
||||
*/
|
||||
public GestureModel() {
|
||||
this.numStates=8; // n=8 states empirical value
|
||||
this.numObservations=14; // k=14 observations empirical value
|
||||
this.markovmodell = new HMM(numStates, numObservations); // init model
|
||||
this.quantizer = new Quantizer(numStates); // init quantizer
|
||||
}
|
||||
|
||||
/** Creates a Unit (Quantizer&Model).
|
||||
*
|
||||
* @param id
|
||||
* int representation of a gesture "name"/class.
|
||||
*/
|
||||
public GestureModel() {
|
||||
this.numStates=8; // n=8 states empirical value
|
||||
this.numObservations=14; // k=14 observations empirical value
|
||||
this.markovmodell = new HMM(numStates, numObservations); // init model
|
||||
this.quantizer = new Quantizer(numStates); // init quantizer
|
||||
}
|
||||
|
||||
/**
|
||||
* Trains the model to a set of motion-sequences, representing
|
||||
* different evaluations of a gesture
|
||||
*
|
||||
* @param trainsequence a vector of gestures
|
||||
*/
|
||||
public void train(Vector<Gesture> trainsequence) {
|
||||
// summarize all vectors from the different gestures in one
|
||||
// gesture called sum.
|
||||
double maxacc=0;
|
||||
double minacc=0;
|
||||
Gesture sum = new Gesture();
|
||||
|
||||
for(int i=0; i<trainsequence.size(); i++) {
|
||||
Vector<AccelerationEvent> t = trainsequence.elementAt(i).getData();
|
||||
/**
|
||||
* Trains the model to a set of motion-sequences, representing
|
||||
* different evaluations of a gesture
|
||||
*
|
||||
* @param trainsequence a vector of gestures
|
||||
*/
|
||||
public void train(Vector<Gesture> trainsequence) {
|
||||
// summarize all vectors from the different gestures in one
|
||||
// gesture called sum.
|
||||
double maxacc=0;
|
||||
double minacc=0;
|
||||
Gesture sum = new Gesture();
|
||||
|
||||
for(int i=0; i<trainsequence.size(); i++) {
|
||||
Vector<AccelerationEvent> t = trainsequence.elementAt(i).getData();
|
||||
|
||||
// add the max and min acceleration, we later get the average
|
||||
maxacc+=trainsequence.elementAt(i).getMaxAcceleration();
|
||||
minacc+=trainsequence.elementAt(i).getMinAcceleration();
|
||||
|
||||
// transfer every single accelerationevent of each gesture to
|
||||
// the new gesture sum
|
||||
for(int j=0; j<trainsequence.elementAt(i).getData().size(); j++) {
|
||||
sum.add(t.elementAt(j));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// get the average and set it to the sum gesture
|
||||
sum.setMaxAndMinAcceleration(maxacc/trainsequence.size(), minacc/trainsequence.size());
|
||||
|
||||
// train the centeroids of the quantizer with this master gesture sum.
|
||||
this.quantizer.trainCenteroids(sum);
|
||||
|
||||
// convert gesture vector to a sequence of discrete values
|
||||
Vector<int[]> seqs = new Vector<int[]>();
|
||||
for(int i=0; i<trainsequence.size(); i++) {
|
||||
seqs.add(this.quantizer.getObservationSequence(trainsequence.elementAt(i)));
|
||||
}
|
||||
|
||||
// train the markov model with this derived discrete sequences
|
||||
this.markovmodell.train(seqs);
|
||||
|
||||
// set the default probability for use with the bayes classifier
|
||||
this.setDefaultProbability(trainsequence);
|
||||
}
|
||||
// add the max and min acceleration, we later get the average
|
||||
maxacc+=trainsequence.elementAt(i).getMaxAcceleration();
|
||||
minacc+=trainsequence.elementAt(i).getMinAcceleration();
|
||||
|
||||
// transfer every single accelerationevent of each gesture to
|
||||
// the new gesture sum
|
||||
for(int j=0; j<trainsequence.elementAt(i).getData().size(); j++) {
|
||||
sum.add(t.elementAt(j));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// get the average and set it to the sum gesture
|
||||
sum.setMaxAndMinAcceleration(maxacc/trainsequence.size(), minacc/trainsequence.size());
|
||||
|
||||
// train the centeroids of the quantizer with this master gesture sum.
|
||||
this.quantizer.trainCenteroids(sum);
|
||||
|
||||
// convert gesture vector to a sequence of discrete values
|
||||
Vector<int[]> seqs = new Vector<int[]>();
|
||||
for(int i=0; i<trainsequence.size(); i++) {
|
||||
seqs.add(this.quantizer.getObservationSequence(trainsequence.elementAt(i)));
|
||||
}
|
||||
|
||||
// train the markov model with this derived discrete sequences
|
||||
this.markovmodell.train(seqs);
|
||||
|
||||
// set the default probability for use with the bayes classifier
|
||||
this.setDefaultProbability(trainsequence);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the probability that a gesture matches to this
|
||||
* gesture model.
|
||||
*
|
||||
* @param gesture a gesture to test.
|
||||
* @return probability that the gesture belongs to this gesture
|
||||
* model.
|
||||
*/
|
||||
public double matches(Gesture gesture) {
|
||||
int[] sequence = quantizer.getObservationSequence(gesture);
|
||||
return this.markovmodell.getProbability(sequence);
|
||||
}
|
||||
/**
|
||||
* Returns the probability that a gesture matches to this
|
||||
* gesture model.
|
||||
*
|
||||
* @param gesture a gesture to test.
|
||||
* @return probability that the gesture belongs to this gesture
|
||||
* model.
|
||||
*/
|
||||
public double matches(Gesture gesture) {
|
||||
int[] sequence = quantizer.getObservationSequence(gesture);
|
||||
return this.markovmodell.getProbability(sequence);
|
||||
}
|
||||
|
||||
/**
|
||||
* For debug purposes or very technical interested people. :)
|
||||
*/
|
||||
public void printMap() {
|
||||
Log.write("Gesture Quantizer-Map:");
|
||||
this.quantizer.printMap();
|
||||
}
|
||||
|
||||
/***
|
||||
* For debug purposes or very technical interested people. :)
|
||||
* @return
|
||||
*/
|
||||
public void print() {
|
||||
Log.write("HMM-Print:");
|
||||
this.markovmodell.print();
|
||||
Log.write("Quanzizer-Print:");
|
||||
this.quantizer.printMap();
|
||||
}
|
||||
|
||||
public int getNumStates() {
|
||||
return this.numStates;
|
||||
}
|
||||
|
||||
public int getNumObservations() {
|
||||
return this.numObservations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the model probability for bayes.
|
||||
*
|
||||
* @return
|
||||
* the model probability
|
||||
*/
|
||||
public double getDefaultProbability() {
|
||||
return this.defaultprobability;
|
||||
}
|
||||
|
||||
/**
|
||||
* Since the bayes classifier needs a model probability for
|
||||
* each model this has to be set once after training. As model
|
||||
* probability the average probability value has been choosen.
|
||||
*
|
||||
* TODO: try lowest or highest model probability as alternative
|
||||
*
|
||||
* @param defsequence the vector of training sequences.
|
||||
*/
|
||||
private void setDefaultProbability(Vector<Gesture> defsequence) {
|
||||
double prob=0;
|
||||
for(int i=0; i<defsequence.size(); i++) {
|
||||
prob+=this.matches(defsequence.elementAt(i));
|
||||
}
|
||||
|
||||
this.defaultprobability=(prob)/defsequence.size();
|
||||
}
|
||||
|
||||
public void setDefaultProbability(double prob) {
|
||||
this.defaultprobability = prob;
|
||||
Log.write("def-prob. set to = "+this.defaultprobability);
|
||||
}
|
||||
/**
|
||||
* For debug purposes or very technical interested people. :)
|
||||
*/
|
||||
public void printMap() {
|
||||
Log.write("Gesture Quantizer-Map:");
|
||||
this.quantizer.printMap();
|
||||
}
|
||||
|
||||
/***
|
||||
* For debug purposes or very technical interested people. :)
|
||||
* @return
|
||||
*/
|
||||
public void print() {
|
||||
Log.write("HMM-Print:");
|
||||
this.markovmodell.print();
|
||||
Log.write("Quanzizer-Print:");
|
||||
this.quantizer.printMap();
|
||||
}
|
||||
|
||||
public int getNumStates() {
|
||||
return this.numStates;
|
||||
}
|
||||
|
||||
public int getNumObservations() {
|
||||
return this.numObservations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the model probability for bayes.
|
||||
*
|
||||
* @return
|
||||
* the model probability
|
||||
*/
|
||||
public double getDefaultProbability() {
|
||||
return this.defaultprobability;
|
||||
}
|
||||
|
||||
/**
|
||||
* Since the bayes classifier needs a model probability for
|
||||
* each model this has to be set once after training. As model
|
||||
* probability the average probability value has been choosen.
|
||||
*
|
||||
* TODO: try lowest or highest model probability as alternative
|
||||
*
|
||||
* @param defsequence the vector of training sequences.
|
||||
*/
|
||||
private void setDefaultProbability(Vector<Gesture> defsequence) {
|
||||
double prob=0;
|
||||
for(int i=0; i<defsequence.size(); i++) {
|
||||
prob+=this.matches(defsequence.elementAt(i));
|
||||
}
|
||||
|
||||
this.defaultprobability=(prob)/defsequence.size();
|
||||
}
|
||||
|
||||
public void setDefaultProbability(double prob) {
|
||||
this.defaultprobability = prob;
|
||||
Log.write("def-prob. set to = "+this.defaultprobability);
|
||||
}
|
||||
|
||||
public Quantizer getQuantizer() {
|
||||
return this.quantizer;
|
||||
}
|
||||
|
||||
public void setQuantizer(Quantizer q) {
|
||||
this.quantizer = q;
|
||||
}
|
||||
|
||||
public HMM getHMM() {
|
||||
return this.markovmodell;
|
||||
}
|
||||
|
||||
public void setHMM(HMM hmm) {
|
||||
this.markovmodell = hmm;
|
||||
}
|
||||
|
||||
public Quantizer getQuantizer() {
|
||||
return this.quantizer;
|
||||
}
|
||||
|
||||
public void setQuantizer(Quantizer q) {
|
||||
this.quantizer = q;
|
||||
}
|
||||
|
||||
public HMM getHMM() {
|
||||
return this.markovmodell;
|
||||
}
|
||||
|
||||
public void setHMM(HMM hmm) {
|
||||
this.markovmodell = hmm;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -39,276 +39,276 @@ import org.wiigee.util.Log;
|
||||
*/
|
||||
|
||||
public class HMM {
|
||||
/** The number of states */
|
||||
protected int numStates;
|
||||
/** The number of states */
|
||||
protected int numStates;
|
||||
|
||||
/** The number of observations */
|
||||
protected int numObservations;
|
||||
/** The number of observations */
|
||||
protected int numObservations;
|
||||
|
||||
/** The initial probabilities for each state: p[state] */
|
||||
protected double pi[];
|
||||
/** 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 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[][];
|
||||
/** 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; i<numStates; i++) {
|
||||
pi[i] = 0;
|
||||
}
|
||||
|
||||
// set state change probabilities in the left-to-right version
|
||||
// NOTE: i now that this is dirty and very static. :)
|
||||
for(int i=0; i<numStates; i++) {
|
||||
for(int j=0; j<numStates; j++) {
|
||||
if(i==numStates-1 && j==numStates-1) { // last row
|
||||
a[i][j] = 1.0;
|
||||
} else if(i==numStates-2 && j==numStates-2) { // next to last row
|
||||
a[i][j] = 0.5;
|
||||
} else if(i==numStates-2 && j==numStates-1) { // next to last row
|
||||
a[i][j] = 0.5;
|
||||
} else if(i<=j && i>j-jumplimit-1) {
|
||||
a[i][j] = 1.0/(jumplimit+1);
|
||||
} else {
|
||||
a[i][j] = 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// emission probability
|
||||
for(int i=0; i<numStates; i++) {
|
||||
for(int j=0; j<numObservations; j++) {
|
||||
b[i][j] = 1.0/(double)numObservations;
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 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; i<numStates; i++) {
|
||||
pi[i] = 0;
|
||||
}
|
||||
|
||||
// set state change probabilities in the left-to-right version
|
||||
// NOTE: i now that this is dirty and very static. :)
|
||||
for(int i=0; i<numStates; i++) {
|
||||
for(int j=0; j<numStates; j++) {
|
||||
if(i==numStates-1 && j==numStates-1) { // last row
|
||||
a[i][j] = 1.0;
|
||||
} else if(i==numStates-2 && j==numStates-2) { // next to last row
|
||||
a[i][j] = 0.5;
|
||||
} else if(i==numStates-2 && j==numStates-1) { // next to last row
|
||||
a[i][j] = 0.5;
|
||||
} else if(i<=j && i>j-jumplimit-1) {
|
||||
a[i][j] = 1.0/(jumplimit+1);
|
||||
} else {
|
||||
a[i][j] = 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// emission probability
|
||||
for(int i=0; i<numStates; i++) {
|
||||
for(int j=0; j<numObservations; j++) {
|
||||
b[i][j] = 1.0/(double)numObservations;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Trains the Hidden Markov Model with multiple sequences.
|
||||
* This method is normally not known to basic hidden markov
|
||||
* models, because they usually use the Baum-Welch-Algorithm.
|
||||
* This method is NOT the traditional Baum-Welch-Algorithm.
|
||||
*
|
||||
* If you want to know in detail how it works please consider
|
||||
* my Individuelles Projekt paper on the wiigee Homepage. Also
|
||||
* there exist some english literature on the world wide web.
|
||||
* Try to search for some papers by Rabiner or have a look at
|
||||
* Vesa-Matti Mäntylä - "Discrete Hidden Markov Models with
|
||||
* application to isolated user-dependent hand gesture recognition".
|
||||
*
|
||||
*/
|
||||
public void train(Vector<int[]> trainsequence) {
|
||||
/**
|
||||
* Trains the Hidden Markov Model with multiple sequences.
|
||||
* This method is normally not known to basic hidden markov
|
||||
* models, because they usually use the Baum-Welch-Algorithm.
|
||||
* This method is NOT the traditional Baum-Welch-Algorithm.
|
||||
*
|
||||
* If you want to know in detail how it works please consider
|
||||
* my Individuelles Projekt paper on the wiigee Homepage. Also
|
||||
* there exist some english literature on the world wide web.
|
||||
* Try to search for some papers by Rabiner or have a look at
|
||||
* Vesa-Matti Mäntylä - "Discrete Hidden Markov Models with
|
||||
* application to isolated user-dependent hand gesture recognition".
|
||||
*
|
||||
*/
|
||||
public void train(Vector<int[]> trainsequence) {
|
||||
|
||||
double[][] a_new = new double[a.length][a.length];
|
||||
double[][] b_new = new double[b.length][b[0].length];
|
||||
// re calculate state change probability a
|
||||
for(int i=0; i<a.length; i++) {
|
||||
for(int j=0; j<a[i].length; j++) {
|
||||
double zaehler=0;
|
||||
double nenner=0;
|
||||
|
||||
for(int k=0; k<trainsequence.size(); k++) {
|
||||
int[] sequence = trainsequence.elementAt(k);
|
||||
|
||||
double[][] fwd = this.forwardProc(sequence);
|
||||
double[][] bwd = this.backwardProc(sequence);
|
||||
double prob = this.getProbability(sequence);
|
||||
|
||||
double zaehler_innersum=0;
|
||||
double nenner_innersum=0;
|
||||
|
||||
|
||||
for(int t=0; t<sequence.length-1; t++) {
|
||||
zaehler_innersum+=fwd[i][t]*a[i][j]*b[j][sequence[t+1]]*bwd[j][t+1];
|
||||
nenner_innersum+=fwd[i][t]*bwd[i][t];
|
||||
}
|
||||
zaehler+=(1/prob)*zaehler_innersum;
|
||||
nenner+=(1/prob)*nenner_innersum;
|
||||
} // k
|
||||
|
||||
a_new[i][j] = zaehler/nenner;
|
||||
} // j
|
||||
} // i
|
||||
|
||||
// re calculate emission probability b
|
||||
for(int i=0; i<b.length; i++) { // zustaende
|
||||
for(int j=0; j<b[i].length; j++) { // symbole
|
||||
double zaehler=0;
|
||||
double nenner=0;
|
||||
|
||||
for(int k=0; k<trainsequence.size(); k++) {
|
||||
int[] sequence = trainsequence.elementAt(k);
|
||||
|
||||
double[][] fwd = this.forwardProc(sequence);
|
||||
double[][] bwd = this.backwardProc(sequence);
|
||||
double prob = this.getProbability(sequence);
|
||||
|
||||
double zaehler_innersum=0;
|
||||
double nenner_innersum=0;
|
||||
|
||||
|
||||
for(int t=0; t<sequence.length-1; t++) {
|
||||
if(sequence[t]==j) {
|
||||
zaehler_innersum+=fwd[i][t]*bwd[i][t];
|
||||
}
|
||||
nenner_innersum+=fwd[i][t]*bwd[i][t];
|
||||
}
|
||||
zaehler+=(1/prob)*zaehler_innersum;
|
||||
nenner+=(1/prob)*nenner_innersum;
|
||||
} // k
|
||||
|
||||
b_new[i][j] = zaehler/nenner;
|
||||
} // j
|
||||
} // i
|
||||
|
||||
this.a=a_new;
|
||||
this.b=b_new;
|
||||
}
|
||||
|
||||
/**
|
||||
* Traditional Forward Algorithm.
|
||||
*
|
||||
* @param o the observationsequence O
|
||||
* @return Array[State][Time]
|
||||
*
|
||||
*/
|
||||
protected double[][] forwardProc(int[] o) {
|
||||
double[][] f = new double[numStates][o.length];
|
||||
for (int l = 0; l < f.length; l++) {
|
||||
f[l][0] = pi[l] * b[l][o[0]];
|
||||
}
|
||||
for (int i = 1; i < o.length; i++) {
|
||||
for (int k = 0; k < f.length; k++) {
|
||||
double sum = 0;
|
||||
for (int l = 0; l < numStates; l++) {
|
||||
sum += f[l][i-1] * a[l][k];
|
||||
}
|
||||
f[k][i] = sum * b[k][o[i]];
|
||||
}
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the probability that a observation sequence O belongs
|
||||
* to this Hidden Markov Model without using the bayes classifier.
|
||||
* Internally the well known forward algorithm is used.
|
||||
*
|
||||
* @param o observation sequence
|
||||
* @return probability that sequence o belongs to this hmm
|
||||
*/
|
||||
public double getProbability(int[] o) {
|
||||
double prob = 0.0;
|
||||
double[][] forward = this.forwardProc(o);
|
||||
// add probabilities
|
||||
for (int i = 0; i < forward.length; i++) { // for every state
|
||||
prob += forward[i][forward[i].length - 1];
|
||||
}
|
||||
return prob;
|
||||
}
|
||||
|
||||
double[][] a_new = new double[a.length][a.length];
|
||||
double[][] b_new = new double[b.length][b[0].length];
|
||||
// re calculate state change probability a
|
||||
for(int i=0; i<a.length; i++) {
|
||||
for(int j=0; j<a[i].length; j++) {
|
||||
double zaehler=0;
|
||||
double nenner=0;
|
||||
|
||||
for(int k=0; k<trainsequence.size(); k++) {
|
||||
int[] sequence = trainsequence.elementAt(k);
|
||||
|
||||
double[][] fwd = this.forwardProc(sequence);
|
||||
double[][] bwd = this.backwardProc(sequence);
|
||||
double prob = this.getProbability(sequence);
|
||||
|
||||
double zaehler_innersum=0;
|
||||
double nenner_innersum=0;
|
||||
|
||||
|
||||
for(int t=0; t<sequence.length-1; t++) {
|
||||
zaehler_innersum+=fwd[i][t]*a[i][j]*b[j][sequence[t+1]]*bwd[j][t+1];
|
||||
nenner_innersum+=fwd[i][t]*bwd[i][t];
|
||||
}
|
||||
zaehler+=(1/prob)*zaehler_innersum;
|
||||
nenner+=(1/prob)*nenner_innersum;
|
||||
} // k
|
||||
|
||||
a_new[i][j] = zaehler/nenner;
|
||||
} // j
|
||||
} // i
|
||||
|
||||
// re calculate emission probability b
|
||||
for(int i=0; i<b.length; i++) { // zustaende
|
||||
for(int j=0; j<b[i].length; j++) { // symbole
|
||||
double zaehler=0;
|
||||
double nenner=0;
|
||||
|
||||
for(int k=0; k<trainsequence.size(); k++) {
|
||||
int[] sequence = trainsequence.elementAt(k);
|
||||
|
||||
double[][] fwd = this.forwardProc(sequence);
|
||||
double[][] bwd = this.backwardProc(sequence);
|
||||
double prob = this.getProbability(sequence);
|
||||
|
||||
double zaehler_innersum=0;
|
||||
double nenner_innersum=0;
|
||||
|
||||
|
||||
for(int t=0; t<sequence.length-1; t++) {
|
||||
if(sequence[t]==j) {
|
||||
zaehler_innersum+=fwd[i][t]*bwd[i][t];
|
||||
}
|
||||
nenner_innersum+=fwd[i][t]*bwd[i][t];
|
||||
}
|
||||
zaehler+=(1/prob)*zaehler_innersum;
|
||||
nenner+=(1/prob)*nenner_innersum;
|
||||
} // k
|
||||
|
||||
b_new[i][j] = zaehler/nenner;
|
||||
} // j
|
||||
} // i
|
||||
|
||||
this.a=a_new;
|
||||
this.b=b_new;
|
||||
}
|
||||
|
||||
/**
|
||||
* Traditional Forward Algorithm.
|
||||
*
|
||||
* @param o the observationsequence O
|
||||
* @return Array[State][Time]
|
||||
*
|
||||
*/
|
||||
protected double[][] forwardProc(int[] o) {
|
||||
double[][] f = new double[numStates][o.length];
|
||||
for (int l = 0; l < f.length; l++) {
|
||||
f[l][0] = pi[l] * b[l][o[0]];
|
||||
}
|
||||
for (int i = 1; i < o.length; i++) {
|
||||
for (int k = 0; k < f.length; k++) {
|
||||
double sum = 0;
|
||||
for (int l = 0; l < numStates; l++) {
|
||||
sum += f[l][i-1] * a[l][k];
|
||||
}
|
||||
f[k][i] = sum * b[k][o[i]];
|
||||
}
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the probability that a observation sequence O belongs
|
||||
* to this Hidden Markov Model without using the bayes classifier.
|
||||
* Internally the well known forward algorithm is used.
|
||||
*
|
||||
* @param o observation sequence
|
||||
* @return probability that sequence o belongs to this hmm
|
||||
*/
|
||||
public double getProbability(int[] o) {
|
||||
double prob = 0.0;
|
||||
double[][] forward = this.forwardProc(o);
|
||||
// add probabilities
|
||||
for (int i = 0; i < forward.length; i++) { // for every state
|
||||
prob += forward[i][forward[i].length - 1];
|
||||
}
|
||||
return prob;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Backward algorithm.
|
||||
*
|
||||
* @param o observation sequence o
|
||||
* @return Array[State][Time]
|
||||
*/
|
||||
protected double[][] backwardProc(int[] o) {
|
||||
int T = o.length;
|
||||
double[][] bwd = new double[numStates][T];
|
||||
/* Basisfall */
|
||||
for (int i = 0; i < numStates; i++)
|
||||
bwd[i][T - 1] = 1;
|
||||
/* Induktion */
|
||||
for (int t = T - 2; t >= 0; t--) {
|
||||
for (int i = 0; i < numStates; i++) {
|
||||
bwd[i][t] = 0;
|
||||
for (int j = 0; j < numStates; j++)
|
||||
bwd[i][t] += (bwd[j][t + 1] * a[i][j] * b[j][o[t + 1]]);
|
||||
}
|
||||
}
|
||||
return bwd;
|
||||
}
|
||||
/**
|
||||
* Backward algorithm.
|
||||
*
|
||||
* @param o observation sequence o
|
||||
* @return Array[State][Time]
|
||||
*/
|
||||
protected double[][] backwardProc(int[] o) {
|
||||
int T = o.length;
|
||||
double[][] bwd = new double[numStates][T];
|
||||
/* Basisfall */
|
||||
for (int i = 0; i < numStates; i++)
|
||||
bwd[i][T - 1] = 1;
|
||||
/* Induktion */
|
||||
for (int t = T - 2; t >= 0; t--) {
|
||||
for (int i = 0; i < numStates; i++) {
|
||||
bwd[i][t] = 0;
|
||||
for (int j = 0; j < numStates; j++)
|
||||
bwd[i][t] += (bwd[j][t + 1] * a[i][j] * b[j][o[t + 1]]);
|
||||
}
|
||||
}
|
||||
return bwd;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Prints everything about this model, including
|
||||
* all values. For debug purposes or if you want
|
||||
* to comprehend what happend to the model.
|
||||
*
|
||||
*/
|
||||
public void print() {
|
||||
DecimalFormat fmt = new DecimalFormat();
|
||||
fmt.setMinimumFractionDigits(5);
|
||||
fmt.setMaximumFractionDigits(5);
|
||||
for (int i = 0; i < numStates; i++)
|
||||
Log.write("pi(" + i + ") = " + fmt.format(pi[i]));
|
||||
Log.write("");
|
||||
for (int i = 0; i < numStates; i++) {
|
||||
for (int j = 0; j < numStates; j++)
|
||||
Log.write("a(" + i + "," + j + ") = "
|
||||
+ fmt.format(a[i][j]) + " ");
|
||||
Log.write("");
|
||||
}
|
||||
Log.write("");
|
||||
for (int i = 0; i < numStates; i++) {
|
||||
for (int k = 0; k < numObservations; k++)
|
||||
Log.write("b(" + i + "," + k + ") = "
|
||||
+ fmt.format(b[i][k]) + " ");
|
||||
Log.write("");
|
||||
}
|
||||
}
|
||||
|
||||
public double[] getPi() {
|
||||
return this.pi;
|
||||
}
|
||||
|
||||
public void setPi(double[] pi) {
|
||||
this.pi = pi;
|
||||
}
|
||||
/**
|
||||
* Prints everything about this model, including
|
||||
* all values. For debug purposes or if you want
|
||||
* to comprehend what happend to the model.
|
||||
*
|
||||
*/
|
||||
public void print() {
|
||||
DecimalFormat fmt = new DecimalFormat();
|
||||
fmt.setMinimumFractionDigits(5);
|
||||
fmt.setMaximumFractionDigits(5);
|
||||
for (int i = 0; i < numStates; i++)
|
||||
Log.write("pi(" + i + ") = " + fmt.format(pi[i]));
|
||||
Log.write("");
|
||||
for (int i = 0; i < numStates; i++) {
|
||||
for (int j = 0; j < numStates; j++)
|
||||
Log.write("a(" + i + "," + j + ") = "
|
||||
+ fmt.format(a[i][j]) + " ");
|
||||
Log.write("");
|
||||
}
|
||||
Log.write("");
|
||||
for (int i = 0; i < numStates; i++) {
|
||||
for (int k = 0; k < numObservations; k++)
|
||||
Log.write("b(" + i + "," + k + ") = "
|
||||
+ fmt.format(b[i][k]) + " ");
|
||||
Log.write("");
|
||||
}
|
||||
}
|
||||
|
||||
public double[] getPi() {
|
||||
return this.pi;
|
||||
}
|
||||
|
||||
public void setPi(double[] pi) {
|
||||
this.pi = pi;
|
||||
}
|
||||
|
||||
public double[][] getA() {
|
||||
return this.a;
|
||||
}
|
||||
|
||||
public void setA(double[][] a) {
|
||||
this.a = a;
|
||||
}
|
||||
|
||||
public double[][] getB() {
|
||||
return this.b;
|
||||
}
|
||||
|
||||
public void setB(double[][] b) {
|
||||
this.b=b;
|
||||
}
|
||||
public double[][] getA() {
|
||||
return this.a;
|
||||
}
|
||||
|
||||
public void setA(double[][] a) {
|
||||
this.a = a;
|
||||
}
|
||||
|
||||
public double[][] getB() {
|
||||
return this.b;
|
||||
}
|
||||
|
||||
public void setB(double[][] b) {
|
||||
this.b=b;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,410 +40,410 @@ import org.wiigee.util.Log;
|
||||
*/
|
||||
|
||||
public class PreciseHMM {
|
||||
/** The number of states */
|
||||
private int numStates;
|
||||
/** The number of states */
|
||||
private int numStates;
|
||||
|
||||
/** The number of observations */
|
||||
private int sigmaSize;
|
||||
/** The number of observations */
|
||||
private int sigmaSize;
|
||||
|
||||
/** The initial probabilities for each state: p[state] */
|
||||
public double pi[];
|
||||
/** The initial probabilities for each state: p[state] */
|
||||
public double pi[];
|
||||
|
||||
/** The state change probability to switch from state A to
|
||||
* state B: a[stateA][stateB] */
|
||||
public double a[][];
|
||||
/** The state change probability to switch from state A to
|
||||
* state B: a[stateA][stateB] */
|
||||
public double a[][];
|
||||
|
||||
/** The probability to emit symbol S in state A: b[stateA][symbolS] */
|
||||
public double b[][];
|
||||
/** The probability to emit symbol S in state A: b[stateA][symbolS] */
|
||||
public double b[][];
|
||||
|
||||
/**
|
||||
* Initialize the Hidden Markov Model in a left-to-right version.
|
||||
*
|
||||
* @param numStates Number of states
|
||||
* @param sigmaSize Number of observations
|
||||
*/
|
||||
public PreciseHMM(int numStates, int sigmaSize) {
|
||||
this.numStates = numStates;
|
||||
this.sigmaSize = sigmaSize;
|
||||
pi = new double[numStates];
|
||||
a = new double[numStates][numStates];
|
||||
b = new double[numStates][sigmaSize];
|
||||
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.0;
|
||||
for(int i=1; i<numStates; i++) {
|
||||
pi[i] = 0;
|
||||
}
|
||||
|
||||
// set state change probabilities in the left-to-right version
|
||||
// NOTE: i now that this is dirty and very static. :)
|
||||
for(int i=0; i<numStates; i++) {
|
||||
for(int j=0; j<numStates; j++) {
|
||||
if(i==numStates-1 && j==numStates-1) { // last row
|
||||
a[i][j] = 1.0;
|
||||
} else if(i==numStates-2 && j==numStates-2) { // next to last row
|
||||
a[i][j] = 0.5;
|
||||
} else if(i==numStates-2 && j==numStates-1) { // next to last row
|
||||
a[i][j] = 0.5;
|
||||
} else if(i<=j && i>j-jumplimit-1) {
|
||||
a[i][j] = 1.0/(jumplimit+1);
|
||||
} else {
|
||||
a[i][j] = 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// emission probability
|
||||
for(int i=0; i<numStates; i++) {
|
||||
for(int j=0; j<sigmaSize; j++) {
|
||||
b[i][j] = 1.0/(double)sigmaSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Initialize the Hidden Markov Model in a left-to-right version.
|
||||
*
|
||||
* @param numStates Number of states
|
||||
* @param sigmaSize Number of observations
|
||||
*/
|
||||
public PreciseHMM(int numStates, int sigmaSize) {
|
||||
this.numStates = numStates;
|
||||
this.sigmaSize = sigmaSize;
|
||||
pi = new double[numStates];
|
||||
a = new double[numStates][numStates];
|
||||
b = new double[numStates][sigmaSize];
|
||||
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.0;
|
||||
for(int i=1; i<numStates; i++) {
|
||||
pi[i] = 0;
|
||||
}
|
||||
|
||||
// set state change probabilities in the left-to-right version
|
||||
// NOTE: i now that this is dirty and very static. :)
|
||||
for(int i=0; i<numStates; i++) {
|
||||
for(int j=0; j<numStates; j++) {
|
||||
if(i==numStates-1 && j==numStates-1) { // last row
|
||||
a[i][j] = 1.0;
|
||||
} else if(i==numStates-2 && j==numStates-2) { // next to last row
|
||||
a[i][j] = 0.5;
|
||||
} else if(i==numStates-2 && j==numStates-1) { // next to last row
|
||||
a[i][j] = 0.5;
|
||||
} else if(i<=j && i>j-jumplimit-1) {
|
||||
a[i][j] = 1.0/(jumplimit+1);
|
||||
} else {
|
||||
a[i][j] = 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// emission probability
|
||||
for(int i=0; i<numStates; i++) {
|
||||
for(int j=0; j<sigmaSize; j++) {
|
||||
b[i][j] = 1.0/(double)sigmaSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Trains the Hidden Markov Model with multiple sequences.
|
||||
* This method is normally not known to basic hidden markov
|
||||
* models, because they usually use the Baum-Welch-Algorithm.
|
||||
* This method is NOT the traditional Baum-Welch-Algorithm.
|
||||
*
|
||||
* If you want to know in detail how it works please consider
|
||||
* my Individuelles Projekt paper on the wiigee Homepage. Also
|
||||
* there exist some english literature on the world wide web.
|
||||
* Try to search for some papers by Rabiner or have a look at
|
||||
* Vesa-Matti Mäntylä - "Discrete Hidden Markov Models with
|
||||
* application to isolated user-dependent hand gesture recognition".
|
||||
*
|
||||
*/
|
||||
public void train(Vector<int[]> trainsequence) {
|
||||
/**
|
||||
* Trains the Hidden Markov Model with multiple sequences.
|
||||
* This method is normally not known to basic hidden markov
|
||||
* models, because they usually use the Baum-Welch-Algorithm.
|
||||
* This method is NOT the traditional Baum-Welch-Algorithm.
|
||||
*
|
||||
* If you want to know in detail how it works please consider
|
||||
* my Individuelles Projekt paper on the wiigee Homepage. Also
|
||||
* there exist some english literature on the world wide web.
|
||||
* Try to search for some papers by Rabiner or have a look at
|
||||
* Vesa-Matti Mäntylä - "Discrete Hidden Markov Models with
|
||||
* application to isolated user-dependent hand gesture recognition".
|
||||
*
|
||||
*/
|
||||
public void train(Vector<int[]> trainsequence) {
|
||||
|
||||
double[][] a_new = new double[a.length][a.length];
|
||||
double[][] b_new = new double[b.length][b[0].length];
|
||||
// re calculate state change probability a
|
||||
for(int i=0; i<a.length; i++) {
|
||||
for(int j=0; j<a[i].length; j++) {
|
||||
double zaehler=0;
|
||||
double nenner=0;
|
||||
|
||||
for(int k=0; k<trainsequence.size(); k++) {
|
||||
//this.reset();
|
||||
int[] sequence = trainsequence.elementAt(k);
|
||||
|
||||
double[] sf = this.calculateScalingFactor(sequence);
|
||||
double[][] fwd = this.scaledForwardProc(sequence);
|
||||
double[][] bwd = this.scaledBackwardProc(sequence, sf);
|
||||
|
||||
double zaehler_innersum=0;
|
||||
double nenner_innersum=0;
|
||||
|
||||
for(int t=0; t<sequence.length-1; t++) {
|
||||
zaehler_innersum+=fwd[i][t]*a[i][j]*b[j][sequence[t+1]]*bwd[j][t+1]*sf[t+1];
|
||||
nenner_innersum+=fwd[i][t]*bwd[i][t];
|
||||
}
|
||||
zaehler+=zaehler_innersum;
|
||||
nenner+=nenner_innersum;
|
||||
} // k
|
||||
|
||||
a_new[i][j] = zaehler/nenner;
|
||||
} // j
|
||||
} // i
|
||||
|
||||
// re calculate emission probability b
|
||||
for(int i=0; i<b.length; i++) { // zustaende
|
||||
for(int j=0; j<b[i].length; j++) { // symbole
|
||||
double zaehler=0;
|
||||
double nenner=0;
|
||||
|
||||
for(int k=0; k<trainsequence.size(); k++) {
|
||||
//this.reset();
|
||||
int[] sequence = trainsequence.elementAt(k);
|
||||
|
||||
double[] sf = this.calculateScalingFactor(sequence);
|
||||
double[][] fwd = this.scaledForwardProc(sequence);
|
||||
double[][] bwd = this.scaledBackwardProc(sequence, sf);
|
||||
|
||||
double zaehler_innersum=0;
|
||||
double nenner_innersum=0;
|
||||
|
||||
for(int t=0; t<sequence.length-1; t++) {
|
||||
if(sequence[t]==j) {
|
||||
zaehler_innersum+=fwd[i][t]*bwd[i][t]*sf[t];
|
||||
}
|
||||
nenner_innersum+=fwd[i][t]*bwd[i][t]*sf[t];
|
||||
}
|
||||
zaehler+=zaehler_innersum;
|
||||
nenner+=nenner_innersum;
|
||||
} // k
|
||||
|
||||
b_new[i][j] = zaehler/nenner;
|
||||
} // j
|
||||
} // i
|
||||
|
||||
this.a=a_new;
|
||||
this.b=b_new;
|
||||
}
|
||||
|
||||
|
||||
private double[] calculateScalingFactor(int[] sequence) {
|
||||
// for all indexing: [state][time]
|
||||
double[][] fwd = this.forwardProc(sequence); // normal
|
||||
double[][] help = new double[fwd.length][fwd[0].length];
|
||||
double[][] scaled = new double[fwd.length][fwd[0].length];
|
||||
double[] sf = new double[sequence.length];
|
||||
|
||||
// ************** BASIS *************
|
||||
// Basis, fixed t=0
|
||||
// setup, because needed for further calculations
|
||||
for(int i=0; i<help.length; i++) {
|
||||
help[i][0] = fwd[i][0];
|
||||
}
|
||||
|
||||
// setup initial scaled array
|
||||
double sum0 = 0;
|
||||
for(int i=0; i<help.length; i++) {
|
||||
sum0+=help[i][0];
|
||||
}
|
||||
|
||||
for(int i=0; i<scaled.length; i++) {
|
||||
scaled[i][0] = help[i][0] / sum0;
|
||||
}
|
||||
|
||||
// calculate scaling factor
|
||||
sf[0] = 1/sum0;
|
||||
|
||||
// **************** INDUCTION ***************
|
||||
// end of fixed t = 0
|
||||
// starting with t>1 to sequence.length
|
||||
// induction, further calculations
|
||||
for(int t=1; t<sequence.length; t++) {
|
||||
// calculate help
|
||||
for(int i=0; i<help.length; i++) {
|
||||
for(int j=0; j<this.numStates; j++) {
|
||||
help[i][t]+=scaled[j][t-1]*a[j][i]*b[i][sequence[t]];
|
||||
}
|
||||
}
|
||||
|
||||
double sum = 0;
|
||||
for(int i=0; i<help.length; i++) {
|
||||
sum+=help[i][t];
|
||||
}
|
||||
|
||||
for(int i=0; i<scaled.length; i++) {
|
||||
scaled[i][t] = help[i][t] / sum;
|
||||
}
|
||||
double[][] a_new = new double[a.length][a.length];
|
||||
double[][] b_new = new double[b.length][b[0].length];
|
||||
// re calculate state change probability a
|
||||
for(int i=0; i<a.length; i++) {
|
||||
for(int j=0; j<a[i].length; j++) {
|
||||
double zaehler=0;
|
||||
double nenner=0;
|
||||
|
||||
for(int k=0; k<trainsequence.size(); k++) {
|
||||
//this.reset();
|
||||
int[] sequence = trainsequence.elementAt(k);
|
||||
|
||||
double[] sf = this.calculateScalingFactor(sequence);
|
||||
double[][] fwd = this.scaledForwardProc(sequence);
|
||||
double[][] bwd = this.scaledBackwardProc(sequence, sf);
|
||||
|
||||
double zaehler_innersum=0;
|
||||
double nenner_innersum=0;
|
||||
|
||||
for(int t=0; t<sequence.length-1; t++) {
|
||||
zaehler_innersum+=fwd[i][t]*a[i][j]*b[j][sequence[t+1]]*bwd[j][t+1]*sf[t+1];
|
||||
nenner_innersum+=fwd[i][t]*bwd[i][t];
|
||||
}
|
||||
zaehler+=zaehler_innersum;
|
||||
nenner+=nenner_innersum;
|
||||
} // k
|
||||
|
||||
a_new[i][j] = zaehler/nenner;
|
||||
} // j
|
||||
} // i
|
||||
|
||||
// re calculate emission probability b
|
||||
for(int i=0; i<b.length; i++) { // zustaende
|
||||
for(int j=0; j<b[i].length; j++) { // symbole
|
||||
double zaehler=0;
|
||||
double nenner=0;
|
||||
|
||||
for(int k=0; k<trainsequence.size(); k++) {
|
||||
//this.reset();
|
||||
int[] sequence = trainsequence.elementAt(k);
|
||||
|
||||
double[] sf = this.calculateScalingFactor(sequence);
|
||||
double[][] fwd = this.scaledForwardProc(sequence);
|
||||
double[][] bwd = this.scaledBackwardProc(sequence, sf);
|
||||
|
||||
double zaehler_innersum=0;
|
||||
double nenner_innersum=0;
|
||||
|
||||
for(int t=0; t<sequence.length-1; t++) {
|
||||
if(sequence[t]==j) {
|
||||
zaehler_innersum+=fwd[i][t]*bwd[i][t]*sf[t];
|
||||
}
|
||||
nenner_innersum+=fwd[i][t]*bwd[i][t]*sf[t];
|
||||
}
|
||||
zaehler+=zaehler_innersum;
|
||||
nenner+=nenner_innersum;
|
||||
} // k
|
||||
|
||||
b_new[i][j] = zaehler/nenner;
|
||||
} // j
|
||||
} // i
|
||||
|
||||
this.a=a_new;
|
||||
this.b=b_new;
|
||||
}
|
||||
|
||||
|
||||
private double[] calculateScalingFactor(int[] sequence) {
|
||||
// for all indexing: [state][time]
|
||||
double[][] fwd = this.forwardProc(sequence); // normal
|
||||
double[][] help = new double[fwd.length][fwd[0].length];
|
||||
double[][] scaled = new double[fwd.length][fwd[0].length];
|
||||
double[] sf = new double[sequence.length];
|
||||
|
||||
// ************** BASIS *************
|
||||
// Basis, fixed t=0
|
||||
// setup, because needed for further calculations
|
||||
for(int i=0; i<help.length; i++) {
|
||||
help[i][0] = fwd[i][0];
|
||||
}
|
||||
|
||||
// setup initial scaled array
|
||||
double sum0 = 0;
|
||||
for(int i=0; i<help.length; i++) {
|
||||
sum0+=help[i][0];
|
||||
}
|
||||
|
||||
for(int i=0; i<scaled.length; i++) {
|
||||
scaled[i][0] = help[i][0] / sum0;
|
||||
}
|
||||
|
||||
// calculate scaling factor
|
||||
sf[0] = 1/sum0;
|
||||
|
||||
// **************** INDUCTION ***************
|
||||
// end of fixed t = 0
|
||||
// starting with t>1 to sequence.length
|
||||
// induction, further calculations
|
||||
for(int t=1; t<sequence.length; t++) {
|
||||
// calculate help
|
||||
for(int i=0; i<help.length; i++) {
|
||||
for(int j=0; j<this.numStates; j++) {
|
||||
help[i][t]+=scaled[j][t-1]*a[j][i]*b[i][sequence[t]];
|
||||
}
|
||||
}
|
||||
|
||||
double sum = 0;
|
||||
for(int i=0; i<help.length; i++) {
|
||||
sum+=help[i][t];
|
||||
}
|
||||
|
||||
for(int i=0; i<scaled.length; i++) {
|
||||
scaled[i][t] = help[i][t] / sum;
|
||||
}
|
||||
|
||||
// calculate scaling factor
|
||||
sf[t] = 1 / sum;
|
||||
|
||||
} // t
|
||||
|
||||
return sf;
|
||||
} // calculateScalingFactor
|
||||
// calculate scaling factor
|
||||
sf[t] = 1 / sum;
|
||||
|
||||
} // t
|
||||
|
||||
return sf;
|
||||
} // calculateScalingFactor
|
||||
|
||||
/***
|
||||
* Returns the scaled Forward variable.
|
||||
* TODO: Maybe try out if the other precalculated method is faster.
|
||||
* @param sequence
|
||||
* @return
|
||||
*/
|
||||
private double[][] scaledForwardProc(int[] sequence) {
|
||||
double[][] fwd = this.forwardProc(sequence);
|
||||
double[][] out = new double[fwd.length][fwd[0].length];
|
||||
for(int i=0; i<fwd.length; i++) {
|
||||
for(int t=0; t<sequence.length; t++) {
|
||||
double sum = 0;
|
||||
for(int j=0; j<fwd.length; j++) {
|
||||
sum+=fwd[j][t];
|
||||
}
|
||||
out[i][t] = fwd[i][t] / sum;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private double[][] scaledBackwardProc(int[] sequence, double[] sf) {
|
||||
double[][] bwd = this.backwardProc(sequence);
|
||||
double[][] out = new double[bwd.length][bwd[0].length];
|
||||
for(int i=0; i<bwd.length; i++) {
|
||||
for(int t=0; t<sequence.length; t++) {
|
||||
out[i][t]=1;
|
||||
for(int r=t+1; r<sequence.length; r++) {
|
||||
out[i][t]*=sf[r]*bwd[i][t];
|
||||
}
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the probability that a observation sequence O belongs
|
||||
* to this Hidden Markov Model without using the bayes classifier.
|
||||
* Internally the well known forward algorithm is used.
|
||||
*
|
||||
* @param o observation sequence
|
||||
* @return probability that sequence o belongs to this hmm
|
||||
*/
|
||||
public double getProbability(int[] o) {
|
||||
return scaledViterbi(o);
|
||||
//return sProbability(o);
|
||||
/*double prob = 0.0;
|
||||
double[][] forward = this.forwardProc(o);
|
||||
// add probabilities
|
||||
for (int i = 0; i < forward.length; i++) { // for every state
|
||||
prob += forward[i][forward[i].length - 1];
|
||||
}
|
||||
return prob;*/
|
||||
}
|
||||
|
||||
public double sProbability(int[] o) {
|
||||
double prod = 1.0;
|
||||
double[][] fwd = this.scaledForwardProc(o);
|
||||
for(int t=0; t<o.length; t++) {
|
||||
double sum = 0.0;
|
||||
for(int i=0; i<this.numStates; i++) {
|
||||
sum+=fwd[i][t];
|
||||
}
|
||||
sum = 1/sum;
|
||||
prod*=sum;
|
||||
}
|
||||
return 1/prod;
|
||||
}
|
||||
|
||||
public double scaledViterbi(int[] o) {
|
||||
double[][] phi = new double[this.numStates][o.length]; //phi[states][oseq]
|
||||
// init
|
||||
for(int i=0; i<this.numStates; i++) {
|
||||
phi[i][0] = Math.log(pi[i]) + Math.log(b[i][o[0]]);
|
||||
}
|
||||
// induction
|
||||
for(int t=1; t<o.length; t++) {
|
||||
for(int j=0; j<this.numStates; j++) {
|
||||
double max = Double.NEGATIVE_INFINITY;
|
||||
for(int i=0; i<this.numStates; i++) {
|
||||
double val = phi[i][t-1] + Math.log(this.a[i][j]);
|
||||
if(val>max) {
|
||||
max = val;
|
||||
}
|
||||
}
|
||||
|
||||
phi[j][t] = max + Math.log(this.b[j][o[t]]);
|
||||
}
|
||||
}
|
||||
// conclusion
|
||||
double lp = Double.NEGATIVE_INFINITY;
|
||||
for(int i=0; i<this.numStates; i++) {
|
||||
if(phi[i][o.length-1]>lp) {
|
||||
lp = phi[i][o.length-1];
|
||||
}
|
||||
}
|
||||
|
||||
//Log.write("log p = "+lp);
|
||||
//return lp;
|
||||
// we now have log10(p) calculated, transform to p.
|
||||
Log.write("prob = "+Math.exp(lp));
|
||||
return Math.exp(lp);
|
||||
//return Math.pow(10, lp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Traditional Forward Algorithm.
|
||||
*
|
||||
* @param o the observationsequence O
|
||||
* @return Array[State][Time]
|
||||
*
|
||||
*/
|
||||
private double[][] forwardProc(int[] o) {
|
||||
double[][] f = new double[numStates][o.length];
|
||||
for (int l = 0; l < f.length; l++) {
|
||||
f[l][0] = pi[l] * b[l][o[0]];
|
||||
}
|
||||
for (int i = 1; i < o.length; i++) {
|
||||
for (int k = 0; k < f.length; k++) {
|
||||
double sum = 0;
|
||||
for (int l = 0; l < numStates; l++) {
|
||||
sum += f[l][i-1] * a[l][k];
|
||||
}
|
||||
f[k][i] = sum * b[k][o[i]];
|
||||
}
|
||||
}
|
||||
return f;
|
||||
}
|
||||
/***
|
||||
* Returns the scaled Forward variable.
|
||||
* TODO: Maybe try out if the other precalculated method is faster.
|
||||
* @param sequence
|
||||
* @return
|
||||
*/
|
||||
private double[][] scaledForwardProc(int[] sequence) {
|
||||
double[][] fwd = this.forwardProc(sequence);
|
||||
double[][] out = new double[fwd.length][fwd[0].length];
|
||||
for(int i=0; i<fwd.length; i++) {
|
||||
for(int t=0; t<sequence.length; t++) {
|
||||
double sum = 0;
|
||||
for(int j=0; j<fwd.length; j++) {
|
||||
sum+=fwd[j][t];
|
||||
}
|
||||
out[i][t] = fwd[i][t] / sum;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private double[][] scaledBackwardProc(int[] sequence, double[] sf) {
|
||||
double[][] bwd = this.backwardProc(sequence);
|
||||
double[][] out = new double[bwd.length][bwd[0].length];
|
||||
for(int i=0; i<bwd.length; i++) {
|
||||
for(int t=0; t<sequence.length; t++) {
|
||||
out[i][t]=1;
|
||||
for(int r=t+1; r<sequence.length; r++) {
|
||||
out[i][t]*=sf[r]*bwd[i][t];
|
||||
}
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the probability that a observation sequence O belongs
|
||||
* to this Hidden Markov Model without using the bayes classifier.
|
||||
* Internally the well known forward algorithm is used.
|
||||
*
|
||||
* @param o observation sequence
|
||||
* @return probability that sequence o belongs to this hmm
|
||||
*/
|
||||
public double getProbability(int[] o) {
|
||||
return scaledViterbi(o);
|
||||
//return sProbability(o);
|
||||
/*double prob = 0.0;
|
||||
double[][] forward = this.forwardProc(o);
|
||||
// add probabilities
|
||||
for (int i = 0; i < forward.length; i++) { // for every state
|
||||
prob += forward[i][forward[i].length - 1];
|
||||
}
|
||||
return prob;*/
|
||||
}
|
||||
|
||||
public double sProbability(int[] o) {
|
||||
double prod = 1.0;
|
||||
double[][] fwd = this.scaledForwardProc(o);
|
||||
for(int t=0; t<o.length; t++) {
|
||||
double sum = 0.0;
|
||||
for(int i=0; i<this.numStates; i++) {
|
||||
sum+=fwd[i][t];
|
||||
}
|
||||
sum = 1/sum;
|
||||
prod*=sum;
|
||||
}
|
||||
return 1/prod;
|
||||
}
|
||||
|
||||
public double scaledViterbi(int[] o) {
|
||||
double[][] phi = new double[this.numStates][o.length]; //phi[states][oseq]
|
||||
// init
|
||||
for(int i=0; i<this.numStates; i++) {
|
||||
phi[i][0] = Math.log(pi[i]) + Math.log(b[i][o[0]]);
|
||||
}
|
||||
// induction
|
||||
for(int t=1; t<o.length; t++) {
|
||||
for(int j=0; j<this.numStates; j++) {
|
||||
double max = Double.NEGATIVE_INFINITY;
|
||||
for(int i=0; i<this.numStates; i++) {
|
||||
double val = phi[i][t-1] + Math.log(this.a[i][j]);
|
||||
if(val>max) {
|
||||
max = val;
|
||||
}
|
||||
}
|
||||
|
||||
phi[j][t] = max + Math.log(this.b[j][o[t]]);
|
||||
}
|
||||
}
|
||||
// conclusion
|
||||
double lp = Double.NEGATIVE_INFINITY;
|
||||
for(int i=0; i<this.numStates; i++) {
|
||||
if(phi[i][o.length-1]>lp) {
|
||||
lp = phi[i][o.length-1];
|
||||
}
|
||||
}
|
||||
|
||||
//Log.write("log p = "+lp);
|
||||
//return lp;
|
||||
// we now have log10(p) calculated, transform to p.
|
||||
Log.write("prob = "+Math.exp(lp));
|
||||
return Math.exp(lp);
|
||||
//return Math.pow(10, lp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Traditional Forward Algorithm.
|
||||
*
|
||||
* @param o the observationsequence O
|
||||
* @return Array[State][Time]
|
||||
*
|
||||
*/
|
||||
private double[][] forwardProc(int[] o) {
|
||||
double[][] f = new double[numStates][o.length];
|
||||
for (int l = 0; l < f.length; l++) {
|
||||
f[l][0] = pi[l] * b[l][o[0]];
|
||||
}
|
||||
for (int i = 1; i < o.length; i++) {
|
||||
for (int k = 0; k < f.length; k++) {
|
||||
double sum = 0;
|
||||
for (int l = 0; l < numStates; l++) {
|
||||
sum += f[l][i-1] * a[l][k];
|
||||
}
|
||||
f[k][i] = sum * b[k][o[i]];
|
||||
}
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Backward algorithm.
|
||||
*
|
||||
* @param o observation sequence o
|
||||
* @return Array[State][Time]
|
||||
*/
|
||||
private double[][] backwardProc(int[] o) {
|
||||
int T = o.length;
|
||||
double[][] bwd = new double[numStates][T];
|
||||
/* Basisfall */
|
||||
for (int i = 0; i < numStates; i++)
|
||||
bwd[i][T - 1] = 1;
|
||||
/* Induktion */
|
||||
for (int t = T - 2; t >= 0; t--) {
|
||||
for (int i = 0; i < numStates; i++) {
|
||||
bwd[i][t] = 0;
|
||||
for (int j = 0; j < numStates; j++)
|
||||
bwd[i][t] += (bwd[j][t + 1] * a[i][j] * b[j][o[t + 1]]);
|
||||
}
|
||||
}
|
||||
return bwd;
|
||||
}
|
||||
/**
|
||||
* Backward algorithm.
|
||||
*
|
||||
* @param o observation sequence o
|
||||
* @return Array[State][Time]
|
||||
*/
|
||||
private double[][] backwardProc(int[] o) {
|
||||
int T = o.length;
|
||||
double[][] bwd = new double[numStates][T];
|
||||
/* Basisfall */
|
||||
for (int i = 0; i < numStates; i++)
|
||||
bwd[i][T - 1] = 1;
|
||||
/* Induktion */
|
||||
for (int t = T - 2; t >= 0; t--) {
|
||||
for (int i = 0; i < numStates; i++) {
|
||||
bwd[i][t] = 0;
|
||||
for (int j = 0; j < numStates; j++)
|
||||
bwd[i][t] += (bwd[j][t + 1] * a[i][j] * b[j][o[t + 1]]);
|
||||
}
|
||||
}
|
||||
return bwd;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Prints everything about this model, including
|
||||
* all values. For debug purposes or if you want
|
||||
* to comprehend what happend to the model.
|
||||
*
|
||||
*/
|
||||
public void print() {
|
||||
DecimalFormat fmt = new DecimalFormat();
|
||||
fmt.setMinimumFractionDigits(10);
|
||||
fmt.setMaximumFractionDigits(10);
|
||||
for (int i = 0; i < numStates; i++)
|
||||
Log.write("pi(" + i + ") = " + fmt.format(pi[i]));
|
||||
Log.write("");
|
||||
for (int i = 0; i < numStates; i++) {
|
||||
for (int j = 0; j < numStates; j++)
|
||||
Log.write("a(" + i + "," + j + ") = "
|
||||
+ fmt.format(a[i][j]) + " ");
|
||||
Log.write("");
|
||||
}
|
||||
Log.write("");
|
||||
for (int i = 0; i < numStates; i++) {
|
||||
for (int k = 0; k < sigmaSize; k++)
|
||||
Log.write("b(" + i + "," + k + ") = "
|
||||
+ fmt.format(b[i][k]) + " ");
|
||||
Log.write("");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Prints everything about this model, including
|
||||
* all values. For debug purposes or if you want
|
||||
* to comprehend what happend to the model.
|
||||
*
|
||||
*/
|
||||
public void print() {
|
||||
DecimalFormat fmt = new DecimalFormat();
|
||||
fmt.setMinimumFractionDigits(10);
|
||||
fmt.setMaximumFractionDigits(10);
|
||||
for (int i = 0; i < numStates; i++)
|
||||
Log.write("pi(" + i + ") = " + fmt.format(pi[i]));
|
||||
Log.write("");
|
||||
for (int i = 0; i < numStates; i++) {
|
||||
for (int j = 0; j < numStates; j++)
|
||||
Log.write("a(" + i + "," + j + ") = "
|
||||
+ fmt.format(a[i][j]) + " ");
|
||||
Log.write("");
|
||||
}
|
||||
Log.write("");
|
||||
for (int i = 0; i < numStates; i++) {
|
||||
for (int k = 0; k < sigmaSize; k++)
|
||||
Log.write("b(" + i + "," + k + ") = "
|
||||
+ fmt.format(b[i][k]) + " ");
|
||||
Log.write("");
|
||||
}
|
||||
}
|
||||
|
||||
public double[][] getA() {
|
||||
return this.a;
|
||||
}
|
||||
|
||||
public void setA(double[][] a) {
|
||||
this.a = a;
|
||||
}
|
||||
|
||||
public double[][] getB() {
|
||||
return this.b;
|
||||
}
|
||||
|
||||
public void setB(double[][] b) {
|
||||
this.b=b;
|
||||
}
|
||||
public double[][] getA() {
|
||||
return this.a;
|
||||
}
|
||||
|
||||
public void setA(double[][] a) {
|
||||
this.a = a;
|
||||
}
|
||||
|
||||
public double[][] getB() {
|
||||
return this.b;
|
||||
}
|
||||
|
||||
public void setB(double[][] b) {
|
||||
this.b=b;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ public abstract class ProcessingUnit implements AccelerationListener, ButtonList
|
||||
* Add an GestureListener to receive GestureEvents.
|
||||
*
|
||||
* @param g
|
||||
* Class which implements GestureListener interface.
|
||||
* Class which implements GestureListener interface.
|
||||
*/
|
||||
public void addGestureListener(GestureListener g) {
|
||||
this.gesturelistener.add(g);
|
||||
|
||||
@@ -39,271 +39,271 @@ import org.wiigee.util.Log;
|
||||
*/
|
||||
public class Quantizer {
|
||||
|
||||
/** This is the initial radius of this model. */
|
||||
private double radius;
|
||||
/** This is the initial radius of this model. */
|
||||
private double radius;
|
||||
|
||||
/** Number of states from the following Hidden Markov Model */
|
||||
private int numStates;
|
||||
/** Number of states from the following Hidden Markov Model */
|
||||
private int numStates;
|
||||
|
||||
/** The representation of the so called Centeroids */
|
||||
private double[][] map;
|
||||
/** The representation of the so called Centeroids */
|
||||
private double[][] map;
|
||||
|
||||
/** True, if map is already trained. */
|
||||
private boolean maptrained;
|
||||
/** True, if map is already trained. */
|
||||
private boolean maptrained;
|
||||
|
||||
/**
|
||||
* Initialize a empty quantizer. The states variable is necessary since some
|
||||
* algorithms need this value to calculate their values correctly.
|
||||
*
|
||||
* @param numStates
|
||||
* number of hidden markov model states
|
||||
*/
|
||||
public Quantizer(int numStates) {
|
||||
this.numStates = numStates;
|
||||
this.map = new double[14][3];
|
||||
this.maptrained = false;
|
||||
}
|
||||
/**
|
||||
* Initialize a empty quantizer. The states variable is necessary since some
|
||||
* algorithms need this value to calculate their values correctly.
|
||||
*
|
||||
* @param numStates
|
||||
* number of hidden markov model states
|
||||
*/
|
||||
public Quantizer(int numStates) {
|
||||
this.numStates = numStates;
|
||||
this.map = new double[14][3];
|
||||
this.maptrained = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trains this Quantizer with a specific gesture. This means that the
|
||||
* positions of the centeroids would adapt to this training gesture. In our
|
||||
* case this would happen with a summarized virtual gesture, containing all
|
||||
* the other gestures.
|
||||
*
|
||||
* @param gesture
|
||||
* the summarized virtual gesture
|
||||
*/
|
||||
public void trainCenteroids(Gesture gesture) {
|
||||
Vector<AccelerationEvent> data = gesture.getData();
|
||||
double pi = Math.PI;
|
||||
this.radius = (gesture.getMaxAcceleration() + gesture
|
||||
.getMinAcceleration()) / 2;
|
||||
Log.write("Using radius: " + this.radius);
|
||||
/**
|
||||
* Trains this Quantizer with a specific gesture. This means that the
|
||||
* positions of the centeroids would adapt to this training gesture. In our
|
||||
* case this would happen with a summarized virtual gesture, containing all
|
||||
* the other gestures.
|
||||
*
|
||||
* @param gesture
|
||||
* the summarized virtual gesture
|
||||
*/
|
||||
public void trainCenteroids(Gesture gesture) {
|
||||
Vector<AccelerationEvent> data = gesture.getData();
|
||||
double pi = Math.PI;
|
||||
this.radius = (gesture.getMaxAcceleration() + gesture
|
||||
.getMinAcceleration()) / 2;
|
||||
Log.write("Using radius: " + this.radius);
|
||||
|
||||
// x , z , y
|
||||
if (!this.maptrained) {
|
||||
this.maptrained = true;
|
||||
this.map[0] = new double[] { this.radius, 0.0, 0.0 };
|
||||
this.map[1] = new double[] { Math.cos(pi / 4) * this.radius, 0.0,
|
||||
Math.sin(pi / 4) * this.radius };
|
||||
this.map[2] = new double[] { 0.0, 0.0, this.radius };
|
||||
this.map[3] = new double[] { Math.cos(pi * 3 / 4) * this.radius,
|
||||
0.0, Math.sin(pi * 3 / 4) * this.radius };
|
||||
this.map[4] = new double[] { -this.radius, 0.0, 0.0 };
|
||||
this.map[5] = new double[] { Math.cos(pi * 5 / 4) * this.radius,
|
||||
0.0, Math.sin(pi * 5 / 4) * this.radius };
|
||||
this.map[6] = new double[] { 0.0, 0.0, -this.radius };
|
||||
this.map[7] = new double[] { Math.cos(pi * 7 / 4) * this.radius,
|
||||
0.0, Math.sin(pi * 7 / 4) * this.radius };
|
||||
// x , z , y
|
||||
if (!this.maptrained) {
|
||||
this.maptrained = true;
|
||||
this.map[0] = new double[] { this.radius, 0.0, 0.0 };
|
||||
this.map[1] = new double[] { Math.cos(pi / 4) * this.radius, 0.0,
|
||||
Math.sin(pi / 4) * this.radius };
|
||||
this.map[2] = new double[] { 0.0, 0.0, this.radius };
|
||||
this.map[3] = new double[] { Math.cos(pi * 3 / 4) * this.radius,
|
||||
0.0, Math.sin(pi * 3 / 4) * this.radius };
|
||||
this.map[4] = new double[] { -this.radius, 0.0, 0.0 };
|
||||
this.map[5] = new double[] { Math.cos(pi * 5 / 4) * this.radius,
|
||||
0.0, Math.sin(pi * 5 / 4) * this.radius };
|
||||
this.map[6] = new double[] { 0.0, 0.0, -this.radius };
|
||||
this.map[7] = new double[] { Math.cos(pi * 7 / 4) * this.radius,
|
||||
0.0, Math.sin(pi * 7 / 4) * this.radius };
|
||||
|
||||
this.map[8] = new double[] { 0.0, this.radius, 0.0 };
|
||||
this.map[9] = new double[] { 0.0, Math.cos(pi / 4) * this.radius,
|
||||
Math.sin(pi / 4) * this.radius };
|
||||
this.map[10] = new double[] { 0.0,
|
||||
Math.cos(pi * 3 / 4) * this.radius,
|
||||
Math.sin(pi * 3 / 4) * this.radius };
|
||||
this.map[11] = new double[] { 0.0, -this.radius, 0.0 };
|
||||
this.map[12] = new double[] { 0.0,
|
||||
Math.cos(pi * 5 / 4) * this.radius,
|
||||
Math.sin(pi * 5 / 4) * this.radius };
|
||||
this.map[13] = new double[] { 0.0,
|
||||
Math.cos(pi * 7 / 4) * this.radius,
|
||||
Math.sin(pi * 7 / 4) * this.radius };
|
||||
}
|
||||
this.map[8] = new double[] { 0.0, this.radius, 0.0 };
|
||||
this.map[9] = new double[] { 0.0, Math.cos(pi / 4) * this.radius,
|
||||
Math.sin(pi / 4) * this.radius };
|
||||
this.map[10] = new double[] { 0.0,
|
||||
Math.cos(pi * 3 / 4) * this.radius,
|
||||
Math.sin(pi * 3 / 4) * this.radius };
|
||||
this.map[11] = new double[] { 0.0, -this.radius, 0.0 };
|
||||
this.map[12] = new double[] { 0.0,
|
||||
Math.cos(pi * 5 / 4) * this.radius,
|
||||
Math.sin(pi * 5 / 4) * this.radius };
|
||||
this.map[13] = new double[] { 0.0,
|
||||
Math.cos(pi * 7 / 4) * this.radius,
|
||||
Math.sin(pi * 7 / 4) * this.radius };
|
||||
}
|
||||
|
||||
int[][] g_alt = new int[this.map.length][data.size()];
|
||||
int[][] g = new int[this.map.length][data.size()];
|
||||
int[][] g_alt = new int[this.map.length][data.size()];
|
||||
int[][] g = new int[this.map.length][data.size()];
|
||||
|
||||
do {
|
||||
// Derive new Groups...
|
||||
g_alt = this.copyarray(g);
|
||||
g = this.deriveGroups(gesture);
|
||||
do {
|
||||
// Derive new Groups...
|
||||
g_alt = this.copyarray(g);
|
||||
g = this.deriveGroups(gesture);
|
||||
|
||||
// calculate new centeroids
|
||||
for (int i = 0; i < this.map.length; i++) {
|
||||
double zaehlerX = 0;
|
||||
double zaehlerY = 0;
|
||||
double zaehlerZ = 0;
|
||||
int nenner = 0;
|
||||
for (int j = 0; j < data.size(); j++) {
|
||||
if (g[i][j] == 1) {
|
||||
zaehlerX += data.elementAt(j).getX();
|
||||
zaehlerY += data.elementAt(j).getY();
|
||||
zaehlerZ += data.elementAt(j).getZ();
|
||||
nenner++;
|
||||
}
|
||||
}
|
||||
if (nenner > 1) { // nur wenn der nenner>0 oder >1??? ist muss
|
||||
// was
|
||||
// geaendert werden
|
||||
// Log.write("Setze neuen Centeroid!");
|
||||
this.map[i] = new double[] {(zaehlerX / (double) nenner),
|
||||
(zaehlerY / (double) nenner),
|
||||
(zaehlerZ / (double) nenner) };
|
||||
// Log.write("Centeroid: "+i+": "+newcenteroid[0]+":"+newcenteroid[1]);
|
||||
}
|
||||
} // new centeroids
|
||||
// calculate new centeroids
|
||||
for (int i = 0; i < this.map.length; i++) {
|
||||
double zaehlerX = 0;
|
||||
double zaehlerY = 0;
|
||||
double zaehlerZ = 0;
|
||||
int nenner = 0;
|
||||
for (int j = 0; j < data.size(); j++) {
|
||||
if (g[i][j] == 1) {
|
||||
zaehlerX += data.elementAt(j).getX();
|
||||
zaehlerY += data.elementAt(j).getY();
|
||||
zaehlerZ += data.elementAt(j).getZ();
|
||||
nenner++;
|
||||
}
|
||||
}
|
||||
if (nenner > 1) { // nur wenn der nenner>0 oder >1??? ist muss
|
||||
// was
|
||||
// geaendert werden
|
||||
// Log.write("Setze neuen Centeroid!");
|
||||
this.map[i] = new double[] {(zaehlerX / (double) nenner),
|
||||
(zaehlerY / (double) nenner),
|
||||
(zaehlerZ / (double) nenner) };
|
||||
// Log.write("Centeroid: "+i+": "+newcenteroid[0]+":"+newcenteroid[1]);
|
||||
}
|
||||
} // new centeroids
|
||||
|
||||
} while (!equalarrays(g_alt, g));
|
||||
} while (!equalarrays(g_alt, g));
|
||||
|
||||
// Debug: Printout groups
|
||||
/*
|
||||
* for (int i = 0; i < n; i++) { for (int j = 0; j < this.data.size();
|
||||
* j++) { Log.write(g[i][j] + "|"); } Log.write(""); }
|
||||
*/
|
||||
// Debug: Printout groups
|
||||
/*
|
||||
* for (int i = 0; i < n; i++) { for (int j = 0; j < this.data.size();
|
||||
* j++) { Log.write(g[i][j] + "|"); } Log.write(""); }
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This methods looks up a Gesture to a group matrix, used by the
|
||||
* k-mean-algorithm (traincenteroid method) above.
|
||||
*
|
||||
* @param gesture
|
||||
* the gesture
|
||||
*/
|
||||
public int[][] deriveGroups(Gesture gesture) {
|
||||
Vector<AccelerationEvent> data = gesture.getData();
|
||||
int[][] groups = new int[this.map.length][data.size()];
|
||||
/**
|
||||
* This methods looks up a Gesture to a group matrix, used by the
|
||||
* k-mean-algorithm (traincenteroid method) above.
|
||||
*
|
||||
* @param gesture
|
||||
* the gesture
|
||||
*/
|
||||
public int[][] deriveGroups(Gesture gesture) {
|
||||
Vector<AccelerationEvent> data = gesture.getData();
|
||||
int[][] groups = new int[this.map.length][data.size()];
|
||||
|
||||
// Calculate cartesian distance
|
||||
double[][] d = new double[this.map.length][data.size()];
|
||||
double[] curr = new double[3];
|
||||
double[] vector = new double[3];
|
||||
for (int i = 0; i < this.map.length; i++) { // zeilen
|
||||
double[] ref = this.map[i];
|
||||
for (int j = 0; j < data.size(); j++) { // spalten
|
||||
// Calculate cartesian distance
|
||||
double[][] d = new double[this.map.length][data.size()];
|
||||
double[] curr = new double[3];
|
||||
double[] vector = new double[3];
|
||||
for (int i = 0; i < this.map.length; i++) { // zeilen
|
||||
double[] ref = this.map[i];
|
||||
for (int j = 0; j < data.size(); j++) { // spalten
|
||||
|
||||
curr[0] = data.elementAt(j).getX();
|
||||
curr[1] = data.elementAt(j).getY();
|
||||
curr[2] = data.elementAt(j).getZ();
|
||||
curr[0] = data.elementAt(j).getX();
|
||||
curr[1] = data.elementAt(j).getY();
|
||||
curr[2] = data.elementAt(j).getZ();
|
||||
|
||||
vector[0] = ref[0] - curr[0];
|
||||
vector[1] = ref[1] - curr[1];
|
||||
vector[2] = ref[2] - curr[2];
|
||||
d[i][j] = Math.sqrt((vector[0] * vector[0])
|
||||
+ (vector[1] * vector[1]) + (vector[2] * vector[2]));
|
||||
// Log.write(d[i][j] + "|");
|
||||
}
|
||||
// Log.write("");
|
||||
}
|
||||
vector[0] = ref[0] - curr[0];
|
||||
vector[1] = ref[1] - curr[1];
|
||||
vector[2] = ref[2] - curr[2];
|
||||
d[i][j] = Math.sqrt((vector[0] * vector[0])
|
||||
+ (vector[1] * vector[1]) + (vector[2] * vector[2]));
|
||||
// Log.write(d[i][j] + "|");
|
||||
}
|
||||
// Log.write("");
|
||||
}
|
||||
|
||||
// look, to which group a value belongs
|
||||
for (int j = 0; j < data.size(); j++) {
|
||||
double smallest = Double.MAX_VALUE;
|
||||
int row = 0;
|
||||
for (int i = 0; i < this.map.length; i++) {
|
||||
if (d[i][j] < smallest) {
|
||||
smallest = d[i][j];
|
||||
row = i;
|
||||
}
|
||||
groups[i][j] = 0;
|
||||
}
|
||||
groups[row][j] = 1; // guppe gesetzt
|
||||
}
|
||||
// look, to which group a value belongs
|
||||
for (int j = 0; j < data.size(); j++) {
|
||||
double smallest = Double.MAX_VALUE;
|
||||
int row = 0;
|
||||
for (int i = 0; i < this.map.length; i++) {
|
||||
if (d[i][j] < smallest) {
|
||||
smallest = d[i][j];
|
||||
row = i;
|
||||
}
|
||||
groups[i][j] = 0;
|
||||
}
|
||||
groups[row][j] = 1; // guppe gesetzt
|
||||
}
|
||||
|
||||
// Debug output
|
||||
/*
|
||||
* for (int i = 0; i < groups.length; i++) { // zeilen for (int j = 0; j
|
||||
* < groups[i].length; j++) { Log.write(groups[i][j] + "|"); }
|
||||
* Log.write(""); }
|
||||
*/
|
||||
// Debug output
|
||||
/*
|
||||
* for (int i = 0; i < groups.length; i++) { // zeilen for (int j = 0; j
|
||||
* < groups[i].length; j++) { Log.write(groups[i][j] + "|"); }
|
||||
* Log.write(""); }
|
||||
*/
|
||||
|
||||
return groups;
|
||||
return groups;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* With this method you can transform a gesture to a discrete symbol
|
||||
* sequence with values between 0 and granularity (number of observations).
|
||||
*
|
||||
* @param gesture
|
||||
* Gesture to get the observationsequence to.
|
||||
*/
|
||||
public int[] getObservationSequence(Gesture gesture) {
|
||||
int[][] groups = this.deriveGroups(gesture);
|
||||
Vector<Integer> sequence = new Vector<Integer>();
|
||||
/**
|
||||
* With this method you can transform a gesture to a discrete symbol
|
||||
* sequence with values between 0 and granularity (number of observations).
|
||||
*
|
||||
* @param gesture
|
||||
* Gesture to get the observationsequence to.
|
||||
*/
|
||||
public int[] getObservationSequence(Gesture gesture) {
|
||||
int[][] groups = this.deriveGroups(gesture);
|
||||
Vector<Integer> sequence = new Vector<Integer>();
|
||||
|
||||
// Log.write("Visible symbol sequence: ");
|
||||
// Log.write("Visible symbol sequence: ");
|
||||
|
||||
for (int j = 0; j < groups[0].length; j++) { // spalten
|
||||
for (int i = 0; i < groups.length; i++) { // zeilen
|
||||
if (groups[i][j] == 1) {
|
||||
// Log.write(" "+ i);
|
||||
sequence.add(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int j = 0; j < groups[0].length; j++) { // spalten
|
||||
for (int i = 0; i < groups.length; i++) { // zeilen
|
||||
if (groups[i][j] == 1) {
|
||||
// Log.write(" "+ i);
|
||||
sequence.add(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// die sequenz darf nicht zu kurz sein... mindestens so lang
|
||||
// wie die anzahl der zustände. weil sonst die formeln nicht klappen.
|
||||
// english: this is very dirty! it have to be here because if not
|
||||
// too short sequences would cause an error. i've to think about a
|
||||
// better resolution than copying the old value a few time.
|
||||
while (sequence.size() < this.numStates) {
|
||||
sequence.add(sequence.elementAt(sequence.size() - 1));
|
||||
// Log.write(" "+sequence.elementAt(sequence.size()-1));
|
||||
}
|
||||
// die sequenz darf nicht zu kurz sein... mindestens so lang
|
||||
// wie die anzahl der zustände. weil sonst die formeln nicht klappen.
|
||||
// english: this is very dirty! it have to be here because if not
|
||||
// too short sequences would cause an error. i've to think about a
|
||||
// better resolution than copying the old value a few time.
|
||||
while (sequence.size() < this.numStates) {
|
||||
sequence.add(sequence.elementAt(sequence.size() - 1));
|
||||
// Log.write(" "+sequence.elementAt(sequence.size()-1));
|
||||
}
|
||||
|
||||
// Log.write("");
|
||||
// Log.write("");
|
||||
|
||||
int[] out = new int[sequence.size()];
|
||||
for (int i = 0; i < sequence.size(); i++) {
|
||||
out[i] = sequence.elementAt(i);
|
||||
}
|
||||
int[] out = new int[sequence.size()];
|
||||
for (int i = 0; i < sequence.size(); i++) {
|
||||
out[i] = sequence.elementAt(i);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints out the current centeroids-map. Its for debug or technical
|
||||
* interests.
|
||||
*/
|
||||
public void printMap() {
|
||||
Log.write("Centeroids:");
|
||||
for (int i = 0; i < this.map.length; i++) {
|
||||
Log.write(i + ". :" + this.map[i][0] + ":"
|
||||
+ this.map[i][1] + ":" + this.map[i][2]);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Prints out the current centeroids-map. Its for debug or technical
|
||||
* interests.
|
||||
*/
|
||||
public void printMap() {
|
||||
Log.write("Centeroids:");
|
||||
for (int i = 0; i < this.map.length; i++) {
|
||||
Log.write(i + ". :" + this.map[i][0] + ":"
|
||||
+ this.map[i][1] + ":" + this.map[i][2]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to deepcopy an array.
|
||||
*/
|
||||
private int[][] copyarray(int[][] alt) {
|
||||
int[][] neu = new int[alt.length][alt[0].length];
|
||||
for (int i = 0; i < alt.length; i++) {
|
||||
for (int j = 0; j < alt[i].length; j++) {
|
||||
neu[i][j] = alt[i][j];
|
||||
}
|
||||
}
|
||||
return neu;
|
||||
}
|
||||
/**
|
||||
* Function to deepcopy an array.
|
||||
*/
|
||||
private int[][] copyarray(int[][] alt) {
|
||||
int[][] neu = new int[alt.length][alt[0].length];
|
||||
for (int i = 0; i < alt.length; i++) {
|
||||
for (int j = 0; j < alt[i].length; j++) {
|
||||
neu[i][j] = alt[i][j];
|
||||
}
|
||||
}
|
||||
return neu;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to look if the two arrays containing the same values.
|
||||
*/
|
||||
private boolean equalarrays(int[][] one, int[][] two) {
|
||||
for (int i = 0; i < one.length; i++) {
|
||||
for (int j = 0; j < one[i].length; j++) {
|
||||
if (!(one[i][j] == two[i][j])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Function to look if the two arrays containing the same values.
|
||||
*/
|
||||
private boolean equalarrays(int[][] one, int[][] two) {
|
||||
for (int i = 0; i < one.length; i++) {
|
||||
for (int j = 0; j < one[i].length; j++) {
|
||||
if (!(one[i][j] == two[i][j])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public double getRadius() {
|
||||
return this.radius;
|
||||
}
|
||||
public double getRadius() {
|
||||
return this.radius;
|
||||
}
|
||||
|
||||
public double[][] getHashMap() {
|
||||
return this.map;
|
||||
}
|
||||
public double[][] getHashMap() {
|
||||
return this.map;
|
||||
}
|
||||
|
||||
public void setUpManually(double[][] map, double radius) {
|
||||
this.map = map;
|
||||
this.radius = radius;
|
||||
}
|
||||
public void setUpManually(double[][] map, double radius) {
|
||||
this.map = map;
|
||||
this.radius = radius;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,152 +37,152 @@ import org.wiigee.util.Log;
|
||||
*/
|
||||
public class TriggeredProcessingUnit extends ProcessingUnit {
|
||||
|
||||
// gesturespecific values
|
||||
private Gesture current; // current gesture
|
||||
// gesturespecific values
|
||||
private Gesture current; // current gesture
|
||||
|
||||
private Vector<Gesture> trainsequence;
|
||||
|
||||
// State variables
|
||||
private boolean learning, analyzing;
|
||||
|
||||
public TriggeredProcessingUnit() {
|
||||
super();
|
||||
this.learning=false;
|
||||
this.analyzing=false;
|
||||
this.current=new Gesture();
|
||||
this.trainsequence=new Vector<Gesture>();
|
||||
}
|
||||
private Vector<Gesture> trainsequence;
|
||||
|
||||
// State variables
|
||||
private boolean learning, analyzing;
|
||||
|
||||
public TriggeredProcessingUnit() {
|
||||
super();
|
||||
this.learning=false;
|
||||
this.analyzing=false;
|
||||
this.current=new Gesture();
|
||||
this.trainsequence=new Vector<Gesture>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Since this class implements the WiimoteListener this procedure is
|
||||
* necessary. It contains the filtering (directional equivalence filter)
|
||||
* and adds the incoming data to the current motion, we want to train
|
||||
* or recognize.
|
||||
*
|
||||
* @param event The acceleration event which has to be processed by the
|
||||
* directional equivalence filter and which has to be added to the current
|
||||
* motion in recognition or training process.
|
||||
*/
|
||||
public void accelerationReceived(AccelerationEvent event) {
|
||||
if(this.learning || this.analyzing) {
|
||||
this.current.add(event); // add event to gesture
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Since this class implements the WiimoteListener this procedure is
|
||||
* necessary. It contains the filtering (directional equivalence filter)
|
||||
* and adds the incoming data to the current motion, we want to train
|
||||
* or recognize.
|
||||
*
|
||||
* @param event The acceleration event which has to be processed by the
|
||||
* directional equivalence filter and which has to be added to the current
|
||||
* motion in recognition or training process.
|
||||
*/
|
||||
public void accelerationReceived(AccelerationEvent event) {
|
||||
if(this.learning || this.analyzing) {
|
||||
this.current.add(event); // add event to gesture
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is from the WiimoteListener interface. A button press
|
||||
* is used to control the data flow inside the structures.
|
||||
*
|
||||
*/
|
||||
public void buttonPressReceived(ButtonPressedEvent event) {
|
||||
this.handleStartEvent(event);
|
||||
}
|
||||
/**
|
||||
* This method is from the WiimoteListener interface. A button press
|
||||
* is used to control the data flow inside the structures.
|
||||
*
|
||||
*/
|
||||
public void buttonPressReceived(ButtonPressedEvent event) {
|
||||
this.handleStartEvent(event);
|
||||
}
|
||||
|
||||
public void buttonReleaseReceived(ButtonReleasedEvent event) {
|
||||
this.handleStopEvent(event);
|
||||
}
|
||||
|
||||
public void motionStartReceived(MotionStartEvent event) {
|
||||
// this.handleStartEvent(event);
|
||||
}
|
||||
|
||||
public void motionStopReceived(MotionStopEvent event) {
|
||||
// this.handleStopEvent(event);
|
||||
}
|
||||
|
||||
public void handleStartEvent(ActionStartEvent event) {
|
||||
|
||||
// TrainButton = record a gesture for learning
|
||||
if((!this.analyzing && !this.learning) &&
|
||||
event.isTrainInitEvent()) {
|
||||
Log.write("Training started!");
|
||||
this.learning=true;
|
||||
}
|
||||
|
||||
// RecognitionButton = record a gesture for recognition
|
||||
if((!this.analyzing && !this.learning) &&
|
||||
event.isRecognitionInitEvent()) {
|
||||
Log.write("Recognition started!");
|
||||
this.analyzing=true;
|
||||
}
|
||||
|
||||
// CloseGestureButton = starts the training of the model with multiple
|
||||
// recognized gestures, contained in trainsequence
|
||||
if((!this.analyzing && !this.learning) &&
|
||||
event.isCloseGestureInitEvent()) {
|
||||
|
||||
if(this.trainsequence.size()>0) {
|
||||
Log.write("Training the model with "+this.trainsequence.size()+" gestures...");
|
||||
this.learning=true;
|
||||
|
||||
GestureModel m = new GestureModel();
|
||||
m.train(this.trainsequence);
|
||||
m.print();
|
||||
this.classifier.addGestureModel(m);
|
||||
|
||||
this.trainsequence=new Vector<Gesture>();
|
||||
this.learning=false;
|
||||
} else {
|
||||
Log.write("There is nothing to do. Please record some gestures first.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void handleStopEvent(ActionStopEvent event) {
|
||||
if(this.learning) { // button release and state=learning, stops learning
|
||||
if(this.current.getCountOfData()>0) {
|
||||
Log.write("Finished recording (training)...");
|
||||
Log.write("Data: "+this.current.getCountOfData());
|
||||
Gesture gesture = new Gesture(this.current);
|
||||
this.trainsequence.add(gesture);
|
||||
this.current=new Gesture();
|
||||
this.learning=false;
|
||||
} else {
|
||||
Log.write("There is no data.");
|
||||
Log.write("Please train the gesture again.");
|
||||
this.learning=false; // ?
|
||||
}
|
||||
}
|
||||
|
||||
else if(this.analyzing) { // button release and state=analyzing, stops analyzing
|
||||
if(this.current.getCountOfData()>0) {
|
||||
Log.write("Finished recording (recognition)...");
|
||||
Log.write("Compare gesture with "+this.classifier.getCountOfGestures()+" other gestures.");
|
||||
Gesture gesture = new Gesture(this.current);
|
||||
|
||||
int recognized = this.classifier.classifyGesture(gesture);
|
||||
if(recognized!=-1) {
|
||||
double recogprob = this.classifier.getLastProbability();
|
||||
this.fireGestureEvent(true, recognized, recogprob);
|
||||
Log.write("######");
|
||||
Log.write("Gesture No. "+recognized+" recognized: "+recogprob);
|
||||
Log.write("######");
|
||||
} else {
|
||||
this.fireGestureEvent(false, 0, 0.0);
|
||||
Log.write("######");
|
||||
Log.write("No gesture recognized.");
|
||||
Log.write("######");
|
||||
}
|
||||
|
||||
this.current=new Gesture();
|
||||
this.analyzing=false;
|
||||
} else {
|
||||
Log.write("There is no data.");
|
||||
Log.write("Please recognize the gesture again.");
|
||||
this.analyzing=false; // ?
|
||||
}
|
||||
}
|
||||
}
|
||||
public void buttonReleaseReceived(ButtonReleasedEvent event) {
|
||||
this.handleStopEvent(event);
|
||||
}
|
||||
|
||||
public void motionStartReceived(MotionStartEvent event) {
|
||||
// this.handleStartEvent(event);
|
||||
}
|
||||
|
||||
public void motionStopReceived(MotionStopEvent event) {
|
||||
// this.handleStopEvent(event);
|
||||
}
|
||||
|
||||
public void handleStartEvent(ActionStartEvent event) {
|
||||
|
||||
// TrainButton = record a gesture for learning
|
||||
if((!this.analyzing && !this.learning) &&
|
||||
event.isTrainInitEvent()) {
|
||||
Log.write("Training started!");
|
||||
this.learning=true;
|
||||
}
|
||||
|
||||
// RecognitionButton = record a gesture for recognition
|
||||
if((!this.analyzing && !this.learning) &&
|
||||
event.isRecognitionInitEvent()) {
|
||||
Log.write("Recognition started!");
|
||||
this.analyzing=true;
|
||||
}
|
||||
|
||||
// CloseGestureButton = starts the training of the model with multiple
|
||||
// recognized gestures, contained in trainsequence
|
||||
if((!this.analyzing && !this.learning) &&
|
||||
event.isCloseGestureInitEvent()) {
|
||||
|
||||
if(this.trainsequence.size()>0) {
|
||||
Log.write("Training the model with "+this.trainsequence.size()+" gestures...");
|
||||
this.learning=true;
|
||||
|
||||
GestureModel m = new GestureModel();
|
||||
m.train(this.trainsequence);
|
||||
m.print();
|
||||
this.classifier.addGestureModel(m);
|
||||
|
||||
this.trainsequence=new Vector<Gesture>();
|
||||
this.learning=false;
|
||||
} else {
|
||||
Log.write("There is nothing to do. Please record some gestures first.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void handleStopEvent(ActionStopEvent event) {
|
||||
if(this.learning) { // button release and state=learning, stops learning
|
||||
if(this.current.getCountOfData()>0) {
|
||||
Log.write("Finished recording (training)...");
|
||||
Log.write("Data: "+this.current.getCountOfData());
|
||||
Gesture gesture = new Gesture(this.current);
|
||||
this.trainsequence.add(gesture);
|
||||
this.current=new Gesture();
|
||||
this.learning=false;
|
||||
} else {
|
||||
Log.write("There is no data.");
|
||||
Log.write("Please train the gesture again.");
|
||||
this.learning=false; // ?
|
||||
}
|
||||
}
|
||||
|
||||
else if(this.analyzing) { // button release and state=analyzing, stops analyzing
|
||||
if(this.current.getCountOfData()>0) {
|
||||
Log.write("Finished recording (recognition)...");
|
||||
Log.write("Compare gesture with "+this.classifier.getCountOfGestures()+" other gestures.");
|
||||
Gesture gesture = new Gesture(this.current);
|
||||
|
||||
int recognized = this.classifier.classifyGesture(gesture);
|
||||
if(recognized!=-1) {
|
||||
double recogprob = this.classifier.getLastProbability();
|
||||
this.fireGestureEvent(true, recognized, recogprob);
|
||||
Log.write("######");
|
||||
Log.write("Gesture No. "+recognized+" recognized: "+recogprob);
|
||||
Log.write("######");
|
||||
} else {
|
||||
this.fireGestureEvent(false, 0, 0.0);
|
||||
Log.write("######");
|
||||
Log.write("No gesture recognized.");
|
||||
Log.write("######");
|
||||
}
|
||||
|
||||
this.current=new Gesture();
|
||||
this.analyzing=false;
|
||||
} else {
|
||||
Log.write("There is no data.");
|
||||
Log.write("Please recognize the gesture again.");
|
||||
this.analyzing=false; // ?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadGesture(String filename) {
|
||||
GestureModel g = org.wiigee.util.FileIO.readFromFile(filename);
|
||||
this.classifier.addGestureModel(g);
|
||||
}
|
||||
@Override
|
||||
public void loadGesture(String filename) {
|
||||
GestureModel g = org.wiigee.util.FileIO.readFromFile(filename);
|
||||
this.classifier.addGestureModel(g);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveGesture(int id, String filename) {
|
||||
org.wiigee.util.FileIO.writeToFile(this.classifier.getGestureModel(id), filename);
|
||||
}
|
||||
@Override
|
||||
public void saveGesture(int id, String filename) {
|
||||
org.wiigee.util.FileIO.writeToFile(this.classifier.getGestureModel(id), filename);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -44,190 +44,190 @@ import org.wiigee.logic.Quantizer;
|
||||
*/
|
||||
public class FileIO {
|
||||
|
||||
public static void writeToFile(GestureModel m, String name) {
|
||||
try {
|
||||
// initialize file and get values
|
||||
BufferedWriter out = new BufferedWriter(new FileWriter(name+".txt"));
|
||||
int numStates = m.getNumStates();
|
||||
int numObservations = m.getNumObservations();
|
||||
double defaultProbability = m.getDefaultProbability();
|
||||
Quantizer quantizer = m.getQuantizer();
|
||||
HMM hmm = m.getHMM();
|
||||
|
||||
out.write("# numStates:");
|
||||
out.newLine();
|
||||
out.write(Integer.toString(numStates));
|
||||
out.newLine();
|
||||
|
||||
out.write("# numObservations:");
|
||||
out.newLine();
|
||||
out.write(Integer.toString(numObservations));
|
||||
out.newLine();
|
||||
|
||||
out.write("# defaultProbability:");
|
||||
out.newLine();
|
||||
out.write(Double.toString(defaultProbability));
|
||||
out.newLine();
|
||||
|
||||
out.write("# Quantizer: Radius");
|
||||
out.newLine();
|
||||
out.write(Double.toString(quantizer.getRadius()));
|
||||
out.newLine();
|
||||
out.write("# Quantizer: MAP");
|
||||
out.newLine();
|
||||
double[][] map = quantizer.getHashMap();
|
||||
for(int v=0; v<map.length; v++) {
|
||||
double[] d = map[v];
|
||||
out.write(Double.toString(d[0])+", "+Double.toString(d[1])+", "+Double.toString(d[2]));
|
||||
out.newLine();
|
||||
}
|
||||
|
||||
out.write("# HMM: PI");
|
||||
out.newLine();
|
||||
double[] pi = hmm.getPi();
|
||||
for (int i=0; i<numStates; i++) {
|
||||
if(i==numStates-1) {
|
||||
out.write(Double.toString(pi[i]));
|
||||
out.newLine();
|
||||
} else {
|
||||
out.write(Double.toString(pi[i])+", ");
|
||||
}
|
||||
}
|
||||
|
||||
out.write("# HMM: A");
|
||||
out.newLine();
|
||||
double[][] a = hmm.getA();
|
||||
for(int i=0; i<numStates; i++) {
|
||||
for(int j=0; j<numStates; j++) {
|
||||
if(j==numStates-1) {
|
||||
out.write(Double.toString(a[i][j]));
|
||||
out.newLine();
|
||||
} else {
|
||||
out.write(Double.toString(a[i][j])+", ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
out.write("# HMM: B");
|
||||
out.newLine();
|
||||
double[][] b = hmm.getB();
|
||||
for(int i=0; i<numStates; i++) {
|
||||
for(int j=0; j<numObservations; j++) {
|
||||
if(j==numObservations-1) {
|
||||
out.write(Double.toString(b[i][j]));
|
||||
out.newLine();
|
||||
} else {
|
||||
out.write(Double.toString(b[i][j])+", ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
out.write("# END");
|
||||
|
||||
// close file
|
||||
out.flush();
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error: Write to File!");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
public static void writeToFile(GestureModel m, String name) {
|
||||
try {
|
||||
// initialize file and get values
|
||||
BufferedWriter out = new BufferedWriter(new FileWriter(name+".txt"));
|
||||
int numStates = m.getNumStates();
|
||||
int numObservations = m.getNumObservations();
|
||||
double defaultProbability = m.getDefaultProbability();
|
||||
Quantizer quantizer = m.getQuantizer();
|
||||
HMM hmm = m.getHMM();
|
||||
|
||||
out.write("# numStates:");
|
||||
out.newLine();
|
||||
out.write(Integer.toString(numStates));
|
||||
out.newLine();
|
||||
|
||||
out.write("# numObservations:");
|
||||
out.newLine();
|
||||
out.write(Integer.toString(numObservations));
|
||||
out.newLine();
|
||||
|
||||
out.write("# defaultProbability:");
|
||||
out.newLine();
|
||||
out.write(Double.toString(defaultProbability));
|
||||
out.newLine();
|
||||
|
||||
out.write("# Quantizer: Radius");
|
||||
out.newLine();
|
||||
out.write(Double.toString(quantizer.getRadius()));
|
||||
out.newLine();
|
||||
out.write("# Quantizer: MAP");
|
||||
out.newLine();
|
||||
double[][] map = quantizer.getHashMap();
|
||||
for(int v=0; v<map.length; v++) {
|
||||
double[] d = map[v];
|
||||
out.write(Double.toString(d[0])+", "+Double.toString(d[1])+", "+Double.toString(d[2]));
|
||||
out.newLine();
|
||||
}
|
||||
|
||||
out.write("# HMM: PI");
|
||||
out.newLine();
|
||||
double[] pi = hmm.getPi();
|
||||
for (int i=0; i<numStates; i++) {
|
||||
if(i==numStates-1) {
|
||||
out.write(Double.toString(pi[i]));
|
||||
out.newLine();
|
||||
} else {
|
||||
out.write(Double.toString(pi[i])+", ");
|
||||
}
|
||||
}
|
||||
|
||||
out.write("# HMM: A");
|
||||
out.newLine();
|
||||
double[][] a = hmm.getA();
|
||||
for(int i=0; i<numStates; i++) {
|
||||
for(int j=0; j<numStates; j++) {
|
||||
if(j==numStates-1) {
|
||||
out.write(Double.toString(a[i][j]));
|
||||
out.newLine();
|
||||
} else {
|
||||
out.write(Double.toString(a[i][j])+", ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
out.write("# HMM: B");
|
||||
out.newLine();
|
||||
double[][] b = hmm.getB();
|
||||
for(int i=0; i<numStates; i++) {
|
||||
for(int j=0; j<numObservations; j++) {
|
||||
if(j==numObservations-1) {
|
||||
out.write(Double.toString(b[i][j]));
|
||||
out.newLine();
|
||||
} else {
|
||||
out.write(Double.toString(b[i][j])+", ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
out.write("# END");
|
||||
|
||||
// close file
|
||||
out.flush();
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error: Write to File!");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static GestureModel readFromFile(String name) {
|
||||
try {
|
||||
// initialize file and create values
|
||||
BufferedReader in = new BufferedReader(new FileReader(name+".txt"));
|
||||
int numStates = 0;
|
||||
int numObservations = 0;
|
||||
double defaultprobability = 0;
|
||||
double radius = 0;
|
||||
double[][] map = new double[numObservations][3];
|
||||
double[] pi = new double[numStates];
|
||||
double[][] a = new double[numStates][numStates];
|
||||
double[][] b = new double[numStates][numObservations];
|
||||
|
||||
String line;
|
||||
int position = 0;
|
||||
while(in.ready()) {
|
||||
line = in.readLine();
|
||||
if(!line.startsWith("#")) { // isn't a comment
|
||||
switch (position++) {
|
||||
case 0:
|
||||
numStates = Integer.parseInt(line);
|
||||
break;
|
||||
case 1:
|
||||
numObservations = Integer.parseInt(line);
|
||||
break;
|
||||
case 2:
|
||||
defaultprobability = Double.parseDouble(line);
|
||||
break;
|
||||
case 3:
|
||||
radius = Double.parseDouble(line);
|
||||
break;
|
||||
case 4:
|
||||
map = new double[numObservations][3];
|
||||
for(int i=0; i<numObservations; i++) {
|
||||
String s[] = line.split(", ");
|
||||
double[] d = new double[] { Double.parseDouble(s[0]),
|
||||
Double.parseDouble(s[1]),
|
||||
Double.parseDouble(s[2]) };
|
||||
map[i] = d;
|
||||
line = in.ready() ? in.readLine() : "";
|
||||
}
|
||||
break;
|
||||
case 5:
|
||||
pi = new double[numStates];
|
||||
String pi_row[] = line.split(", ");
|
||||
for(int i=0; i<numStates; i++) {
|
||||
pi[i] = Double.parseDouble(pi_row[i]);
|
||||
}
|
||||
break;
|
||||
case 6:
|
||||
a = new double[numStates][numStates];
|
||||
for(int i=0; i<numStates; i++) {
|
||||
String a_row[] = line.split(", ");
|
||||
for(int j=0; j<numStates; j++) {
|
||||
a[i][j] = Double.parseDouble(a_row[j]);
|
||||
}
|
||||
line = in.ready() ? in.readLine() : "";
|
||||
}
|
||||
break;
|
||||
case 7:
|
||||
b = new double[numStates][numObservations];
|
||||
for(int i=0; i<numStates; i++) {
|
||||
String b_row[] = line.split(", ");
|
||||
for(int j=0; j<numObservations; j++) {
|
||||
b[i][j] = Double.parseDouble(b_row[j]);
|
||||
}
|
||||
line = in.ready() ? in.readLine() : "";
|
||||
}
|
||||
break;
|
||||
default:
|
||||
System.out.println("SWITCH EMPTY!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GestureModel ret = new GestureModel();
|
||||
ret.setDefaultProbability(defaultprobability);
|
||||
|
||||
Quantizer quantizer = new Quantizer(numStates);
|
||||
quantizer.setUpManually(map, radius);
|
||||
ret.setQuantizer(quantizer);
|
||||
|
||||
HMM hmm = new HMM(numStates, numObservations);
|
||||
hmm.setPi(pi);
|
||||
hmm.setA(a);
|
||||
hmm.setB(b);
|
||||
ret.setHMM(hmm);
|
||||
|
||||
return ret;
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error: Read from File!");
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null; // bad error case
|
||||
}
|
||||
public static GestureModel readFromFile(String name) {
|
||||
try {
|
||||
// initialize file and create values
|
||||
BufferedReader in = new BufferedReader(new FileReader(name+".txt"));
|
||||
int numStates = 0;
|
||||
int numObservations = 0;
|
||||
double defaultprobability = 0;
|
||||
double radius = 0;
|
||||
double[][] map = new double[numObservations][3];
|
||||
double[] pi = new double[numStates];
|
||||
double[][] a = new double[numStates][numStates];
|
||||
double[][] b = new double[numStates][numObservations];
|
||||
|
||||
String line;
|
||||
int position = 0;
|
||||
while(in.ready()) {
|
||||
line = in.readLine();
|
||||
if(!line.startsWith("#")) { // isn't a comment
|
||||
switch (position++) {
|
||||
case 0:
|
||||
numStates = Integer.parseInt(line);
|
||||
break;
|
||||
case 1:
|
||||
numObservations = Integer.parseInt(line);
|
||||
break;
|
||||
case 2:
|
||||
defaultprobability = Double.parseDouble(line);
|
||||
break;
|
||||
case 3:
|
||||
radius = Double.parseDouble(line);
|
||||
break;
|
||||
case 4:
|
||||
map = new double[numObservations][3];
|
||||
for(int i=0; i<numObservations; i++) {
|
||||
String s[] = line.split(", ");
|
||||
double[] d = new double[] { Double.parseDouble(s[0]),
|
||||
Double.parseDouble(s[1]),
|
||||
Double.parseDouble(s[2]) };
|
||||
map[i] = d;
|
||||
line = in.ready() ? in.readLine() : "";
|
||||
}
|
||||
break;
|
||||
case 5:
|
||||
pi = new double[numStates];
|
||||
String pi_row[] = line.split(", ");
|
||||
for(int i=0; i<numStates; i++) {
|
||||
pi[i] = Double.parseDouble(pi_row[i]);
|
||||
}
|
||||
break;
|
||||
case 6:
|
||||
a = new double[numStates][numStates];
|
||||
for(int i=0; i<numStates; i++) {
|
||||
String a_row[] = line.split(", ");
|
||||
for(int j=0; j<numStates; j++) {
|
||||
a[i][j] = Double.parseDouble(a_row[j]);
|
||||
}
|
||||
line = in.ready() ? in.readLine() : "";
|
||||
}
|
||||
break;
|
||||
case 7:
|
||||
b = new double[numStates][numObservations];
|
||||
for(int i=0; i<numStates; i++) {
|
||||
String b_row[] = line.split(", ");
|
||||
for(int j=0; j<numObservations; j++) {
|
||||
b[i][j] = Double.parseDouble(b_row[j]);
|
||||
}
|
||||
line = in.ready() ? in.readLine() : "";
|
||||
}
|
||||
break;
|
||||
default:
|
||||
System.out.println("SWITCH EMPTY!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GestureModel ret = new GestureModel();
|
||||
ret.setDefaultProbability(defaultprobability);
|
||||
|
||||
Quantizer quantizer = new Quantizer(numStates);
|
||||
quantizer.setUpManually(map, radius);
|
||||
ret.setQuantizer(quantizer);
|
||||
|
||||
HMM hmm = new HMM(numStates, numObservations);
|
||||
hmm.setPi(pi);
|
||||
hmm.setA(a);
|
||||
hmm.setB(b);
|
||||
ret.setHMM(hmm);
|
||||
|
||||
return ret;
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error: Read from File!");
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null; // bad error case
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,42 +2,42 @@ package org.wiigee.util;
|
||||
|
||||
public class Log {
|
||||
|
||||
public static final int OFF = -1;
|
||||
public static final int NORMAL = 0;
|
||||
public static final int DEBUG = 1;
|
||||
|
||||
public static final int PRINT = 0;
|
||||
public static final int FILE = 1;
|
||||
|
||||
public static int level = Log.NORMAL;
|
||||
public static int mode = Log.PRINT;
|
||||
|
||||
public static void setLevel(int n) {
|
||||
level = n;
|
||||
}
|
||||
|
||||
public static void write(String s) {
|
||||
write(Log.NORMAL, s, null);
|
||||
}
|
||||
|
||||
public static void write(String s, Object o) {
|
||||
write(Log.NORMAL, s, o);
|
||||
}
|
||||
|
||||
public static void write(int n, String s, Object o) {
|
||||
if(level>=n) {
|
||||
if(mode==Log.PRINT) {
|
||||
// console output enabled
|
||||
if(o!=null) {
|
||||
System.out.println(o.getClass()+": "+s);
|
||||
} else {
|
||||
System.out.println(s);
|
||||
}
|
||||
} else if(mode==Log.FILE) {
|
||||
// file output enabled
|
||||
// TODO
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static final int OFF = -1;
|
||||
public static final int NORMAL = 0;
|
||||
public static final int DEBUG = 1;
|
||||
|
||||
public static final int PRINT = 0;
|
||||
public static final int FILE = 1;
|
||||
|
||||
public static int level = Log.NORMAL;
|
||||
public static int mode = Log.PRINT;
|
||||
|
||||
public static void setLevel(int n) {
|
||||
level = n;
|
||||
}
|
||||
|
||||
public static void write(String s) {
|
||||
write(Log.NORMAL, s, null);
|
||||
}
|
||||
|
||||
public static void write(String s, Object o) {
|
||||
write(Log.NORMAL, s, o);
|
||||
}
|
||||
|
||||
public static void write(int n, String s, Object o) {
|
||||
if(level>=n) {
|
||||
if(mode==Log.PRINT) {
|
||||
// console output enabled
|
||||
if(o!=null) {
|
||||
System.out.println(o.getClass()+": "+s);
|
||||
} else {
|
||||
System.out.println(s);
|
||||
}
|
||||
} else if(mode==Log.FILE) {
|
||||
// file output enabled
|
||||
// TODO
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user