MATLAB Answers

How to Convert .wav file into binary?

203 views (last 30 days)
Asra Khalid
Asra Khalid on 7 Mar 2018
Commented: Jan on 31 May 2021 at 11:03
load handel.mat
filename = 'handel.wav';
clear y Fs
[y,Fs] = audioread('handel.wav');
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?
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 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');
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.
Jan on 31 May 2021 at 11:03
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: . Here you see:
'r' Open file for reading.

Sign in to comment.

More Answers (1)

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

Community Treasure Hunt

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

Start Hunting!