audioread() sampled data type conversion

Hi,
I have double data returned from audio read file using audioread(). I need to get bit vector(numeric value includes 1s and 0s). Could you read an audio file using audioread() or audioread('native') ? How do you get bit array (1,0,1,0,0,0...) from sampled data you get from audioread() or audioread('native')? And then how do you get double data back from bit array ?
Thanks!

回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 12 月 5 日

0 投票

audioread() with 'native' will only return an integer data type if the data is stored as integer internally. That is not always the case.
typecast() the returned data to 'uint8' and then convert the uint8 to bit vectors.
Hint: non-zero uint8 can be used as indices into a list of bits for the byte...

5 件のコメント

nur dsc
nur dsc 2021 年 12 月 5 日
編集済み: nur dsc 2021 年 12 月 5 日
But we can learn that if data type is integer internally or not using audioinfo() looking bit per sample ,right ? If our data is stored as int16 data type, typecasting to uint8(converting 16 bit to 8 bit ) will cuase data loss ? I think it is same for float data type(4 or 8 byte) which returned from audioread().
Walter Roberson
Walter Roberson 2021 年 12 月 5 日
But we can learn that if data type is integer internally or not using audioinfo() looking bit per sample ,right ?
I do not think that can be relied on. But it doesn't matter, since you can read with 'native' and then check the datatype of what you get back using isfloat() if you need to.
If our data is stored as int16 data type, typecasting to uint8(converting 16 bit to 8 bit ) will cuase data loss ?
No, you are thinking of cast() rather than typecast() . See for example,
format long g
x = pi
x =
3.14159265358979
num2hex(x)
ans = '400921fb54442d18'
xbytes = typecast(x, 'uint8')
xbytes = 1×8
24 45 68 84 251 33 9 64
dec2hex(xbytes)
ans = 8×2 char array
'18' '2D' '44' '54' 'FB' '21' '09' '40'
back_to_double = typecast(xbytes, 'double')
back_to_double =
3.14159265358979
back_to_double - x
ans =
0
So xbytes is a stream of uint8 extracted from the double, and the stream of bytes can be typecast back to double and exactly reconstruct the original data.
If you read the num2hex results starting from the right side towards the left, you can see matches with the dec2hex printout. The last byte of the num2hex representation is hex '18', and the first byte of the typecast is hex '18' . num2hex() displays in "big-endian" order, in which the "most significant" byte is first, but typecast() gives you the actual byte order in memory, which on an x64 architecture is "little-endian" order, in which the "least significant" byte is first.
nur dsc
nur dsc 2021 年 12 月 6 日
But you didn't show how unsigned int value was converted to bit array after getting xbytes :
xbytes_dec2bin = dec2bin(xbytes);
xbytes_dec2bin = xbytes_dec2bin(:);
xbytes_dec2bin_str2num = str2num(xbytes_dec2bin);
What is the faster way to do this ? (Bcs str2num or num2str is slow.)
Or how to get double sampled data back using bitstream effectively ? My code is slow for large data because of str2num, num2str and (:) operator.
Thanks !
Walter Roberson
Walter Roberson 2021 年 12 月 6 日
You are back to asking about the fastest conversion method. That requires testing, not theory. And fastest is the topic of your other post and so should be discussed there. Really this present Question is a duplicate, since you asked the same thing as part of the other question, but since it was more narrowly focused on the question of datatype, I allowed it instead of closing it as Duplicate. The "fastest" part, however, is squarely the topic of the other question so I am not going to respond here. (Do not open multiple questions on the same topic in hopes that you might get a faster response.)
nur dsc
nur dsc 2021 年 12 月 6 日
I think that this question is simpler than the other one.The other question was specific and got confused. It was not about getting a quick response. But thanks !

サインインしてコメントする。

カテゴリ

質問済み:

2021 年 12 月 5 日

コメント済み:

2021 年 12 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by