What does the 'audioread' function actually do?

5 views (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 7 Jul 2016
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 Comments
Pedro Andre Viegas Ochoa de Carvalho
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 on 8 Jul 2016
The data that has been stored in the file was effectively already multiplied by 256 to produce a 32 bit number.

Sign in to comment.

More Answers (0)

Categories

Find more on Audio I/O and Waveform Generation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!