What does the 'audioread' function actually do?

I have a .wav file exported by a vibration analyser Svan 958A. It contains data from 4 channels. The samples are written in hexadecimal representation.
I read the file by executing the following line: [data,fs] = audioread('svan0004.wav','native'). MATLAB correctly organises the data in an 4-column array. Since the file was obtained at 24 bits per sample, the output array elements are of the type int32.
However, there is something wrong with the reading/extraction of the sample values from the .wav file. The first 4 values of each column are known to me, because they include the data definitions for each channel. By looking at those values I realised that somehow MATLAB did not convert the sample hexadecimal numbers as I was expecting. Here is what I get. The following values are the first 4 extracted values for the 4 channels:
Channel 1: [256, 512, 4199168, 0];
Channel 2: [512, 512, 4199168, 0];
Channel 3: [768, 512, 4199168, 0];
Channel 4: [1024, 512, 4452608, 0];
The following values are what should have been read:
Channel 1: [1, 2, 16403, 0];
Channel 2: [2, 2, 16403, 0];
Channel 3: [3, 2, 16403, 0];
Channel 4: [4, 2, 17393, 0];
Could you explain me what does the 'audioread' function actually do? What is the way around this? How can I extract the sample values correctly from my .wav file?
Thank you for the support. Best regards, Pedro Ochôa

1 comentario

José-Luis
José-Luis el 7 de Jul. de 2016
Divide by 256?
Have you read the documentation on audioread()?

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 7 de Jul. de 2016

0 votos

When a value is 24 bits placed in a 32 bit integer, the value can either be packed at the "left" or at the "right" of the 32 bit integer. Whatever is creating the values is packing at the "left". MATLAB does not know that; it just thinks it is working with 32 bit values.
To get your expected values back, divide the values by 256. But watch out for sign extension: if you do use int32() instead of uint32() and you divide (say) 0x85401300 by 256 then you would get 0xFF854013 as the result as the sign bit propagates on the signed division. If you would want 0x85401300 to become 0x00854013 then you should be using uint32 instead of int32

4 comentarios

Walter,
Thank you for your answer. In the array of int32 values that I get right after reading the .wav file with 'audioread', there are values with minus sign. Does that mean that the original hexadecimal numbers contained sign?
Another question popped up. According to the documentation of 'audioread', if I ask data type to be 'double' the sample values will be normalised. What are normalised to?
Thank you. Best regards, Pedro Ochôa
Walter Roberson
Walter Roberson el 7 de Jul. de 2016
If the first bit of the 32 bit numbers is set, then the value will be considered negative when interpreted as an int32 .
Normalization divides by the maximum value for the data type, so in this case by intmax('int32') which would be 2^31-1 . Which is good if you want to look at the values as being proportions of min or max (as is common) but not appropriate if you have encoded binary values.
To be more correct, the division would be by 2^31, so the maximum negative value of -(2^31) would become -1.0 exactly, and the maximum positive value of 2^31-1 would become slightly less than +1.0 . The asymmetry is due to their being 1 more representable negative values than positive values because one of the non-negative positions is used for 0.
Once again thank you for your reply, Wlater.
I got slightly confused with the explanation of the normalisation. If I specifically ask 'audioread' to import in the 'double' type, will the values be normalised by 2^31? I am asking this because, although MATLAB uses int32 to store the extracted data (when asked in 'native' format), in fact each sample point has 24 bits.
Thank you for your support. Best regards,
Pedro Ochôa
Walter Roberson
Walter Roberson el 8 de Jul. de 2016
The data that has been stored in the file was effectively already multiplied by 256 to produce a 32 bit number.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Audio I/O and Waveform Generation en Centro de ayuda y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by