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

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.
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.
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.
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'?
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