How to Convert .wav file into binary?

89 Ansichten (letzte 30 Tage)
Asra Khalid
Asra Khalid am 7 Mär. 2018
Kommentiert: Jan am 31 Mai 2021
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 Kommentare
Jan
Jan am 7 Mär. 2018
Bearbeitet: Jan am 7 Mär. 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 am 7 Mär. 2018
Bearbeitet: Jan am 7 Mär. 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]

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 7 Mär. 2018
Bearbeitet: Jan am 7 Mär. 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.
  10 Kommentare
Rashika Raina
Rashika Raina am 31 Mai 2021
fid = fopen(FileName, 'r');
what is 'r'?
Jan
Jan am 31 Mai 2021
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

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

Kategorien

Mehr zu Audio I/O and Waveform Generation finden Sie in Help 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