MATLAB Answers

How to Convert .wav file into binary?

144 views (last 30 days)
Asra Khalid
Asra Khalid on 7 Mar 2018
Answered: Omar Longoria on 12 Apr 2019
load handel.mat
filename = 'handel.wav';
audiowrite(filename,y,Fs);
clear y Fs
[y,Fs] = audioread('handel.wav');
sound(y,Fs)
I want to use the following code to convert the .wav audio file into binary. Which MATLAB command should be used for converting .wav file in to binary?
TIA.

  4 Comments

Show 1 older comment
Asra Khalid
Asra Khalid on 7 Mar 2018
Binary stream (zeros and ones).
Jan
Jan on 7 Mar 2018
@Asra: Please try to explain the problem clearly. A binary stream of what? The signal or the contents of the file? If the data are stored in floating point format, do you want to convert the data to int8/16/32 at first or is is sufficient to interpret the binary representation of the IEEE754 doubles? Do you want a char vector or UINT8?
The less the readers have to guess, the easier and more likely matching is the answer.
Jan
Jan on 7 Mar 2018
[MOVED from section for answers]
I have to transmit sound file (.wav file) through LED's. For that I have to convert the .wav file in binary digits (0's & 1's) to turn LED's ON and OFF. I need the contents of the file to be converted into binary digits.
I hope it is more clear to you now.
[Please post comments in the section for comments]

Sign in to comment.

Accepted Answer

Jan
Jan on 7 Mar 2018
Edited: Jan on 7 Mar 2018
If you simply want to get a bit-stream representation, it does not matter in any way, if the data are a wav file or a jpeg. A WAV file contains additional header information compared to the pure signal. Getting a bit-stream from it:
fid = fopen(FileName, 'r');
data = fread(fid, [1, Inf], 'uint8');
fclose(fid);
bit = uint8(rem(floor((1 ./ [128, 64, 32, 16, 8, 4, 2, 1].') * data), 2));
bit = bit(:);
Now you have a bit stream of 1s and 0s stored in an UINT8 vector. This contains the complete data of the file and does not care in any way about the contents or format.

  2 Comments

Asra Khalid
Asra Khalid on 7 Mar 2018
bit = uint8(rem(floor((1 ./ [128, 64, 32, 16, 8, 4, 2, 1].') * data), 2));
bit = bit(:);
Can you please explain these two lines?
Jan
Jan on 7 Mar 2018
Imagine that data is [1, 3, 6, 8]. Now it is multiplied by the vector:
1 ./ [128, 64, 32, 16, 8, 4, 2, 1].'
This means a division by powers of 2. If you round this result towards zero by floor, the remainder to 2 is the binary representation of the input element.
Try this for 163:
163 ./ [128,64,32,16,8,4,2,1]
[1.27, 2.55, 5.09, 10.19, 20.38, 40.75, 81.5, 163.0]
Now floor it:
[1, 2, 5, 10, 20, 40, 81, 163]
and get the remainder to 2:
[1, 0, 1, 0, 0, 0, 1, 1]
This means that 163 equal 1*128 + 1*32 + 1*2 + 1*1.
bit = bit(:)
reshapes the matrix bit to a column vector.
The shown method is more direct than the nicer:
bit = dec2bin(data) - '0';
dec2bin creates a char vector of '0' and '1' by a method equivalent to my suggestion. Converting it back to numerical data is an indirection.

Sign in to comment.

More Answers (1)

Omar Longoria
Omar Longoria on 12 Apr 2019
Very good explanation. Thanks.

  0 Comments

Sign in to comment.