Clear Filters
Clear Filters

How to get last two bits except for LSB from audio signal in MATLAB?

2 views (last 30 days)
i have a code in which i am getting LSB values of audio signal now i am want to get last two bits except for LSB..Here is code:
fid1=fopen('ash.wav','r');
header=fread(fid1,40,'uint8=>char');
data_size=fread(fid1,1,'uint32');
[dta,count]=fread(fid1,inf,'uint16');
fclose(fid1);
lsb=1;
identity=[1 0 1 0 1 0 1 0]';
dta(index)=bitset(dta(index),lsb,identity(1:8));
dta(index)=bitset(dta(index),lsb,m_bin(1:10));
index=([indices(19):indices(28)]);
dta(index)=bitset(dta(index),lsb,n_bin(1:10));
index=([indices(29):indices(28+len)]);
dta(index)=bitset(dta(index),lsb,msg_bin(1:len)');
how to do it?
  2 Comments
michael
michael on 4 Oct 2016
Please write the complete code in a code format, so that it would be readable
ayesha jabeen
ayesha jabeen on 5 Oct 2016
Edited: Walter Roberson on 5 Oct 2016
This is a code:
fid1=fopen([pathname filename],'r');
%first 40 bytes make wav header,store the header
header=fread(fid1,40,'uint8=>char');
%41st byte to 43rd byte,length of wav data samples
data_size=fread(fid1,1,'uint32');
%copy the 16 bit wav data samples starting from 44th byte
[dta,count]=fread(fid1,inf,'uint16');
%close the file only wav data samples are sufficient to hide the text
fclose(fid1);
lsb=1;
msg='Hello how are you?';
%run aurecover.m to recover this message from new2.wav file
msg_double=double(msg);
%convert it to double
msg_bin=de2bi(msg_double,8); %then convert message to binary
[m,n]=size(msg_bin); %size of message binary
msg_bin_re=reshape(msg_bin,m*n,1); %reshape the message binary in a column vector
m_bin=de2bi(m,10)';
n_bin=de2bi(n,10)';
len=length(msg_bin_re); %length of message binary
%len=m*n
len_bin=de2bi(len,20)'; %convert the length to binary
%hide identity in first 8 wav data samples.
identity=[1 0 1 0 1 0 1 0]';
dta(1:8)=bitset(dta(1:8),lsb,identity(1:8));
%hide binary length of message from 9th to 28 th sample
dta(9:18)=bitset(dta(9:18),lsb,m_bin(1:10));
dta(19:28)=bitset(dta(19:28),lsb,n_bin(1:10));
%hide the message binary starting from 29th position of wave data samples
dta(29:28+len)=bitset(dta(29:28+len),lsb,msg_bin(1:len)');
%open a new wav file in write mode
fid2=fopen('new2.wav','w');

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 5 Oct 2016
bitset(YourArray, bitset(YourArray, 2, NewBit2Array), 3, NewBit3Array)
  3 Comments
Walter Roberson
Walter Roberson on 5 Oct 2016
Those arrays should be the same size as YourArray, and they should hold the new value to go into bit 2 for each corresponding location, and the new value to go into bit 3 for each corresponding location.
If you are only changing a single location, you still need to use the two bitset() calls. bitset() is not able on its own to realize that you want to change multiple bits in a single location, so you need to call it once for each bit position to be changed at the location.

Sign in to comment.

More Answers (1)

Geoff Hayes
Geoff Hayes on 4 Oct 2016
ayesha - if
lsb=1;
and
dta(index)=bitset(dta(index),lsb,msg_bin(1:len)');
sets the least significant bit (lsb), then to set the bits before this one, you would just bitshift lsb to the left to modify the two bits prior to lsb. For example,
% bit shift once to to modify the bit prior to lsb
bitToModify = bitshift(lsb, 1);
% bit shift twice to modify the bit that is two away from lsb
bitToModify = bitshift(lsb, 2);
  4 Comments
Walter Roberson
Walter Roberson on 6 Oct 2016
Edited: Walter Roberson on 6 Oct 2016
bitset() returns a value rather than modifying the array in place, so you need to make those into assignments.
Also bitshift(lsb,2) would be 4 rather than 3 so you would not be storing into bits 1, 2, 3, but instead 1, 2, 4. The second parameter should just be a bit number in natural numbers.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!