MATLAB: I want to convert a .wav file to binary,modulate it,transmit,demodulate and convert the binary back to .wav file.But I am not able to write binary data back to wav file.

binary to wav

wavdata = audioread('roja.wav');
wavbinary = dec2bin( typecast( single(wavdata(:)), 'uint8'), 8 ) - '0';
orig_size = size(wavdata);
wavb1=uint8(wavbinary);
w1=reshape(wavb1,15486976,1);
xmod=qammod(w1,16,0,'gray');
y1=awgn(xmod,10);
rdmod=qamdemod(y1,16,0,'gray');
% rdmod1=reshape(rdmod,1935872,8);
rdmod2=uint8(rdmod);
data = uint8(bin2dec( char( reshape( rdmod2, 8,[]).'+'0')));
audiowrite(data, 'rec_roja.wav',8000)
Error using bin2dec (line 55)
Binary string may consist only of characters 0 and 1
Error in wavb (line 14)
data = uint8(bin2dec( char( reshape( rdmod2, 8,[]).'+'0')));
also waveread is not working in r2016

Best Answer

Which release are you using? R2016a was the last release that accepted by initial phase (0) and symbol order (gray) for qammod, with R2016b being the last release that permitted initial phase.
In current releases, the default output for qamdemod is integer, and the integers can be anywhere from 0 to M-1 where M is the order you passed (which is 16 in your case). Current releases have an option 'outputtype', 'bit'
But when you qammod(), in current releases the default input is integer as well, and you would need 'inputtype', 'bit' to specify bits.
Which is to say, you are currently telling qammod that your inputs are integers in the range 0 to 15, but it just happens that none of them are anything other than 0 or 1. You then add white noise and decode. But the white noise might happen to knock some of the symbols into a different constellation, so you when you qamdemod you can get back integers other than what you put in.
If you were using modern MATLAB, you would
xmod=qammod(w1, 16, 'gray', 'inputtype', 'bit');
and
rdmod=qamdemod(y1, 16, 'gray', 'outputtype', 'bit');
and the problem with bin2dec would no longer occur.
Related Question