Results 1 to 4 of 4
- 12-06-2010, 03:29 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 1
- Rep Power
- 0
short [] <-> double[] conversion query
I am using some example code to try and implement some Matlab capabilities in Java. One of the methods involves the conversion of a short[] to and from a double[].
Here is the code from the example
Likewise, conversion back to Short[] simply * the double value by 32768Java Code:public ShortAndDouble(short[] array) { shortArray = array; doubleArray = new double[shortArray.length]; for (int i = 0; i < shortArray.length; i++) { doubleArray[i] = (double)(shortArray[i]) / 32768; } }
I am still new to this kind of thing, but wont this method loose some precision. Is there a more precise way to do the conversion?
Many thanks!
- 12-06-2010, 07:15 PM #2
Why not do this instead?
I'm not sure why you're dividing by 2^15, in all honesty... you could even just do a single cast from short to double. The short value 5 is equivalent to the double value of 5.0, not 0.000152587890625. Is your data encoded somehow in a specific range?Java Code:Converting from double to short: double d = 5.0; short s = (new Double(d)).shortValue(); Converting from short to double: short s = 5; double d = (new Short(s)).doubleValue();
- 12-06-2010, 07:24 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 3
- Rep Power
- 0
This is example code from a basic implementation of DSP. I thought that the way it was done was hacky, but didnt know how to improve on it with the basic knowledge I have
- 12-06-2010, 07:30 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
double a * double b = weird output
By GPB in forum New To JavaReplies: 3Last Post: 03-26-2010, 10:40 AM -
Need help with short problem
By fataguila in forum New To JavaReplies: 1Last Post: 01-30-2010, 08:58 AM -
Short Circuiting?
By Philly0494 in forum New To JavaReplies: 12Last Post: 11-12-2009, 10:45 AM -
Double.valueOf() vs Double.parseDouble()
By greenbean in forum New To JavaReplies: 10Last Post: 01-12-2009, 08:39 AM -
Short-cut key
By tskumarme in forum New To JavaReplies: 2Last Post: 05-28-2008, 04:16 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks