# Fread type problem

• 12-14-2006
conor20_ie
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.
• 12-14-2006
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
• 12-14-2006
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.
• 12-14-2006
vart
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
• 12-14-2006
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?

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.
• 12-14-2006
conor20_ie
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.
• 12-14-2006
conor20_ie
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.
• 12-14-2006
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
• 12-14-2006
conor20_ie
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.
• 12-15-2006
manofsteel972
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.