How to convert a wav file data into binary (0,1) format, so that it can again be converted from binary form to wav?

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 18 Mär. 2013

1 Stimme

wavdata = wavread('FileName.wav');
wavbinary = dec2bin( typecast( single(wavdata(:)), 'uint8'), 8 ) - '0';
orig_size = size(wavdata);
Then the reverse:
wavdata = reshape( typecast( bin2dec( uint8(wavbinary + '0') ), 'single' ), orig_size );

6 Kommentare

sangeeta
sangeeta am 19 Mär. 2013
the reverse says, ??? Error using ==> bin2dec Input must be a string.
Walter Roberson
Walter Roberson am 19 Mär. 2013
wavdata = reshape( typecast( uint8(bin2dec( char(wavbinary + '0') )), 'single' ), orig_size );
Jan
Jan am 19 Mär. 2013
Bearbeitet: Jan am 19 Mär. 2013
Then use "char" instead of "uint8".
I do noth think, that representing bits as CHAR array is a useful idea. Therefore I avoid DEC2BIN in general, but perform the conversion arithmetically.
Nishant Prakash
Nishant Prakash am 7 Aug. 2014
Hi Walter can you please explain the 2nd step in short.
Nelson Rodriguez Hernandez
Bearbeitet: Nelson Rodriguez Hernandez am 8 Jul. 2016
But if I have a binary array of only "Zeros and Ones"(0,1), how can I create sound from binary in .wav or other extension? Do I have to create my own samples using audiowrite, bin2dec or what? In other words how can I sampling from binary?
Walter Roberson
Walter Roberson am 8 Jul. 2016
Assuming your binary array of 0 and 1 is stored in wavbinary then
data_class_to_use = 'int32'; %or as appropriate
SampleRate = 22100; %set as appropriate
wavdata = reshape( typecast( uint8(bin2dec( char(wavbinary + '0') )), data_class_to_use ), orig_size );
audiowrite('FileNameGoesHere.wav', wavdata, SampleRate)
The data_class_to_use will depend upon kind of data you intend your binary array to represent. The sample code I gave here assumes that your binary array uses groups of 32 bits per sample, and that the 32 bits represent a signed 32 bit integer. For the question that was originally posted, it was 'float' that was appropriate to use, because the binary array represented 32 bit single precision numbers.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Jan
Jan am 19 Mär. 2013

0 Stimmen

What exactly are "wav file data"? Are you talking about doubles or integer types?
What is the prupose of converting the data to a binary format, when you consider, that all data are store in a binary format inside the computer. What do you want to achieve?

3 Kommentare

sangeeta
sangeeta am 20 Mär. 2013
I actually want to store audio data in an Image, that is hiding wav binary data(0,1) in pixels of an Image (Steganography), in which pixels would be selected using an algorithm.
Kushant
Kushant am 2 Apr. 2014
i also have the same problem...
i want to use generated c code on dsp, but matlab coder do not support wavread function. i am planning to use fopen and fread but for that i need binary files instead of .wav files.
please tell me how to do it?
and how it can be done arithmatically?
Walter Roberson
Walter Roberson am 2 Apr. 2014
Outside of the dsp, wavread() the .wav files, and fwrite() them to a binary file. In the dsp read the binary file.

Melden Sie sich an, um zu kommentieren.

Kyaw Yegaung
Kyaw Yegaung am 24 Sep. 2018

0 Stimmen

I get the problem converting wav file to binary with wave file read command wavread. But i use another command audioread. Authoug it was work i got trouble in another command like dec2bin. It said that deceimal be less than 2 power 52. The point is what actually the data type of wav or other audio source file and how can i convert to decimal before converting binary data. If anyone could help me please contact my mail 'oooooyaungoooo@gmail.com'. Thank you.

Kategorien

Mehr zu Audio I/O and Waveform Generation finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by