Fread type problem

This is a discussion on Fread type problem within the C++ Programming forums, part of the General Programming Boards category; Hello, I am trying to read in values from a wav file. Each sample is 16 bits long (eg equivalent ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    11

    Fread type problem

    Hello,

    I am trying to read in values from a wav file. Each sample is 16 bits long (eg equivalent to a short int).

    I want to run a FFT routine on the values then which takes a double array.

    When I try to read in the values from the wav file with fread straight to the double array (with the code)

    Code:
    fread(sound_buffer, 2, 512, WavFile);
    it puts two samples into each element of the double array sound_buffer as (presumably) each element is 32 bits and each sample is only 16 bits long.
    This means that the values in the double array are useless.

    I have it working by first f reading the values into a short int array then copying them into a double array but this is hugely ineffecient as it's going to be called a few hundred times a second.

    Does anyone know how to use fread to copy one short int to every double element in the array?

    Thanks a million,
    Conor.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,459
    it is impossible, values in integer types and float types are internelly stored differently
    so you still need the explicit conversion of each value from short int to double

    PS. not on every platform short is 16-bit
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    I would think you could use an array of unions. You can then treat it as either a short int or a double. You can read in the values as a short int and then use them as a double with the calculations. Just be sure to zero out the array before you read more values into it.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,459
    Quote Originally Posted by manofsteel972
    I would think you could use an array of unions. You can then treat it as either a short int or a double. You can read in the values as a short int and then use them as a double with the calculations. Just be sure to zero out the array before you read more values into it.
    Just take the sample and see what you get
    Code:
     union MyUnion
     {
      short int x;
      double y;
     } myUnion;
     myUnion.x = 15;
     printf("%f",myUnion.y );
    I don't think he wants that result
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > Each sample is 16 bits long (eg equivalent to a short int).
    a) which byte order - MSB first, or LSB first?
    b) represented as what?
    [0 .. 65535] maps to [0.0 .. 1.0), say a 0.16 fixed point number
    [0 .. 65535] maps to [0.0 .. 256), say a 8.8 fixed point number
    Or something else?

    The union trick isn't going to work at all, because they're different sizes.

    > but this is hugely ineffecient as it's going to be called a few hundred times a second.
    File reading is a hell of a lot worse than any simple conversion from something 'int' to something 'float'.

    fread the whole block of ints into an array of short int, then use a loop to perform the conversion into your array of doubles.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    11
    Quote Originally Posted by vart
    it is impossible, values in integer types and float types are internelly stored differently
    so you still need the explicit conversion of each value from short int to double

    PS. not on every platform short is 16-bit

    Thanks for the help.

    If this is the case then I think even if there was a way of padding out f read, the values would still need to be converted so I guess I'll just have to work around it.


    Thanks,
    Conor.

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    11
    Quote Originally Posted by Salem
    > Each sample is 16 bits long (eg equivalent to a short int).
    a) which byte order - MSB first, or LSB first?
    b) represented as what?
    [0 .. 65535] maps to [0.0 .. 1.0), say a 0.16 fixed point number
    [0 .. 65535] maps to [0.0 .. 256), say a 8.8 fixed point number
    Or something else?
    They're signed short ints stored in little endian (LSB first) as far as I know. I don't know the representation but I don't need to know it as the function that works with the numbers is already written, I just need to pass in a doube array containing these samples.

    Quote Originally Posted by Salem
    >
    fread the whole block of ints into an array of short int, then use a loop to perform the conversion into your array of doubles.
    I'll try this and see how the performance is.
    Thanks.

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,459
    if your function that works with the numbers works with doubles and not with integers - the most performance issues will be there...
    If you are intrested in performance - you should find a version that makes a conversion using integer arithmetics
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    11
    Quote Originally Posted by vart
    if your function that works with the numbers works with doubles and not with integers - the most performance issues will be there...
    If you are intrested in performance - you should find a version that makes a conversion using integer arithmetics
    You're quite right. I think though the fact that it's performing a Fourier transform means they're going to need the percision of doubles so they're going to have to be cast to doublles at some stage, it's probably easier just to do it before I pass them in.

    Thanks for all the help.
    Conor.

  10. #10
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    I know that you do not use a union to do type conversions.

    I was under the impression that you needed to store an short int in the same space as a double. You could calculate using the integer and then store the result back into the same location as a double. But it seems you have figured it out.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Replies: 4
    Last Post: 03-21-2004, 02:34 PM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM
  5. qwerty/azerty keyboard type problem + question about loop.
    By Robin Hood in forum C++ Programming
    Replies: 9
    Last Post: 07-22-2002, 01:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21