How to Convert .wav file into binary?

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 件のコメント

Jan
Jan 2018 年 3 月 7 日
What exactly is "binary" in your case?
Asra Khalid
Asra Khalid 2018 年 3 月 7 日
Binary stream (zeros and ones).
Jan
Jan 2018 年 3 月 7 日
編集済み: Jan 2018 年 3 月 7 日
@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 2018 年 3 月 7 日
編集済み: Jan 2018 年 3 月 7 日
[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]

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

 採用された回答

Jan
Jan 2018 年 3 月 7 日
編集済み: Jan 2018 年 3 月 7 日

2 投票

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.

10 件のコメント

Asra Khalid
Asra Khalid 2018 年 3 月 7 日
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 2018 年 3 月 7 日
編集済み: Jan 2018 年 3 月 7 日
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.
Shannon Marie Falter
Shannon Marie Falter 2021 年 4 月 21 日
How would I write this back to become an audio file
Jan
Jan 2021 年 4 月 21 日
@Shannon Marie Falter: It depends on what you call "an audio file". Maybe with fwrite or with audiowrite.
Shannon Marie Falter
Shannon Marie Falter 2021 年 4 月 21 日
Well I do some work on the file after reading it then result with another binary sequence. I need to right this into an audio file of .flac or .wav, but I believe I need to change it back to numbers from binary first and don't know how.
I am trying to compare the original audio file with this one so I need to hear it.
Jan
Jan 2021 年 4 月 22 日
For hearing it you can play the original and modified arrays directly in Matlab using audioplay.
Sayantika Chakraborty
Sayantika Chakraborty 2021 年 5 月 17 日
I want to do the same with a whole audio datset..how do i do that? I dont want to do it seperately for each audio file
Jan
Jan 2021 年 5 月 17 日
What is "a whole audio datset"?
Rashika Raina
Rashika Raina 2021 年 5 月 31 日
fid = fopen(FileName, 'r');
what is 'r'?
Jan
Jan 2021 年 5 月 31 日
Whenever you have a question to a specific command, start with reading the documentation:
doc fopen
If you do not have a local Matlab installation, serach online for "Matlab fopen" to find: https://www.mathworks.com/help/matlab/ref/fopen.html#btrnibn-1-permission . Here you see:
'r' Open file for reading.

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

その他の回答 (1 件)

Omar Longoria
Omar Longoria 2019 年 4 月 12 日

0 投票

Very good explanation. Thanks.

タグ

質問済み:

2018 年 3 月 7 日

コメント済み:

Jan
2021 年 5 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by