February 2012
M T W T F S S
« Jul «-»  
 12345
6789101112
13141516171819
20212223242526
272829  

Leaving Chris World?

Why not bring back a souvenir?

Archives

Language Translation

As you may or may not know I endeavor to
use the Nintendo WiiMote to collect position data.
As noted in this post. in which I first set up the theoretical foundation for myself.

I write best in python. But since then the tide has been changing
towards java. Processing.org provided me an API
with a lot of power and flexibility and if I ever intended to
make my software useful. I'd have to recode the thing in java.

Hence, tonight's little quest. (Very little)

Take a look at both the python and the java versions.
You'll notice some dramatic differences in size, and I think
you'll quickly see why I prefer python over java.

This is how my Acceleration to Position function looks
in python...

 
# Functionalized Acceleration to Location
# July 16, 2008 Wednesday
# Chris B Stones
 
# input a list of accelerations
#  provide the initial values of x and v and chose your time slice
def AccToLocation(data,x,v,DELTA=.1):
	#DELTA = .1  # the Time slice
	n = 0       # index value
 
	# Init values
	x = 0.0
	v = 0.0
	FINAL_N = len(data) # cont. the sim until we run out of acc data
 
	# output is a table:  n,a,v,x,t
	output = []
	v_next = v
	x_next = x
	for n in range(FINAL_N):
		a = data.pop(0)
		v_last = v_next
		x_last = x_next
		x_next = a*DELTA**2 + v_last*DELTA + x_last
		output.append((n,a,v_last,x_next,n*DELTA))
		v_next = a*DELTA + v_last
 
	#print "%s,%s,%s,%s,%s" % ('n','a','v','x','t')
 
	#for n,a,v,x,t in output:
	#	print "%f,%f,%f,%f,%f" % (n,a,v,x,t)
 
	return output
 

And this is how it looks in java...


 
/*
 Written at Mosgos while Rosie Studied Some sort of biology thing.
 September 24, 2008
*/
import java.util.ArrayList;
 
public class AccToLoc {
    // python never had to declare these specifically
	double a;
	double v_last;
	double x_last;
 
	double DELTA = 0.1; // x frames per sec 1/xframesPerSec  1/25 0.04
	int	   n;
	double x;
	double v;
	// must init to length of data
	int FINAL_N;// = len(data) // cont. the sim until we run out of acc data
 
	//	# output is a table:  n,a,v,x,t
	String[] output;
	double v_next;
	double x_next;
 
	// so I can use the .add method
	ArrayList n_lst;
	ArrayList a_lst;
	ArrayList v_lst;
	ArrayList x_lst;
	ArrayList t_lst;
 
	// construct with
	public AccToLoc(double[] data, double DELTA, int FINAL_N) {
 
		DELTA = 0.1; // x frames per sec 1/xframesPerSec  1/25 0.04
		n = 0;
		x = 0.0;
		v = 0.0;
		// must init to length of data
		FINAL_N = 3;//len(data) // cont. the sim until we run out of acc data
 
		//	# output is a table:  n,a,v,x,t strings of values
		 v_next = v;
		 x_next = x;
 
		// so I can use the .add method
		ArrayList n_lst = new ArrayList();
		ArrayList a_lst = new ArrayList();
		ArrayList v_lst = new ArrayList();
		ArrayList x_lst = new ArrayList();
		ArrayList t_lst = new ArrayList();
 
		output = new String[FINAL_N];
 
		// Being processing loop
		for( int i = 0; i < FINAL_N; i++ ) {
			a = data[i];
			v_last = v_next;
			x_last = x_next;
			//x_next = a*DELTA**2 + v_last*DELTA + x_last
			x_next = a*Math.pow(DELTA,2) + v_last*DELTA + x_last;
			output[i] = n+" "+a+" "+v_last+" "+x_next+" "+n*DELTA;
			x_lst.add(x_next);
			//output.append((n,a,v_last,x_next,n*DELTA))
			v_next = a*DELTA + v_last;
		}
 
		// output
		for(int index = 0;index < output.length;index++) {
			System.out.println(output[index]);
		}
 
		// ACc out
		System.out.println("");
		for(int index = 0;index < output.length;index++) {
			System.out.println(x_lst.get(index));
		}
	}
 
    public static void main(String[] args) {
	    // test acceleration data
	    double[] data = {1.0,1.0,1.0,1.0,1.0,1.0};
	    AccToLoc obj = new AccToLoc(data,0.1,data.length);
    }
}
 

The translation took longer than I liked because I was
a bit rusty on the java.

You must be logged in to post a comment.