You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Audio to binary conversion
49 views (last 30 days)
Show older comments
Hello
i'm working on a project related to steganography ( audio steganography), and at first i want to convert an existing wav file to audio and extract the LSB's of it using a MATLAB script. how can i start?
i used this code:
[filename, pathname] = uigetfile('*.wav', 'Pick an audio');
wavebinary = dec2bin( typecast( single(filename(:)), 'uint8'), 8 ) - '0';
[r c] = size(wavebinary);
wavb= [];
wavd=[];
for k=1:r
wavb=horzcat(num2str(wavebinary(k,1:8),wavb));
x=bin2dec(wavb(1:8));
wavd=horzcat(x,wavd);
wavb=[];
end
wavd=fliplr(wavd);
it gives me a 36*8 martix don't know why
and i need more samples.. how can i do that?
2 Comments
Image Analyst
on 26 Dec 2017
Have you tried bitget()?
Accepted Answer
Walter Roberson
on 26 Dec 2017
Your code never reads the file. Your code is transforming the file NAME to binary.
38 Comments
Sara MH
on 26 Dec 2017
ops looks i got lost somehow with the variables.. i'll change it but if i want to convert the audio to a stream of bits.. can the code give the desired result? i have tried many times.. but i didn't get what i want and can i change the sampling frequency?
Walter Roberson
on 26 Dec 2017
[sounddata, fs] = audioread(filename);
wavebinary = dec2bin( typecast( single(soundata), 'uint8'), 8 ) - '0';
I cannot tell what the purpose of the rest of your code is.
The least significant bit of each byte of wavebinary will be
mod(wavebinary, 2)
Walter Roberson
on 26 Dec 2017
[filename, pathname] = uigetfile('*.wav', 'Pick an audio');
if ~ischar(filename)
return; %user cancel
end
filename = fullfile(pathane, filename);
[sounddata, fs] = audioread(filename);
wavebinary = dec2bin( typecast( single(soundata), 'uint8'), 8 ) - '0';
LSBs = mod(wavebinary, 2);
fs will be whatever is stored in the .wav file as the sampling frequency: it is not something that you input.
Walter Roberson
on 26 Dec 2017
wavebinary = dec2bin( typecast( single(soundata(:)), 'uint8'), 8 ) - '0';
The problem is not that the files were large, but rather that they were stereo.
Walter Roberson
on 27 Dec 2017
The code I posted converts them to binary.
Walter Roberson
on 7 Jan 2018
The code I gave previously,
wavebinary = dec2bin( typecast( single(soundata(:)), 'uint8'), 8 ) - '0';
converts to an N x 8 numeric array. If you embedded that into a file, you would have had to do so as a vector, which would have require that you choose a sequence to use the bits from the array -- which could have been that you went across the rows so that all information from a particular sample is grouped together, or it could have been that you went down the columns, as that would be the normal MATLAB order of addressing the bits.
I think it likely that if you are having difficulty reversing the line of code I showed here, that you are probably also having difficulty getting the extracted bits together into the proper order to turn back into sound. In order for us to help you with that, you will have to describe the order that the wavebinary bits were stored in the file.
Is the size of the sound that you embed a constant, or are you also embedding bits indicating what the size is? You need the size information in order to extract the information properly. At the very least you need to know how many channels and you need to know the point at which the embedding stops so that you do not end up extracting bits that were never set.
Sara MH
on 7 Jan 2018
Edited: Walter Roberson
on 7 Jan 2018
using the code that you have provided, i converted a .wav file with a size of 25.6 KB (26,302 bytes), as written in its properties, to the matrix:
wavebinary <52516x8 double>, as written in the workspace in MATLAB. now i want to re-shape a modified version of this matrix to a .wav file again.
hope this clarified things to you.
Sara MH
on 7 Jan 2018
Edited: Walter Roberson
on 7 Jan 2018
And the matrix that i want to reshape ((call it wavebinary2)) into .wav has the same number of bytes ( <52516x8 doublr.> , as the wavebinary now i want to reshape it again into .wav file
Walter Roberson
on 7 Jan 2018
sounddata2 = reshape( double( typecast(bin2dec( char(wavebinary2 + '0')), 'single') ), size(sounddata) );
and now you can use audiowrite() on that, being sure to specify the fs when you write.
Sara MH
on 7 Jan 2018
Edited: Walter Roberson
on 7 Jan 2018
the following error appears,
Error using reshape
To RESHAPE the number of elements must not change.
Error in MainSS (line 39)
stego_audio = reshape( double( typecast(bin2dec( char(stego_wavebinary + '0')),
'single') ), size(sounddata) );
Walter Roberson
on 7 Jan 2018
Please show class(sounddata), size(soundata), size(wavebinary), size(stego_wavebinary)
Walter Roberson
on 7 Jan 2018
stego_audio = reshape(double(typecast(uint8(bin2dec( char(stego_wavebinary + '0'))),'single')), size(sounddata));
Sara MH
on 8 Jan 2018
i know i've asked much about this :D .. but i used audiowrite after specifying fs:
StegoAudio= audiowrite(stego_audio,y,fs);
and the error says:
Error using audiowrite
Too many output arguments.
Error in MainSS (line 44)
StegoAudio= audiowrite(stego_audio,y,fs);
Walter Roberson
on 8 Jan 2018
audiowrite does not accept any output arguments. It writes to a file. You can fopen() the file to read it as binary, or you can audioread() the file if you want to check the contents.
Walter Roberson
on 8 Jan 2018
No, audioread() reads back from audio files. audiowrite converts data to .wav files.
Walter Roberson
on 8 Jan 2018
audiowrite('stego.wav', stego_wavebinary, fs);
Sara MH
on 8 Jan 2018
Edited: Sara MH
on 8 Jan 2018
thank you it worked! but i get distorted file with a longer time although i i changed the lsb and it has the same fs as the original one but the stego.wav size is much larger as w .wav file
Walter Roberson
on 8 Jan 2018
The size of the audio file itself depends upon compression, which the original might have used but the new version probably did not.
Or are you referring to what shows up for "bytes" when you use whos() ?
Walter Roberson
on 9 Jan 2018
Could you confirm that you are referring to bytes from the whos output for the variables, not looking at what MS Windows reports for the file size of the .wav file? And that the matrices show up as the same size()? What class() shows up for them?
Sara MH
on 9 Jan 2018
in the extraction process, i opened the file (stego) to obtain the matrix sounddata2 and then stego_binary2 to start extraction. check the following:
[filename, pathname] = uigetfile('*.wav', 'Pick an audio');
if ~ischar(filename)
return; %user cancel
end
filename = fullfile(pathname, filename);
[sounddata2, fs] = audioread(filename);
stego_wavebinary2 = dec2bin( typecast( single(sounddata2(:)), 'uint8'), 8 ) - '0';
size(stego_wavebinary2)
size(sounddata2)
Sara MH
on 9 Jan 2018
Edited: Walter Roberson
on 9 Jan 2018
before converting it to .wav , the matrix was called stego_wavebinary and it was exactly the same size as the original wavebinary (obtained from sounddata matrix)
summarizing the sizes:
1.sounddata = 13129 1
2. wavebinary = 52516 8
3.stego_wavebinary= 52516 8
4.stego_audio = 13129 1
and then stego_audio was converted to audio .. to be later converted to sounddata2 and then stego_wavebinary2 which are shown in previous comments.
Walter Roberson
on 9 Jan 2018
audiowrite('stego.wav', stego_audio, fs);
Walter Roberson
on 12 Jan 2018
Edited: Walter Roberson
on 12 Jan 2018
No, I prefer questions to be posted in public. Also, I am not interested in steganography itself.
More Answers (0)
See Also
Categories
Find more on Audio and Video Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)