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
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 件のコメント
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
num2hex(x)
xbytes = typecast(x, 'uint8')
dec2hex(xbytes)
back_to_double = typecast(xbytes, 'double')
back_to_double - x
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
2021 年 12 月 6 日
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
2021 年 12 月 6 日
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!