What is the fastest way to get bit stream from 1D float data ?

Hi,
I have a 5000x1 double data which returned from audio read file. I need to get bit stream(bit vector)(numeric value includes 1s and 0s) from double data.(actually the output is float but matlab says double.) I tried two ways to get bit stream.
First I used typecast to 64 bit and get 5000x64 integer value, dec2bin and str2num. But str2num is so slow for large data(for example 16000000x1 double data) and got double data back from bit stream using num2str and bin2dec, dec2hex and hex2num. When I sounded it it was perfect. The problem was about str2num and num2str. Is there any efficient way to get the process faster ?
Second way was to read audio file using 'native'. So sampled data type was up to bits per sample of an audio data. (I read integer data )
It was faster 5 times than first step but when I sounded it, it was not clean like first step output. But I reduced the steps using native. I did not need to convert data to 64 bit. So second step was faster.
If my question got you confused , could you read any audio file using first audioread() and then audioread('native') ? How do you get bit array (1,0,1,0,0,0...) from sampled data you get from audioread() or audioread('native')?
Thanks for your time.

Answers (1)

I do not really get, what you try to do. Which format does the audiofile have? What do you call "bit stream"?
Instead of explaining the code, it would be easier to understand, if you post the code.
dec2bin converts the decimal numbers to a char vector. What do you do with str2num afterwards? Do you know the faster and safer tools str2double and sscanf?
The conversion between different types can be avoided to save time:
bin = rem(floor(dec(:) .* pow2(1-nBit:0).'), 2);
This converts integer values to a vector of 0 and 1.

7 Comments

Okay. Could you read any audio file using first audioread() and then audioread('native') ? And how do you get bit array (1,0,1,0,0,0...) from sampled data you get from audioread() ?
"dec2bin converts the decimal numbers to a char vector. What do you do with str2num afterwards? "
Because when I use typecast() to cast sampled data to integer, the output was 5000x64 char array.str2num() converts char array to numeric data.
"Do you know the faster and safer tools str2double and sscanf?"
I know that str2double is faster than str2num. But str2double() does not give me numeric value.
@nur dsc: I'm not sure, what your comments mean. I do understand what dec2bin and str2num do. I've tried to clarify, that the conversions from a numerical integer to a char vector and bach to a numerical vector is not efficient.
It would help to understand your problem, if you post a copy of your code instead of explanations in words, what your code does. E.g. "str2double() does not give me numeric value" is not clear - of course it replies numeric values.
"could you read any audio file using first audioread() and then audioread('native') ?"
Yes, of course you can. But I cannot imagine any useful purpose of importing the signal twice.
With importing the audio file in 'native' mode, you obtain the data as they are saved in the file. But which format is it? Knowing this would simplify posting a working solution.
If the signal is store in floating point format, you get values inside the interval [-1, +1]. What do you expect as bit stream of these data?
There are some different outputs for audioread(file, 'native'). See:
doc audioread
There is no genereal conversion for all types of output.
"Yes, of course you can." : I have just said that can you (not me) read an audio file and show me how you do get bit array from your sample wav file. I don't have any useful code to share.
"But I cannot imagine any useful purpose of importing the signal twice." : I was trying to show the difference between adioread() output and audioread('native') output. if your wav file internally integer datatype, you can get int16 data using 'native' . But just using audioread() it gives you float data. So what is your conversion step to get bit array in these cases.
"But which format is it? " : int16 format for example.
"If the signal is store in floating point format, you get values inside the interval [-1, +1]." : You are right.
"What do you expect as bit stream of these data?" : I just need quantized data( or bit array) from float data. How do you do it ?
Thanks !
The description of the problem is still nor precise. There are standard, A-law and u-law wave files with uint8, int16/32/64, single and double data. They all reply different values and you have to decide e.g. if the 2-th complement is wanted for negative values.
Okay Jan.
Let me wait for others to share code and explain me the faster way to do it.
Thanks for help !
You are welcome. I have still do not know, what you want to solve, so I cannot provide more suggestions.

Sign in to comment.

Categories

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

Products

Asked:

on 3 Dec 2021

Commented:

Jan
on 6 Dec 2021

Community Treasure Hunt

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

Start Hunting!