Error in converting binary to audio after recording the audio and converting it into binary .......KINDLY HELP!!

i recorded an audio , converted it into binary and now i have to convert it back into the audio but there is noise in the audio signal which i got in the result , its not the same. what should i do to get the same audio as i recorded ...after converting it back from binary . i even normalised the values... i am new to matlab ... kindly help... i will be very grateful for this .

 Accepted Answer

format long g
myRecording = randn(5,1)
myRecording = 5×1
-0.742153922329459 1.51715451030976 -0.738441174539258 1.86274349485519 0.470504960861284
wavebinary = dec2bin( typecast( single(myRecording(:)), 'uint16'), 16 ) - '0';
size(wavebinary)
ans = 1×2
10 16
Notice that is N x 16 where N is number of samples times 2 (you are converting myRecording to single no matter whether it is integer or single or double, so the result of the single() will be 32 bits per sample no matter what the original bits per sample. Then you typecast() into two 16 bit halves, and you convert each 16 bit value to binary. Each converted value creates a 16 bit row.
Now look further and see that you construct
sounddata = double(bin2dec( char( reshape( wavebinary, 16, []).'+'0')));
So you would take that 10 x 16 data in which the rows represent the binary of half of one sample, and you would reshape it to be 16 rows... which in this case would give you 10 columns. But that is the wrong bit order. You are taking like
1 : 16
17 : 32
33 : 48
and rearranging it like
[1 17 33 ... 16 32 48 ...]
The flaw here is that when you create wavebinary you should be doing
wavebinary = reshape((dec2bin( typecast( single(myRecording(:)), 'uint16'), 16 ) - '0').', 1, []);
and now with the transpose the bit order would follow the way you are reconstructing.

7 Comments

After you do the bin2dec(), you have to typecast() back to single()
Note: when you post pictures of code, the volunteers cannot test and edit the code without typing it all in by hand, which is more work than you should reasonably expect the volunteers to go to. If you are hoping for repaired code as a response, then post editable code, not pictures of code.
load handel.mat;
nob=8;
noc=1;
recObj=audiorecorder(Fs,nob,noc);
recordblocking(recObj,5);
play(recObj);
myRecording=getaudiodata(recObj);
plot(myRecording);
audiowrite('myRecording.wav',myRecording,Fs);
samples=[1*3*Fs];
[myRecording, fs] = audioread('myRecording.wav');
wavebinary = dec2bin( typecast( single(myRecording), 'uint8'), 8 ) - '0';
wavebin=transpose(wavebinary)
wavbin_size=size(wavebinary);
sounddata = double(bin2dec( char( reshape( wavebin, 8, []).'+'0')));
sounddata = sounddata - min(sounddata(:));
sounddata = sounddata ./max(sounddata(:));
audiowrite('rashi.wav',sounddata,Fs);
sound(real(double(sounddata)));
load handel.mat;
myRecording = y; %name from file
%encode data into bits
wavebin = reshape((dec2bin( typecast( single(myRecording), 'uint8'), 8 ) - '0').',1,[]);
%decode data from bits into samples
sounddata = typecast(uint8(bin2dec( char(reshape( wavebin, 8, []).'+'0'))),'single') .';
audiowrite('testrestored.wav',sounddata,Fs);
sound(sounddata, Fs)
%are they the same ?
maxerror = max(abs(myRecording(:) - sounddata(:)))
maxerror = single 2.8010e-08
No, not the same, but not very different.
Why are they different? Well, because you converted the double precision data into single precision when you took single(myRecording)
Thank you so much it worked and sorry to bother you again but how can i observe the data which was encoded in bits because it says "Cannot display summaries of variables with more than 524288 elements". And also can u please explain how this data is more than 5242888 elements ?
Displaying variables in graphics format takes time and memory. In order to prevent opening a variable from potentially taking a long time and using too much memory, Mathworks made a decision to limit the display t o 524288 elements. I do not know how Mathworks choose that exact number; it is 2^19 entries
Why is the data more than 524288 elements? Well,
format long g
%you are converting elements to single() which is 32 bits each
load handel.mat
handel_samples = numel(y)
handel_samples =
73113
required_bits = handel_samples * 32
required_bits =
2339616
max_samples_supported = 524288 / 32
max_samples_supported =
16384
max_time_at_8192 = max_samples_supported / Fs
max_time_at_8192 =
2
actual_time = handel_samples / Fs
actual_time =
8.9249267578125
So the handel data is just under 9 seconds long, but at 8192 samples per second, anything over 2 seconds would exceed the Variable Browser limit of 524288 elements.
To observe the data that was encoded into bits, at the command window ask to disp() some subset of it, such as disp(wavbin(1:100)) . Realistically speaking, there is no way that you would truly want to look by hand through all of the 2339616 bits of the data... what would you even look for by eye?

Sign in to comment.

More Answers (0)

Categories

Find more on Measurements and Spatial Audio 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!