Results 1 to 4 of 4
  1. #1
    KittenFace is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Default 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
    Java 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;
    		}
    	}
    Likewise, conversion back to Short[] simply * the double value by 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!

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Why not do this instead?
    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();
    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?

  3. #3
    Fillis52 is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default

    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

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,438
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fillis52 View Post
    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
    Encoding doubles in the range [-1, 1) to shorts by scaling them as the OP showed is fine; you do lose some precision though because a double has 53 bits of precision in its mantissa while a short only stores 15 of them.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. double a * double b = weird output
    By GPB in forum New To Java
    Replies: 3
    Last Post: 03-26-2010, 10:40 AM
  2. Need help with short problem
    By fataguila in forum New To Java
    Replies: 1
    Last Post: 01-30-2010, 08:58 AM
  3. Short Circuiting?
    By Philly0494 in forum New To Java
    Replies: 12
    Last Post: 11-12-2009, 10:45 AM
  4. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 08:39 AM
  5. Short-cut key
    By tskumarme in forum New To Java
    Replies: 2
    Last Post: 05-28-2008, 04:16 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •