Code for CCITT speech compression standards G.711, G.721, G.723 may be found at Sun Microsystems, Inc. Speech Compression Code
Sample input .wav file (16-bit, 8kHz): audio16bit.wav
Corresponding sample input .raw file file: audio16bit.raw
Sample output 4-bit .raw file after compression by 4-bit G.721 file: audio4bitg721.raw
Sample restored 16-bit .raw file after compression: audio16bitout.raw
Corresponding output .wav file, restored from compressed G.721 file: audio16bitout.wav
Conversion from .wav files to .raw files can of course be carried out in C. But it is instructive to do the conversion in Matlab, because then one can easily examine sample values and compare input and output signals.
A very simple version of such code goes as follows:
%%%%%%%%%%%%%%%%%
[Y,FS,NBITS,OPTS]=wavread('audio16bit.wav');
% Y is in -1..1
% Make a raw output file:
YB = YB+1;
YB = YB*256;
fid=fopen('audio16bit.raw','w')
fwrite(fid,YB,'int16');
fclose(fid);
% After compression by G.721:
fid=fopen('audio16bitout.raw','r')
[YYB,n]=fread(fid,'int16');
fclose(fid)
% Put in range -1..1:
YY = YYB/256;
YY=YY-1;
wavwrite(YY(:,1),FS,NBITS,'audio16bitout.wav');
%%%%%%%%%%%%%%%%%