How to get last two bits except for LSB from audio signal in MATLAB?
1 view (last 30 days)
Show older comments
ayesha jabeen
on 4 Oct 2016
Commented: Walter Roberson
on 9 Oct 2016
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
Accepted Answer
Walter Roberson
on 5 Oct 2016
bitset(YourArray, bitset(YourArray, 2, NewBit2Array), 3, NewBit3Array)
3 Comments
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.
Walter Roberson
on 9 Oct 2016
Opps, should be
bitset(bitset(YourArray, 2, NewBit2Array), 3, NewBit3Array)
More Answers (1)
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
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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!